package com.yutou.jianr_mg.Adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.Log;
import com.yutou.jianr_mg.Tools.Utils;
import com.yutou.jianr_mg.views.ConfigActivity;
import com.yutou.jianr_mg.views.DownloadListActivity;
import com.yutou.jianr_mg.views.FavoritesActivity;
import com.yutou.jianr_mg.views.UpFeedBack;

import java.util.List;


public class MyItemGridViewAdapter extends BaseAdapter {
    public static class ItemData {
        private String title;
        private int imageUrl = -1;

        public ItemData(int imageUrl, String title) {
            this.imageUrl = imageUrl;
            this.title = title;
        }

        public int getImageUrl() {
            return imageUrl;
        }

        public String getTitle() {
            return title;
        }
    }

    private List<ItemData> list;
    private Context context;

    public MyItemGridViewAdapter(List<ItemData> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Item item = null;
        if (view == null) {
            item = new Item();
            view = LayoutInflater.from(context).inflate(R.layout.item_my_gridview, null);
            item.imageView = view.findViewById(R.id.image);
            item.title = view.findViewById(R.id.title);
            view.setTag(item);
        } else {
            item = (Item) view.getTag();
        }
        if (list.get(i).imageUrl != -1) {
            item.imageView.setImageResource(list.get(i).getImageUrl());
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(Utils.getPixelsFromDp((Activity) context,32),Utils.getPixelsFromDp((Activity) context,32));
            params.topMargin=Utils.getPixelsFromDp((Activity) context,8);
            params.leftMargin=Utils.getPixelsFromDp((Activity) context,24);
            params.bottomMargin=Utils.getPixelsFromDp((Activity) context,8);
            item.imageView.setLayoutParams(params);
        }
        item.title.setText(list.get(i).title);
        view.setOnClickListener(view1 -> {
            Log.i("点击" + list.get(i).getTitle());
            Intent intent = null;
            switch (i) {
                case 0:
                    intent = new Intent(context, DownloadListActivity.class);
                    break;
                case 1:
                    intent = new Intent(context, FavoritesActivity.class);
                    break;
                case 2:
                    intent = new Intent(context, UpFeedBack.class);
                    break;
                case 3:
                    intent=new Intent(context, ConfigActivity.class);
                    break;
            }
            if (intent != null)
                context.startActivity(intent);
        });
        return view;
    }

    private class Item {
        private ImageView imageView;
        private TextView title;
    }
}
