package com.sheep.gamegroup.view.adapter;

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

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.util.DownloadUtil;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.TaskEty;
import com.sheep.gamegroup.model.entity.Release_task;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DeviceUtil;
import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.BuildConfig;
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 io.reactivex.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import io.reactivex.schedulers.Schedulers;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.RECEIVE_AWARD_GAME_TASK;
import static com.sheep.gamegroup.view.adapter.TryMakeMoneyAdp.PUBLIC_TAG_PREFIX_TEXTVIEW_LIST;

/**
 * Created by realicing on 2018/8/28.
 * realicing@sina.com
 */
public class WelfareAdapter extends AdbCommonRecycler<Release_task>{
    private Action1<Integer> action1;
    public WelfareAdapter(Context context, List<Release_task> list, Action1<Integer> action1) {
        super(context, list);
        this.action1 = action1;
    }
    public static final int TO_REFRESH = 1;
    public static final int RECEIVE_FAILED = -1;//领取失败
    @Override
    public int getViewIdByType(int type) {
        return R.layout.item_download_welfare;
    }

    @Override
    public void convert(ViewHolder holder, final Release_task item) {
        ImageView item_download_welfare_iv = (ImageView)holder.getView(R.id.item_download_welfare_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);
        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_num_tv1.setVisibility(View.GONE);
        if(item == null){
            ViewUtil.setDefaultText(item_download_welfare_name_tv);
            ViewUtil.setDefaultText(item_download_welfare_num_tv);
            ViewUtil.setDefaultText(item_download_welfare_date_tv);
            ViewUtil.setDefaultText(item_download_welfare_money);
            item_download_welfare_iv2.setVisibility(View.GONE);
        } else if(context instanceof Activity){
            final TaskEty itemTask = item.getTask();
            if(itemTask == null){
                return;
            }
            GlideImageLoader.setGameImage(item_download_welfare_iv, itemTask.getIcon());
            ViewUtil.setText(item_download_welfare_name_tv, item.getName());
            ViewUtil.setH5Text(item_download_welfare_money, item.getH5BonusText());
//            ViewUtil.setText(item_download_welfare_num_tv, item.getGiftRatioText());
            ViewUtil.setText(item_download_welfare_date_tv, item.getDateText());
            item_download_welfare_iv2.setVisibility(item.isTaskFinished()? View.VISIBLE : View.INVISIBLE);

            item_download_welfare_btn_bottom.setTag(PUBLIC_TAG_PREFIX_TEXTVIEW_LIST + itemTask.getDownload_link());
            switch (item.getUser_task_status()){//1,已接受任务 2,正在进行中,3完成任务,4放弃任务,5任务失败,6审核失败,7提交审核,8任务已下线,9至少完成了一个任务了
                case 0:
                    ViewUtil.setText(item_download_welfare_num_tv, item.getLastNumText());
                    item_download_welfare_money.setVisibility(View.VISIBLE);
                    item_download_welfare_btn_top.setVisibility(View.INVISIBLE);
                    item_download_welfare_btn_bottom.setEnabled(true);
                    item_download_welfare_btn_bottom.setText("下载领取");
                    item_download_welfare_btn_bottom.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            acceptedTask(item.getId());
                        }
                    });
                    break;
                case 1:
                case 2:
                    ViewUtil.setText(item_download_welfare_num_tv, item.getLastNumText());
                    item_download_welfare_money.setVisibility(View.VISIBLE);
                    item_download_welfare_btn_top.setVisibility(View.INVISIBLE);
                    item_download_welfare_btn_bottom.setEnabled(true);
                    item.getDownloadHelper().updateDownloadTaskView((Activity) context, itemTask, item_download_welfare_btn_bottom);
                    if(item.getDownloadHelper().getDownLoadType() == DownloadUtil.STATUS_INSTALLED){//已经安装的话
                        item_download_welfare_btn_bottom.setText("领取奖励");
                        item_download_welfare_btn_bottom.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if (itemTask.isGameReservationCantDownload()) {
                                    CommonUtil.getInstance().reservationGameTask(context, item, new Action1<Integer>() {
                                        @Override
                                        public void call(Integer integer) {
                                            if(action1 != null)
                                                action1.call(TO_REFRESH);
                                        }
                                    });
                                } else {
                                    acceptedReceiveAward(item);
                                }
                            }
                        });
                    }
                    break;
                case 3:
                    ViewUtil.setH5Text(item_download_welfare_num_tv, item.getH5BonusText());
                    item_download_welfare_money.setVisibility(View.INVISIBLE);
                    item_download_welfare_btn_top.setVisibility(View.VISIBLE);
                    item.getDownloadHelper().updateDownloadTaskView((Activity) context, itemTask, item_download_welfare_btn_bottom);
                    if(item.getDownloadHelper().getDownLoadType() == DownloadUtil.STATUS_INSTALLED) {//已经安装的话
                        item_download_welfare_btn_bottom.setText("启动");
                    }
                    item_download_welfare_btn_top.setText("充值");
                    item_download_welfare_btn_top.setEnabled(true);
                    item_download_welfare_btn_top.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (itemTask.getThird_task_id() != null) {
                                ViewUtil.showGamePayAccount((Activity) context, itemTask);
                            }
                        }
                    });
                    break;
                default:
                    ViewUtil.setText(item_download_welfare_num_tv, item.getLastNumText());
                    item_download_welfare_money.setVisibility(View.VISIBLE);
                    item_download_welfare_btn_top.setVisibility(View.INVISIBLE);
                    item_download_welfare_btn_bottom.setText("已经下线");
                    item_download_welfare_btn_bottom.setEnabled(false);
                    break;
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Jump2View.getInstance().goGameTaskDetailView(context,  item.getId(), itemTask.getThird_task_id(),1);
                }
            });
            DownloadService.setDownLoadLongClick(holder.itemView, item.getTask());
        }
    }

    /**
     * 领取任务
     */
    private void acceptedTask(int release_task_id) {
        String device_id = DeviceUtil.getDeviceId(SheepApp.getInstance());
        UMConfigUtils.Event.TASK_ACCEPT.onEvent("device_id", device_id, "release_task_id", release_task_id);
        JSONObject object = new JSONObject();
        object.put("device_id", device_id);
        object.put("release_task_id", release_task_id);
        object.put("create_time", System.currentTimeMillis() / 1000L);//这里添加时间戳，防止用户抓包后重复使用
        object.put("app_version", String.valueOf(BuildConfig.VERSION_CODE));
        SheepApp.getInstance()
                .getNetComponent()
                .getApiService()
                .acceptedTask(object)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        G.showToast(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {//领取任务成功
                        if(action1 != null)
                            action1.call(TO_REFRESH);
                    }
                });
    }
    /**
     * 领取奖励
     */
    private void acceptedReceiveAward(Release_task item) {
        RECEIVE_AWARD_GAME_TASK.onEvent();
        SheepApp.getInstance()
                .getNetComponent()
                .getApiService()
                .receive_award(item.getAccepted_task_id())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        G.showToast(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        //这里领取了奖励，要刷新用户信息
                        CommonUtil.getInstance().updateUserInfo(null);
                        if(action1 != null)
                            action1.call(TO_REFRESH);
                    }
                });
    }
}
