package com.kfzs.duanduan.adp;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;

import com.bumptech.glide.Glide;
import com.kfzs.duanduan.utils.DiscountUtils;
import com.kfzs.duanduan.view.KFProgressButton;
import com.kfzs.appstore.utils.adapter.recyclerview.ViewHolder;
import com.sheep.jiuyan.samllsheep.R;
import com.kfzs.duanduan.utils.InstallButtonUtils;
import com.kfzs.duanduan.bean.RecyleObj;
import com.kfzs.duanduan.bean.RecyleType;
import com.kfzs.duanduan.proto.GameOuterClass;
import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.kfzs.duanduan.utils.dlg.DownBtnUtils;
import com.kfzs.duanduan.utils.dlg.SkipUtils;
import com.kfzs.duanduan.view.FilterWindow;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  AdpFragmentRank</p>
 * @ <p>Description: 首页排行页面的Recycle适配器</p>
 * @ date:  2017/7/12 14:30
 * @ QQ:    315096953
 */

public class AdpFragmentRank extends AdpCommonRecy<RecyleObj> {


    private final String TAG = this.getClass().getName();
    private final int TAG_ID = R.id.btn_list_item_game_info;
    private Map<String, InstallButtonUtils> mDownLoadItemLogicMgrMap = new HashMap<>();
    private Context mContext;
    private RecyclerView mRecyView;
    private FilterWindow filterWindow;
    private Button btnPanel;
    private View.OnClickListener mTypeClickListener;

    private int[] mIconId = {R.id.game_rank_top3_icon1, R.id.game_rank_top3_icon2, R.id.game_rank_top3_icon3};
    private int[] mNameId = {R.id.game_rank_top3_name1, R.id.game_rank_top3_name2, R.id.game_rank_top3_name3};
    private int[] mBtnId = {R.id.game_rank_top3_install1, R.id.game_rank_top3_install2, R.id.game_rank_top3_install3};
    private int[] mLayoutId = {R.id.game_rank_top3_content1, R.id.game_rank_top3_content2, R.id.game_rank_top3_content3};
    private int[] mGiftId = {R.id.img_listview_item_game_rank_top1, R.id.img_listview_item_game_rank_top2, R.id.img_listview_item_game_rank_top3};

    public AdpFragmentRank(Context context, RecyclerView recyclerView, View.OnClickListener clickListener) {
        super(context);
        mContext = context;
        mRecyView = recyclerView;
        mTypeClickListener = clickListener;
    }

    @Override
    public int getViewIdByType(int viewType) {
        int resId = 0;
        RecyleType recyleType = RecyleType.values()[viewType];
        switch (recyleType) {
            case RANK_TYPE:
                resId = R.layout.listview_item_filter_panel;
                break;
            case RANK_TOP3:
                resId = R.layout.listview_item_game_rank_top3;
                break;
            case RANK_LIST:
                resId = R.layout.listview_item_game_info;
        }
        return resId;
    }

    /**
     * 返回下载管理年HashMap
     *
     * @return
     */
    public Map<String, InstallButtonUtils> getmDownLoadItemLogicMgrMap() {
        return mDownLoadItemLogicMgrMap;
    }

    /**
     * 这一步不能少，不然会类型不匹配
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return getmListDatas().get(position).getRecyleType().ordinal();
    }

    @Override
    public void convert(ViewHolder holder, RecyleObj recyleObj) {
        switch (recyleObj.getRecyleType()) {
            case RANK_TYPE:
                bindFilterPanel(holder);
                break;
            case RANK_TOP3:
                bindTop3(holder, (List<GameOuterClass.Game>) recyleObj.getDataObj());
                break;
            case RANK_LIST:
                bindList(holder, (GameOuterClass.Game) recyleObj.getDataObj(), holder.getAdapterPosition());
                break;
        }
    }


    /**
     * 绑定排行页的分类选择View
     *
     * @param helper
     */
    private void bindFilterPanel(final ViewHolder helper) {
        btnPanel = helper.getView(R.id.btn_query_condition);
        btnPanel.setOnClickListener(mTypeClickListener);
    }


    /**
     * 绑定排行页的列表View
     *
     * @param helper
     * @param model
     */
    private void bindList(final ViewHolder helper, GameOuterClass.Game model, int position) {
        helper.setText(R.id.txt_list_item_game_info_title, model.getGameName());
        helper.setText(R.id.txt_list_item_game_info_type, model.getTypeText());

        helper.setText(R.id.txt_list_item_game_info_down_count,
                String.format(mContext.getString(R.string.download_count), model.getDownloadNum() + ""));

        helper.getView(R.id.ratbar_list_item_game_info, RatingBar.class)
                .setRating(NumberFormatUtils.getDivideResult(model.getStar()));
        DiscountUtils.getInstance().showDiscount(helper.itemView,
                model.getIconImage(),
                model.getDiscount());

//        Glide.with(mContext)
//                .load(model.getIconImage())
//                .transform(new KFGlideRoundTransform(mContext, 5))
//                .into((ImageView) helper.getView(R.id.img_list_item_game_info_logo));

        if (!helper.getConvertView().hasOnClickListeners()) {
            helper.getConvertView().setOnClickListener(clsListGoDetail);
        }
        helper.getConvertView().setTag(TAG_ID, model.getId());
        DownBtnUtils.addDownloadBtnListener(helper.getView(R.id.btn_list_item_game_info, KFProgressButton.class),
                mContext, mDownLoadItemLogicMgrMap, model, "rank_list" + position);
    }


    /**
     * 点击排行 ，跳游戏详情
     */
    private View.OnClickListener clsListGoDetail = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SkipUtils.getInstance().goGameDetails(mContext, (int) v.getTag(TAG_ID));
        }
    };

    /**
     * 绑定Top3的View
     *
     * @param helper
     * @param lResponseTopThreeList
     */
    private void bindTop3(final ViewHolder helper, final List<GameOuterClass.Game> lResponseTopThreeList) {
        for (int i = 0; i < lResponseTopThreeList.size(); i++) {
            final GameOuterClass.Game oTempGame = lResponseTopThreeList.get(i);
            if (i == 0) {
                ImageView ivChampion = helper.getView(R.id.game_rank_top3_1_bg);
                Glide.with(mContext)
                        .load(oTempGame.getDetailBackImage())
                        .into(ivChampion);
            }

            helper.setText(mNameId[i], oTempGame.getGameName());
            helper.setVisible(mGiftId[i], oTempGame.getExistGift() == 1);
            ImageView ivGameIcon = helper.getView(mIconId[i]);
            Glide.with(mContext)
                    .load(oTempGame.getIconImage())
                    .into(ivGameIcon);

            String position = String.valueOf((i + 1) * 12345);
            mDownLoadItemLogicMgrMap.put(oTempGame.getDownloadUrl(), InstallButtonUtils
                    .init(position, oTempGame.getDownloadUrl(), oTempGame.getPackageName(), helper.getView(mBtnId[i], KFProgressButton.class),
                            "bindTopThreePanel" + i));
            DownBtnUtils.addDownloadBtnListener(helper.getView(mBtnId[i], KFProgressButton.class),
                    mContext,mDownLoadItemLogicMgrMap,lResponseTopThreeList.get(i),TAG);
            helper.getView(mLayoutId[i]).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SkipUtils.getInstance().goGameDetails(mContext, oTempGame.getId());
                }
            });
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
    }
}
