package com.kfzs.duanduan.adp;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


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

import java.util.ArrayList;


/**
 * 分类adapter
 * Created by Administrator on 2017/12/11.
 */

public class ClassificationAdapter extends BaseAdapter {  private ArrayList<String> mList;
    private LayoutInflater mInflater;
    private Context mContext;
//    List<GameTypeSpecialOuterClass.GameTypeSpecial> listGameTS;

    public ClassificationAdapter(ArrayList list, Context context) {
        this.mList = list;
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }

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

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Object o = getItem(position);
//        if(position == 0 ){
//            return createTopGridView();
//        }
        GameTypeOuterClass.GameType gameType = ((GameTypeOuterClass.GameType) o);
        convertView = mInflater.inflate(R.layout.classification_item, null);
        ImageView iconIv = convertView.findViewById(R.id.icon_iv);
        TextView icon_tv = convertView.findViewById(R.id.icon_tv);
        TextView one_tv = convertView.findViewById(R.id.one_tv);
        TextView two_tv = convertView.findViewById(R.id.two_tv);
        TextView three_tv = convertView.findViewById(R.id.three_tv);
        TextView four_tv = convertView.findViewById(R.id.four_tv);
        TextView five_tv = convertView.findViewById(R.id.five_tv);
        TextView six_tv = convertView.findViewById(R.id.six_tv);
//        LinearLayout game_classified_layout = convertView.findViewById(R.id.game_classified_layout);
//        if(position == 0){
//            game_classified_layout.setVisibility(View.VISIBLE);
//        }else {
//            game_classified_layout.setVisibility(View.GONE);
//        }
        if(gameType.getSecondTypesList() != null && gameType.getSecondTypesList().size() > 0){
            for(int i=0;i<gameType.getSecondTypesList().size();i++){
                GameTypeOuterClass.GameType gameTypeSecond = gameType.getSecondTypesList().get(i);
                switch (i){
                    case 0:
                        setTextView(mContext,gameType,one_tv, i);
                        one_tv.setOnClickListener(new Onclick(gameTypeSecond));
                        break;
                    case 1:
                        setTextView(mContext,gameType,two_tv, i);
                        two_tv.setOnClickListener(new Onclick(gameTypeSecond));
                        break;
                    case 2:
                        setTextView(mContext,gameType,three_tv, i);
                        three_tv.setOnClickListener(new Onclick(gameTypeSecond));
                        break;
                    case 3:
                        setTextView(mContext,gameType,four_tv, i);
                        four_tv.setOnClickListener(new Onclick(gameTypeSecond));
                        break;
                    case 4:
                        setTextView(mContext,gameType,five_tv, i);
                        five_tv.setOnClickListener(new Onclick(gameTypeSecond));
                        break;
                    case 5:
                        setTextView(mContext,gameType,six_tv, i);
                        six_tv.setOnClickListener(new Onclick(gameTypeSecond));
                        break;
                }
            }
        }
        icon_tv.setVisibility(View.VISIBLE);
        icon_tv.setText(gameType.getTypeName());
        switch (gameType.getTypeName()){
            case "冒险":
                icon_tv.setTextColor(0xff7dc6fb);
                iconIv.setImageResource(R.drawable.fen_lei_mao_xian);
                break;
            case "角色":
                icon_tv.setTextColor(0xfffcc954);
                iconIv.setImageResource(R.drawable.fen_lei_jiao_se);
                break;
            case "策略":
                icon_tv.setTextColor(0xfff38096);
                iconIv.setImageResource(R.drawable.fen_lei_ce_nue);
                break;
            case "射击":
                icon_tv.setTextColor(0xff59d4be);
                iconIv.setImageResource(R.drawable.fen_lei_she_ji);
                break;
            case "传奇":
                icon_tv.setTextColor(0xff59defd);
                iconIv.setImageResource(R.drawable.fen_lei_chuan_qi);
                break;
            case "动作":
                icon_tv.setTextColor(0xff8892fd);
                iconIv.setImageResource(R.drawable.fen_lei_dong_zuo);
                break;
        }
        iconIv.setOnClickListener(new Onclick(gameType));
//        Glide.with(mContext)
//                .load(gameType.getIcon()+"")
//                .apply(new RequestOptions().error(android.R.drawable.stat_notify_error))
//                .into(iconIv);
        return convertView;
    }

    private void setTextView(Context context, GameTypeOuterClass.GameType o, TextView v, int i){
        if(!TextUtils.isEmpty(o.getSecondTypesList().get(i).getTypeName())){
//            v.setTextColor(android.R.color.black);
            v.setText(o.getSecondTypesList().get(i).getTypeName()+"");
        }

    }

    class Onclick implements View.OnClickListener{
        GameTypeOuterClass.GameType gameTypes;
        public Onclick(GameTypeOuterClass.GameType gameType) {
            this.gameTypes = gameType;
        }

        @Override
        public void onClick(View v) {
            if(!TextUtils.isEmpty(gameTypes.getTypeName()))
                startToActivity(gameTypes, mContext);
        }
    }


