package com.sheep.gamegroup.view.adapter;

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

import com.kfzs.appstore.utils.adapter.recyclerview.RecyclerViewAdapter;
import com.kfzs.appstore.utils.adapter.recyclerview.ViewHolder;
import com.sheep.gamegroup.model.entity.GameEntity;
import com.sheep.gamegroup.model.entity.GiftBag;
import com.sheep.gamegroup.model.entity.GiftBagApp;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by realicing on 2018/11/5.
 * realicing@sina.com
 */
public class AdpGcGameAppGiftBag extends RecyclerViewAdapter<GiftBagApp> {

    private GameEntity gameEntity;
    public AdpGcGameAppGiftBag(Context context, List<GiftBagApp> datas, GameEntity gameEntity) {
        super(context, R.layout.item_gift_bag, datas);
        this.gameEntity = gameEntity;
    }

    @Override
    public void convert(ViewHolder viewHolder, final GiftBagApp item, int position) {
        View item_gift_bag_line_top = viewHolder.itemView.findViewById(R.id.item_gift_bag_line_top);
        final TextView btn_tv = viewHolder.itemView.findViewById(R.id.item_gift_bag_btn);
        TextView item_gift_bag_num = viewHolder.itemView.findViewById(R.id.item_gift_bag_num);
        TextView item_gift_bag_name = viewHolder.itemView.findViewById(R.id.item_gift_bag_name);

        ViewUtil.setVisibility2(item_gift_bag_line_top, position != 0);

        GiftBag giftBag;
        if(item == null || (giftBag = item.getGift_bag()) == null){
            return;
        }
        ViewUtil.setText(item_gift_bag_name, giftBag.getGiftName());
        ViewUtil.setText(item_gift_bag_num, giftBag.getLastNumText());

        if (!item.isReceived()){
            btn_tv.setText("领取");
            btn_tv.setEnabled(true);
            btn_tv.setTextColor(SheepApp.getInstance().getResources().getColor(R.color.txt_bule));
            btn_tv.setBackgroundResource(R.drawable.shape_blue_stroke_rectangle);
            btn_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(CommonUtil.getInstance().isInstallApp(gameEntity.getApp().getPackage_name(), gameEntity.getApp().getName())){
                        CommonUtil.getInstance().receiveGiftById(ActivityManager.getInstance().currentActivity(), item, new Action1<Integer>() {
                            @Override
                            public void call(Integer integer) {
                                item.setReceived(true);
                                btn_tv.setText("已领取");
                                btn_tv.setEnabled(false);
                                notifyDataSetChanged();
                            }
                        },gameEntity.getApp().getPackage_name());
                    }

                }
            });
        }else {
            btn_tv.setText("已领取");
            btn_tv.setEnabled(false);
            btn_tv.setTextColor(SheepApp.getInstance().getResources().getColor(R.color.time_created));
            btn_tv.setBackgroundResource(R.drawable.shape_gray_stroke_rectangle_10_radius);
        }
    }

}
