package com.sheep.gamegroup.view.adapter;

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

import com.sheep.gamegroup.model.entity.RecyleObj;
import com.sheep.gamegroup.model.entity.RecyleType;
import com.sheep.gamegroup.model.entity.WXOnHookAccount;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.view.activity.LieMakeMoneyAct;
import com.sheep.gamegroup.view.dialog.DialogAccountAbnormal;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;

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

import static com.sheep.gamegroup.view.activity.CommitWxCodeAct.ALTER_CODE;
import static com.sheep.gamegroup.view.activity.CommitWxCodeAct.ALTER_INFO;
import static com.sheep.gamegroup.view.activity.CommitWxCodeAct.HAS_DEBLOCKED;
import static com.sheep.gamegroup.view.activity.LieMakeMoneyAct.STATE_ON_HOOK_ABNORMAL;
import static com.sheep.gamegroup.view.activity.LieMakeMoneyAct.STATE_ON_HOOK_BLOCKED;
import static com.sheep.gamegroup.view.activity.LieMakeMoneyAct.STATE_ON_HOOK_DEBLOCKED;
import static com.sheep.gamegroup.view.activity.LieMakeMoneyAct.STATE_ON_HOOK_NEED_VC;
import static com.sheep.gamegroup.view.activity.LieMakeMoneyAct.STATE_ON_HOOK_TOMORROW_RESET;
import static com.sheep.gamegroup.view.activity.LieMakeMoneyAct.STATE_ON_HOOK_VC_OVERTIME;
import static com.sheep.gamegroup.view.activity.LieMakeMoneyAct.STATE_ON_HOOK_VERIFIED;

/**
 * Created by realicing on 2018/3/23.
 * realicing@sina.com
 */
public class LieMakeMoneyAdp extends AdbCommonRecycler<RecyleObj> {