//    public void toBindTopviewData(List<GameTypeSpecialOuterClass.GameTypeSpecial> listGameTS){
//        this.listGameTS = listGameTS;
//    }
//
//    /**
//     * 顶部 gridview
//     * @return
//     */
//    private View createTopGridView(){
//
//        View view = null;
//        GridviewHolder holder = null;
//        if( holder == null){
//            view = mInflater.inflate(R.layout.classification_gridview_item, null);
//            holder = new GridviewHolder(view);
//        }else{
//            holder = (GridviewHolder) view.getTag();
//        }
//
//        holder.adapter.toBindTopviewData(listGameTS);
//        holder.adapter.notifyDataSetChanged();
//        return view;
//    }
//
//    /**
//     * 创建图片容器
//     * @return
//     */
//    @SuppressLint("ResourceType")
//    public View createTopView() {
//        LinearLayout linearLayout = new LinearLayout(mContext);
//        if(listGameTS == null || listGameTS.size()<8){
//            return linearLayout;
//        }
//        linearLayout.setOrientation(LinearLayout.VERTICAL);
//        // 获取屏幕宽度
//        int W = G.WIDTH <G.HEIGHT ? G.WIDTH : G.HEIGHT;
//        int cricleRadius = W / 2 ;
//        Log.e("-------------", cricleRadius+"");
//        // 根据每行个数设置布局大小
//        linearLayout.setPadding(5, 5, 5, 5);
//        linearLayout.setGravity(Gravity.CENTER);
//        // 设置图片大小
//        linearLayout.setLayoutParams(new AbsListView.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT , LinearLayout.LayoutParams.WRAP_CONTENT));
//        /*--------------------------big img----------------------------------*/
//        LinearLayout bigLinearLayout = new LinearLayout(mContext);
//        bigLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
//        bigLinearLayout.setGravity(Gravity.CENTER);
//        // 设置图片大小
//        bigLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT , LinearLayout.LayoutParams.WRAP_CONTENT));
//        bigLinearLayout.setId(0);
//
//        ImageView bigCircleImageView = new ImageView(mContext);
//        bigCircleImageView.setLayoutParams(new LinearLayout.LayoutParams(cricleRadius, cricleRadius));
////        Bitmap map = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.classification_tengxun);
////        bigCircleImageView.setImageBitmap(map);
//        Glide.with(mContext)
//                .load(listGameTS.get(0).getIcon())
//                .error(android.R.drawable.stat_notify_error)
//                .into(bigCircleImageView);
//        bigCircleImageView.setId(1);
//        bigCircleImageView.setPadding(0,3,0,3);
//
//        bigCircleImageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startToActivity(listGameTS.get(0), mContext);
//            }
//        });
//        bigLinearLayout.addView(bigCircleImageView);
//
//        RelativeLayout bigLinearLayout2 = new RelativeLayout(mContext);
//        bigLinearLayout2.setPadding(5, 5, 5, 5);
//        bigLinearLayout2.setLayoutParams(new LinearLayout.LayoutParams(cricleRadius , cricleRadius));
//        bigLinearLayout2.setId(2);
//
//        ImageView ircleImageView1 = new ImageView(mContext);
//        RelativeLayout.LayoutParams imageView1Params = new RelativeLayout.LayoutParams(cricleRadius/2-5,cricleRadius/2-5);
//        ircleImageView1.setLayoutParams(imageView1Params);
//        Glide.with(mContext)
//                .load(listGameTS.get(1).getIcon())
//                .error(android.R.drawable.stat_notify_error)
//                .into(ircleImageView1);
//        ircleImageView1.setId(21);
//        ircleImageView1.setPadding(0,0,2,0);
//        ircleImageView1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startToActivity(listGameTS.get(1), mContext);
//            }
//        });
//
//        ImageView ircleImageView2 = new ImageView(mContext);
//        RelativeLayout.LayoutParams imageView1Params2 = new RelativeLayout.LayoutParams(cricleRadius/2-5,cricleRadius/2-5);
//        imageView1Params2.addRule(RelativeLayout.RIGHT_OF, ircleImageView1.getId());
//        ircleImageView2.setLayoutParams(imageView1Params2);
//        Glide.with(mContext)
//                .load(listGameTS.get(2).getIcon())
//                .error(android.R.drawable.stat_notify_error)
//                .into(ircleImageView2);
//        ircleImageView2.setId(22);
//        ircleImageView2.setPadding(3,0,0,0);
//        ircleImageView2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startToActivity(listGameTS.get(2), mContext);
//            }
//        });
//
//        ImageView ircleImageView3 = new ImageView(mContext);
//        RelativeLayout.LayoutParams imageView1Params3 = new RelativeLayout.LayoutParams(cricleRadius/2-5,cricleRadius/2-5);
//        imageView1Params3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
//        ircleImageView3.setLayoutParams(imageView1Params3);
//
//        Glide.with(mContext)
//                .load(listGameTS.get(3).getIcon())
//                .error(android.R.drawable.stat_notify_error)
//                .into(ircleImageView3);
//        ircleImageView3.setId(23);
//        ircleImageView3.setPadding(0,5,0,0);
//        ircleImageView3.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startToActivity(listGameTS.get(3), mContext);
//            }
//        });
//
//        ImageView ircleImageView4 = new ImageView(mContext);
//        RelativeLayout.LayoutParams imageView1Params4 = new RelativeLayout.LayoutParams(cricleRadius/2-5,cricleRadius/2-5);
//        imageView1Params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
//        imageView1Params4.addRule(RelativeLayout.RIGHT_OF, ircleImageView3.getId());
//        ircleImageView4.setLayoutParams(imageView1Params4);
////        Bitmap map4 = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.classification_erciyuan);
////        ircleImageView4.setImageBitmap(map4);
//        Glide.with(mContext)
//                .load(listGameTS.get(4).getIcon())
//                .error(android.R.drawable.stat_notify_error)
//                .into(ircleImageView4);
//        ircleImageView4.setId(24);
//        ircleImageView4.setPadding(5, 5, 0, 0);
//        ircleImageView4.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startToActivity(listGameTS.get(4), mContext);
//            }
//        });
//
//        bigLinearLayout2.addView(ircleImageView1);
//        bigLinearLayout2.addView(ircleImageView2);
//        bigLinearLayout2.addView(ircleImageView3);
//        bigLinearLayout2.addView(ircleImageView4);
//
//        bigLinearLayout.addView(bigLinearLayout2);
//
//        linearLayout.addView(bigLinearLayout);
//        /*------------------------------------------------------------*/
//
//        /*--------------------------bottom img----------------------------------*/
//
//        LinearLayout bottomLinearLayout = new LinearLayout(mContext);
//        bottomLinearLayout.setPadding(5, 5, 5, 5);
//        bottomLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
//        bottomLinearLayout.setGravity(Gravity.CENTER);
//        // 设置图片大小
//        bottomLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT , LinearLayout.LayoutParams.WRAP_CONTENT));
//        bottomLinearLayout.setId(3);
//
//        bottomLinearLayout.addView(createImageView(R.drawable.classification_chijizhuanq, cricleRadius/2-5, listGameTS.get(5)));
//        bottomLinearLayout.addView(createImageView(R.drawable.classification_wangyizhuanqu, cricleRadius/2-5, listGameTS.get(6)));
//        bottomLinearLayout.addView(createImageView(R.drawable.classification_xianxia, cricleRadius/2-5, listGameTS.get(7)));
//        bottomLinearLayout.addView(createImageView(R.drawable.classification_gengduoyouxo, cricleRadius/2-5, null));
//
//        linearLayout.addView(bottomLinearLayout);
//        /*--------------------------bottom img----------------------------------*/
//
//        return linearLayout;
//    }

    /**
     * 创建imageView
     * @return
     */
    private View createImageView(int mipmap, final int cricleRadius, final Object o){
        ImageView bottomImageView1 = new ImageView(mContext);
        LinearLayout.LayoutParams bottomImgParams1 = new LinearLayout.LayoutParams(cricleRadius,cricleRadius);
        bottomImageView1.setLayoutParams(bottomImgParams1);
        if(o == null){
            Bitmap  bottomMap1 = BitmapFactory.decodeResource(mContext.getResources(), mipmap);
            bottomImageView1.setImageBitmap(bottomMap1);
        }else {
            Glide.with(mContext)
                    .load(((GameTypeSpecialOuterClass.GameTypeSpecial) o).getIcon()+"")
                    .apply(new RequestOptions().error(android.R.drawable.stat_notify_error))
                    .into(bottomImageView1);
        }

        bottomImageView1.setPadding(2,2,2,2);
        bottomImageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(o == null){
                    SkipUtils.getInstance().goGameType(mContext,0);
                }else{
                    startToActivity(o, mContext);
                }


            }
        });
