package com.kfzs.duanduan.adp;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.kfzs.duanduan.proto.GameOuterClass;
import com.kfzs.duanduan.utils.DiscountUtils;
import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.kfzs.duanduan.utils.dlg.DownBtnUtils;
import com.kfzs.duanduan.utils.dlg.SkipUtils;
import com.kfzs.duanduan.utils.dlg.ViewFindUtils;
import com.kfzs.duanduan.view.KFProgressButton;
import com.sheep.jiuyan.samllsheep.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  AdpSearchGame</p>
 * @ <p>Description:</p>
 * @ date:  2017/6/22 16:14
 * @ QQ:    315096953
 */

public class AdpSearchGame extends BaseAdpterWithDownBtn {

    private List<GameOuterClass.Game> mList = new ArrayList<>();

    public AdpSearchGame(Context context) {
        this.mContext = context;
    }

    public void addAll(List<GameOuterClass.Game> list) {
        mList.addAll(list);
    }

    public void clear() {
        mList.clear();
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.listview_item_game_info, null);
            convertView.setOnClickListener(cls_GoDetail);
        }
        DiscountUtils.getInstance().showDiscount(convertView,
                getItem(position).getIconImage(),
                getItem(position).getDiscount());

//        Glide.with(mContext).load(getItem(position).getIconImage())
//                .into(ViewFindUtils.find(convertView, R.id.img_logo, ImageView.class));

        ViewFindUtils.find(convertView, R.id.img_listview_item_game_info)
                .setVisibility(getItem(position).getExistGift() == 1 ? View.VISIBLE : View.GONE);
        ViewFindUtils.find(convertView, R.id.txt_list_item_game_info_title, TextView.class)
                .setText(getItem(position).getGameName());
        ViewFindUtils.find(convertView, R.id.txt_list_item_game_info_type, TextView.class)
                .setText(getItem(position).getTypeText());

        String downloadCnt = String.format(mContext.getString(R.string.download_count),
                getItem(position).getDownloadNum() + "");
        ViewFindUtils.find(convertView, R.id.txt_list_item_game_info_down_count, TextView.class)
                .setText(downloadCnt + "");
        ViewFindUtils.find(convertView, R.id.ratbar_list_item_game_info, RatingBar.class)
                .setRating(NumberFormatUtils.getDivideResult(getItem(position).getStar()));

        //更新跳转位置
        convertView.setTag(TAG_ID, getItem(position).getId());

        //下载按钮的功能处理
        KFProgressButton btn = ViewFindUtils.hold(convertView, R.id.btn_list_item_game_info);
        if (getItem(position).getIsInBook() == 1) {
            if (getItem(position).getIsCanPreDown()) {
                btn.setText(mContext.getString(R.string.predown));
                btn.setOnClickListener(null);
                DownBtnUtils.addDownloadBtnListener(btn, mContext,
                        getmInstallButtonMgrMap(), getItem(position),
                        "search_game" + position);
            } else {
                btn.setText(mContext.getString(R.string.tab_game_order));
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        goBookedGame(getItem(position));
                    }
                });
            }
        } else {
            btn.setText(mContext.getString(R.string.install));
            btn.setOnClickListener(null);
            DownBtnUtils.addDownloadBtnListener(btn, mContext,
                    getmInstallButtonMgrMap(), getItem(position),
                    "search_game" + position);
        }
        return convertView;
    }


    /**
     * 跳到预约游戏
     *
     * @param game
     */
    private void goBookedGame(GameOuterClass.Game game) {
        SkipUtils.getInstance().goGameDetails(mContext, game.getId());
//        Intent intent = new Intent(mContext, ActWeb.class);
//
//        intent.putExtra(KFIntentKeys.EXTRA_BEHAIOR, ActWeb.BEHAVIOR_GAME_BOOK);
//        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_SHARE_IMGURL, game.getIconImage());
//        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_GAME_BOOK_ID, String.valueOf(game.getBookId()));
//        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_GAME_ID, String.valueOf(game.getId()));
//        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_PACKAGENAME, game.getPackageName());
//        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_GAME_VERSION, game.getVersion());
//        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_GAME_SIZE, game.getSize());
//
//        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_URL, game.getUrl());
//        intent.putExtra(KFIntentKeys.EXTRA_WEBVIEW_TITLE, game.getGameName());
//        mContext.startActivity(intent);
    }
}
