package com.sheep.gamegroup.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.DialogConfig;
import com.sheep.gamegroup.model.entity.Applications;
import com.sheep.gamegroup.model.entity.GiftBag;
import com.sheep.gamegroup.model.entity.GiftBagApp;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.service.DownloadService;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.List;
import java.util.Locale;

import io.reactivex.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import io.reactivex.schedulers.Schedulers;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.GIFT_BAG_COPY_CODE;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.GIFT_BAG_LOOK_TIP;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.GIFT_BAG_RECEIVE;
import static com.sheep.gamegroup.view.adapter.WelfareAdapter.RECEIVE_FAILED;
import static com.sheep.gamegroup.view.adapter.WelfareAdapter.TO_REFRESH;

/**
 * Created by realicing on 2018/8/28.
 * realicing@sina.com
 */
public class GiftCenterAdapter extends AdbCommonRecycler<GiftBagApp> {

    private Action1<Integer> action1;
    public GiftCenterAdapter(Context context, List<GiftBagApp> list, Action1<Integer> action1) {
        super(context, list);
        this.action1 = action1;
    }

    @Override
    public int getViewIdByType(int type) {
        return R.layout.item_download_welfare;
    }

    @Override
    public void convert(ViewHolder holder, final GiftBagApp item) {
        ImageView item_download_welfare_iv = (ImageView)holder.getView(R.id.item_download_welfare_iv);
        ImageView item_download_welfare_tip_iv = (ImageView)holder.getView(R.id.item_download_welfare_tip_iv);
        ImageView item_download_welfare_iv2 = (ImageView)holder.getView(R.id.item_download_welfare_iv2);
        TextView item_download_welfare_name_tv = (TextView)holder.getView(R.id.item_download_welfare_name_tv);
        TextView item_download_welfare_money = (TextView)holder.getView(R.id.item_download_welfare_money);
        TextView item_download_welfare_num_tv = (TextView)holder.getView(R.id.item_download_welfare_num_tv);
        TextView item_download_welfare_num_tv1 = (TextView)holder.getView(R.id.item_download_welfare_num_tv1);
        TextView item_download_welfare_date_tv = (TextView)holder.getView(R.id.item_download_welfare_date_tv);
        TextView item_download_welfare_btn_bottom = (TextView)holder.getView(R.id.item_download_welfare_btn_bottom);
        TextView item_download_welfare_btn_top = (TextView)holder.getView(R.id.item_download_welfare_btn_top);
        TextView item_download_welfare_btn_center = (TextView)holder.getView(R.id.item_download_welfare_btn_center);
        View item_download_welfare_line = holder.getView(R.id.item_download_welfare_line);
        item_download_welfare_line.setVisibility(holder.getAdapterPosition() + 1 == getItemCount() ? View.GONE : View.VISIBLE);
        item_download_welfare_money.setVisibility(View.GONE);
        item_download_welfare_btn_bottom.setVisibility(View.GONE);
        item_download_welfare_btn_top.setVisibility(View.GONE);
        item_download_welfare_num_tv1.setVisibility(View.GONE);
        item_download_welfare_iv2.setVisibility(View.GONE);
        item_download_welfare_btn_center.setVisibility(View.VISIBLE);
        if(item == null){
            ViewUtil.setDefaultText(item_download_welfare_name_tv);
            ViewUtil.setDefaultText(item_download_welfare_num_tv);
            ViewUtil.setDefaultText(item_download_welfare_date_tv);
            item_download_welfare_iv2.setVisibility(View.GONE);
            item_download_welfare_tip_iv.setVisibility(View.GONE);
        } else if(context instanceof Activity){
            final Applications itemApp = item.getApp();
            final GiftBag itemGiftBag = item.getGift_bag();
            if(itemApp == null || itemGiftBag == null){
                return;
            }
            GlideImageLoader.setGameImage(item_download_welfare_iv, itemApp.getIcon());
            ViewUtil.setText(item_download_welfare_name_tv, itemGiftBag.getGiftName());
            ViewUtil.setText(item_download_welfare_date_tv, itemGiftBag.getDateText());
            if(TextUtils.isEmpty(item.getCode())){//没有code，就视为没有领取礼包
                item_download_welfare_tip_iv.setVisibility(View.GONE);
                ViewUtil.setText(item_download_welfare_num_tv, itemGiftBag.getLastNumText());
                item_download_welfare_btn_center.setText("领取");
                item_download_welfare_btn_center.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        receiveGiftBag((Activity) context, item, action1);
                    }
                });
            } else {
                item_download_welfare_tip_iv.setVisibility(View.VISIBLE);
                ViewUtil.setH5Text(item_download_welfare_num_tv, String.format(Locale.CHINA, "兑换码：<font color='#33CCFF'>%s</font>", item.getCode()));
                item_download_welfare_btn_center.setText("复制");
                item_download_welfare_btn_center.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        GIFT_BAG_COPY_CODE.onEvent("gift_bag_id", item.getGift_bag().getId(), "gift_bag_code", item.getCode());
                        StringUtils.CopyText(item.getCode());
                        G.showToast(R.string.success_copy_code);
                    }
                });
                item_download_welfare_tip_iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        GIFT_BAG_LOOK_TIP.onEvent("gift_bag_id", item.getGift_bag().getId());
                        ViewUtil.showMsgDialog(context, new DialogConfig().setTitle("礼包使用说明").setBtnLeftText("我知道了")
                                .setMsgGravity(Gravity.START)
                                .setMsg(String.format(Locale.CHINA, "礼包内容：\n%s\n\n使用方式：\n%s", itemGiftBag.getGiftContent(), itemGiftBag.getUseMethod())));
                    }
                });
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //点击礼包项
                    Jump2View.getInstance().goGiftDetail(context, item);
                }
            });
            DownloadService.setDownLoadLongClick(holder.itemView, item.getApp());
        }
    }

    /**
     * 领取礼包
     * @param activity 为空时，不显示领取成功对话框
     * @param item
     * @param action1
     */
    public static void receiveGiftBag(final Activity activity, final GiftBagApp item, final Action1<Integer> action1) {
        int gift_bag_id = item.getGift_bag().getId();
        GIFT_BAG_RECEIVE.onEvent("gift_bag_id", gift_bag_id);
        SheepApp.getInstance().getNetComponent().getApiService().receiveGiftBag(gift_bag_id)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                            @Override
                            public void onNext(BaseMessage baseMessage) {
                                String code = baseMessage.getData(String.class);
                                item.setReceived(true);
                                item.setCode(code);
                                if(action1 != null)
                                    action1.call(TO_REFRESH);
                                if(activity != null)
                                    ViewUtil.showGetWelfareDialog(activity, item);
                            }

                            @Override
                            public void onError(BaseMessage baseMessage) {
                                if(action1 != null)
                                    action1.call(RECEIVE_FAILED);
                                G.showToast(baseMessage);
                            }
                        });
    }
}