//        bottomImageView1.setId(31);
        return bottomImageView1;
    }
    /**
     * t跳转到游戏列表页面
     * @param o
     * @param context
     */
    private void startToActivity(Object o, Context context){
        if(o == null){
            return;
        }
        int type = 0;
        if(o instanceof GameTypeSpecialOuterClass.GameTypeSpecial){//top
            type = ((GameTypeSpecialOuterClass.GameTypeSpecial) o).getGameTypeId();
        }else if(o instanceof GameTypeOuterClass.GameType){
            type = ((GameTypeOuterClass.GameType) o).getId();
        }
        SkipUtils.getInstance().goGameType(context,type);
    }

    /**
     *
     */
    private AbsListView.LayoutParams returnLayoutparas(int w, int h){
        return new AbsListView.LayoutParams(w, h);
    }

//    /**
//     * gridview holder
//     */
//    class GridviewHolder{
//        MyGridview gridview;
//        ClassificationGridviewAdapter adapter;
//
//        public GridviewHolder(View view) {
//            this.gridview = view.findViewById(R.id.classification_gridview);
//            this.adapter = new ClassificationGridviewAdapter( listGameTS, mContext);
//            gridview.setAdapter(adapter);
////            gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
//            view.setTag(this);
//        }
//    }

    /**
     * 获取图片的颜色
     */
   /* private void getImgColor(Bitmap bitmap , final View view){
        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                Palette.Swatch s1, s2, s3 ,s4, s5, s6 ,s7 ;
                s1 = palette.getVibrantSwatch();
                s2 = palette.getDarkVibrantSwatch();
                s3 = palette.getLightVibrantSwatch();
                s4 = palette.getMutedSwatch();
                s5 = palette.getDarkMutedSwatch();
                s6 = palette.getLightMutedSwatch();
                s7 = palette.getDominantSwatch();
                if(s1 != null){
                    ((TextView)view).setTextColor(s1.getRgb());
                    Log.e("--color",s1.getRgb()+","+s1.getBodyTextColor()+","+s1.getTitleTextColor());
                }else if(s2 != null){
                    ((TextView)view).setTextColor(s2.getRgb());
                    Log.e("--color",s2.getRgb()+","+s2.getBodyTextColor()+","+s2.getTitleTextColor());
                }else if(s3 != null){
                    ((TextView)view).setTextColor(s3.getRgb());
                    Log.e("--color",s3.getRgb()+","+s3.getBodyTextColor()+","+s3.getTitleTextColor());
                }else if(s4 != null){
                    ((TextView)view).setTextColor(s4.getRgb());
                    Log.e("--color",s4.getRgb()+","+s4.getBodyTextColor()+","+s4.getTitleTextColor());
                }else if(s5 != null){
                    ((TextView)view).setTextColor(s5.getRgb());
                    Log.e("--color",s5.getRgb()+","+s5.getBodyTextColor()+","+s5.getTitleTextColor());
                }else if(s6 != null){
                    ((TextView)view).setTextColor(s6.getRgb());
                    Log.e("--color",s6.getRgb()+","+s6.getBodyTextColor()+","+s6.getTitleTextColor());
                }else if(s7 != null){
                    ((TextView)view).setTextColor(s1.getRgb());
                    Log.e("--color",s7.getRgb()+","+s7.getBodyTextColor()+","+s7.getTitleTextColor());
                }

//                if(palette.getSwatches() != null){
//                    for(Palette.Swatch s : palette.getSwatches()){
//                        if(s != null){
//                            Log.e("s--color",s.getRgb()+","+s.getBodyTextColor()+","+s.getTitleTextColor());
//                        }
//                    }
//                }
            }
        });
    }*/
}
