package com.kfzs.duanduan.adp;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.R;
import com.kfzs.duanduan.proto.GameTypeSpecialOuterClass;
import com.kfzs.duanduan.utils.dlg.SkipUtils;

import java.util.List;

/**
 * 分类、top中gridview的adapter
 * Created by ljy on 2018/1/22.
 */

public class ClassificationGridviewAdapter extends BaseAdapter {
    private Context mContext;
    private List<GameTypeSpecialOuterClass.GameTypeSpecial> listGameTS;

    public ClassificationGridviewAdapter(List<GameTypeSpecialOuterClass.GameTypeSpecial> listGameTS,Context mContext) {
        this.mContext = mContext;
        this.listGameTS = listGameTS;
    }
    public void toBindTopviewData(List<GameTypeSpecialOuterClass.GameTypeSpecial> listGameTS){
        this.listGameTS = listGameTS;
    }

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Object tag = view != null ? view.getTag() : null;
        ViewHolder holder ;
        if(tag instanceof ViewHolder) {
            holder = (ViewHolder) tag;
        } else {
            holder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.classification_gv_item_item, null);

            holder.imageView = view.findViewById(R.id.classification_gv_item_iv);
            holder.textView = view.findViewById(R.id.classification_gv_item_tv);
            // 获取屏幕宽度
            int W = G.WIDTH <G.HEIGHT ? G.WIDTH : G.HEIGHT;
            int cricleRadius = W / 4 ;
            int h = cricleRadius * 83 / 100;
            view.setLayoutParams(new AbsListView.LayoutParams(cricleRadius, h));
            view.setPadding(0,15,0,20);
            view.setTag(holder);
        }

        final GameTypeSpecialOuterClass.GameTypeSpecial gameTypeSpecial = (GameTypeSpecialOuterClass.GameTypeSpecial) getItem(i);
        Log.e("gridveiw--",listGameTS.size() +","+(gameTypeSpecial == null));
        int type = 0;
        if(gameTypeSpecial == null){
            type = 0;
//            Bitmap bottomMap1 = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.classification_gengduoyouxo);
//            holder.imageView.setImageBitmap(bottomMap1);
            Glide.with(mContext)
                    .load(R.drawable.classification_gengduoyouxo)
                    .into(holder.imageView);
        }else{
            Log.e("gamepecial.getIcon()",listGameTS.size() +","+(gameTypeSpecial.getIcon()));
            type = gameTypeSpecial.getGameTypeId();
            switch (type){
                case 42:
                    holder.textView.setText("腾讯游戏");
                    holder.imageView.setImageResource(R.drawable.fen_lei_teng_xun);
                    break;
                case 49:
                    holder.textView.setText("仙侠专区");
                    holder.imageView.setImageResource(R.drawable.fen_lei_xian_xia);
                    break;
                case 44:
                    holder.textView.setText("热门推荐");
                    holder.imageView.setImageResource(R.drawable.fen_lei_re_men);
                    break;
                case 45:
                    holder.textView.setText("新游推荐");
                    holder.imageView.setImageResource(R.drawable.fen_lei_tui_jian);
                    break;
                case 46:
                    holder.textView.setText("二次元");
                    holder.imageView.setImageResource(R.drawable.fen_lei_er_ci);
                    break;
                case 47:
                    holder.textView.setText("吃鸡专区");
                    holder.imageView.setImageResource(R.drawable.fen_lei_chi_ji);
                    break;
                case 48:
                    holder.textView.setText("网易专区");
                    holder.imageView.setImageResource(R.drawable.fen_lei_wang_yi);
                    break;
                case 43:
                    holder.textView.setText("游戏预约");
                    holder.imageView.setImageResource(R.drawable.fen_lei_yu_yue);
                    break;
            }
//            Glide.with(mContext)
//                    .load(gameTypeSpecial.getIcon())
//                    .apply(new RequestOptions().error(android.R.drawable.stat_notify_error))
//                    .into( holder.imageView);
        }

        final int finalType = type;
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SkipUtils.getInstance().goGameType(mContext, finalType);
            }
        });
        final ViewHolder finalHolder = holder;
        holder.imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        finalHolder.imageView.setAlpha(0.2f);
                        break;
                    case MotionEvent.ACTION_UP:
                    default:
                        finalHolder.imageView.setAlpha(1f);
                        break;
                }
                return false;
            }
        });

        return view;
    }

    private class ViewHolder{
        ImageView imageView;
        TextView textView;
    }
}