    public LieMakeMoneyAdp(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public int getViewIdByType(int type) {
        int resId = R.layout.empty;
        RecyleType recyleType = RecyleType.values()[type];
        switch (recyleType){
            case START_ON_HOOK:
                resId = R.layout.lie_make_money_start_item;
                break;
            case ABNORMAL_ON_HOOK:
                resId = R.layout.lie_make_money_abnormal_item;
                break;
            case NORMAL_ON_HOOK:
                resId = R.layout.lie_make_money_normal_item;
                break;
            case PROCESS_ON_HOOK:
                resId = R.layout.lie_make_money_processing_item;
                break;
            case PROMPT_ON_HOOK:
                resId = R.layout.lie_make_money_prompt_item;
                break;
        }
        return resId;
    }

    @Override
    public void convert(ViewHolder holder, RecyleObj recyleObj) {

        switch (recyleObj.getRecyleType()){
            case START_ON_HOOK:
                bindStartOnHook(holder);
                break;
            case ABNORMAL_ON_HOOK:
                bindAbnormalOnHook(holder, (WXOnHookAccount) recyleObj.getDataObjT());
                break;
            case NORMAL_ON_HOOK:
                bindNormalOnHook(holder, (WXOnHookAccount) recyleObj.getDataObjT());
                break;
            case PROCESS_ON_HOOK:
                bindProcessOnHook(holder, (WXOnHookAccount) recyleObj.getDataObjT());
                break;
            case PROMPT_ON_HOOK:
                bindPromptOnHook(holder, (List<WXOnHookAccount>) recyleObj.getDataObjT());
                break;
        }
    }

    private void bindProcessOnHook(ViewHolder holder, final WXOnHookAccount dataObjT) {
        TextView weixin_name_tv = holder.getView(R.id.weixin_name_tv);
        TextView customer_service_name_tv = holder.getView(R.id.customer_service_name_tv);
        weixin_name_tv.setText(dataObjT.getAccount());
        customer_service_name_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                G.showToast("请联系客服："+dataObjT.getCustomer_service());
            }
        });
    }

    private void bindNormalOnHook(ViewHolder holder, WXOnHookAccount dataObjT) {
        TextView weixin_name_tv = holder.getView(R.id.weixin_name_tv);
        TextView earnings_money_tv = holder.getView(R.id.earnings_money_tv);
        weixin_name_tv.setText(dataObjT.getAccount());
        earnings_money_tv.setText(String.format(Locale.CHINA,"%.2f",dataObjT.getMoney()));
    }

    private void bindAbnormalOnHook(ViewHolder holder, final WXOnHookAccount dataObjT) {
        TextView state_name_tv = holder.getView(R.id.state_name_tv);
        TextView weixin_name_tv = holder.getView(R.id.weixin_name_tv);
        TextView wx_is_open_tv = holder.getView(R.id.wx_is_open_tv);
        TextView earnings_name_tv = holder.getView(R.id.earnings_name_tv);
        TextView wx_how_to_open_tv = holder.getView(R.id.wx_how_to_open_tv);
        weixin_name_tv.setText(dataObjT.getAccount());
        if(TextUtils.isEmpty(dataObjT.getRemarks())){
            earnings_name_tv.setText("账号异常，请联系客服");
        } else {
            earnings_name_tv.setText(dataObjT.getRemarks());
        }
        switch (dataObjT.getStatus()) {
            case STATE_ON_HOOK_NEED_VC://需要用户验证码
                state_name_tv.setText("账号异常");
//                earnings_name_tv.setText("需要验证码");
                wx_is_open_tv.setText("提交验证码");
                wx_how_to_open_tv.setVisibility(View.GONE);
                break;
            case STATE_ON_HOOK_BLOCKED://被封
                state_name_tv.setText("账号异常");
//                earnings_name_tv.setText("账号封禁");
                wx_is_open_tv.setText("已解封，继续赚钱");
                wx_how_to_open_tv.setVisibility(View.VISIBLE);
                break;
            case STATE_ON_HOOK_TOMORROW_RESET://三次验证码失败请明日再提交
            case STATE_ON_HOOK_DEBLOCKED://用户已经解封等待服务器验证
                state_name_tv.setText("账号处理中");
//                earnings_name_tv.setText("等待验证");
                wx_is_open_tv.setText("刷新看看");
                wx_how_to_open_tv.setVisibility(View.GONE);
                break;
            case STATE_ON_HOOK_ABNORMAL://账号异常
                state_name_tv.setText("账号异常");
//                earnings_name_tv.setText("账号或密码错误");
                wx_is_open_tv.setText("修改账号或密码");
                wx_how_to_open_tv.setVisibility(View.GONE);
                break;
            case STATE_ON_HOOK_VC_OVERTIME://验证码超时请重新上传
                state_name_tv.setText("账号异常");
//                earnings_name_tv.setText("验证码超时");
                wx_is_open_tv.setText("提交验证码");
                wx_how_to_open_tv.setVisibility(View.GONE);
                break;
            case STATE_ON_HOOK_VERIFIED://用户已经验证等待脚本验证
                state_name_tv.setText("账号处理中");
//                earnings_name_tv.setText("等待验证");
                wx_is_open_tv.setText("刷新看看");
                wx_how_to_open_tv.setVisibility(View.GONE);
                break;
            default:
                state_name_tv.setText("账号异常");
//                earnings_name_tv.setText("账号异常，请联系客服");
                wx_is_open_tv.setText("修改账号或密码");
                wx_how_to_open_tv.setVisibility(View.GONE);
                break;
        }
        // 各种状态用户可以进行不同的操作
        wx_is_open_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (dataObjT.getStatus()) {
                    case STATE_ON_HOOK_NEED_VC://需要用户验证码
                    case STATE_ON_HOOK_VC_OVERTIME://验证码超时请重新上传
                        //修改验证码
                        Jump2View.getInstance().goCommitWxCodeOnHookView(context, dataObjT.getId(), dataObjT.getAccount(), ALTER_CODE);
                        break;
                    case STATE_ON_HOOK_BLOCKED://被封
                        Jump2View.getInstance().goCommitWxCodeOnHookView(context, dataObjT.getId(), dataObjT.getAccount(), HAS_DEBLOCKED);
                        break;
                    case STATE_ON_HOOK_TOMORROW_RESET://用户已经修改账号密码等待服务器验证
                    case STATE_ON_HOOK_DEBLOCKED://用户已经解封等待服务器验证
                    case STATE_ON_HOOK_VERIFIED://用户已经验证等待脚本验证
                        if(context instanceof LieMakeMoneyAct) {
                            clear();
                            notifyDataSetChanged();
                            ((LieMakeMoneyAct) context).initData();
                        }else
                            G.showToast("请等待或者联系客服："+dataObjT.getCustomer_service());
                        break;
                    case STATE_ON_HOOK_ABNORMAL://账号异常
                    default:
                        //修改用户信息
                        Jump2View.getInstance().goCommitWxCodeOnHookView(context, dataObjT.getId(), dataObjT.getAccount(), ALTER_INFO);
                        break;
                }
            }
        });
        wx_how_to_open_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(context instanceof LieMakeMoneyAct)
                    DialogAccountAbnormal.showDialog((Activity) context);
                else if(BuildConfig.DEBUG)
                    G.showToast("测试：请确保context为LieMakeMoneyAct的实例");
            }
        });
        //测试
        if(BuildConfig.DEBUG){
            earnings_name_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    G.showToast("备注："+dataObjT.getRemarks());
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        RecyleObj item = ListUtil.getItem(listData, position);
        return item != null ? item.getRecyleType().ordinal() : position;
    }

    private void bindStartOnHook(ViewHolder holder) {
        ImageView imageView = holder.getView(R.id.start_on_hook_iv, ImageView.class);
        imageView.setOnClickListener(new View.OnClickListener() {
                                         @Override
                                         public void onClick(View view) {
                                             Jump2View.getInstance().goGuideOnHook(context, null);
                                         }
                                     });
    }

    private void bindPromptOnHook(ViewHolder holder, final List<WXOnHookAccount> list) {
        View ll = holder.getView(R.id.lie_make_operation_ll);
        ll.setVisibility(list != null ? View.VISIBLE : View.GONE);
        ll.findViewById(R.id.start_on_hook_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击添加挂机账号,进入引导小号界面
                Jump2View.getInstance().goGuideOnHook(context, null);
            }
        });
        ll.findViewById(R.id.customer_service_name_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//            // 点击联系客服
                if(list != null && !list.isEmpty())
                    G.showToast("请联系客服："+list.get(0).getCustomer_service());
                else if(BuildConfig.DEBUG)
                    G.showToast("传入的context不是activity，无法弹出对话框");
            }
        });
    }

}
