package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.di.components.DaggerCommitWxCodeMakeMoneyComponent;
import com.sheep.gamegroup.di.modules.CommitWxCodeMakeMoneyModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.presenter.CommitWxCodeMakeMoneyContract;
import com.sheep.gamegroup.presenter.CommitWxCodeMakeMoneyPresenter;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.StringUtils;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

import static com.sheep.gamegroup.view.activity.LieMakeMoneyAct.STATE_ON_HOOK_DEBLOCKED;
import static com.sheep.gamegroup.view.activity.LieMakeMoneyAct.STATE_ON_HOOK_VERIFIED;

/**
 * Created by realicing on 2018/3/23.
 * realicing@sina.com
 * 提交微信验证码
 */

public class CommitWxCodeAct extends BaseActivity implements CommitWxCodeMakeMoneyContract.View{

    public static final int ALTER_INFO = 0;
    public static final int HAS_DEBLOCKED = 1;
    public static final int ALTER_CODE = 2;
    @BindView(R.id.tv_wx_commit)
    TextView tv_wx_commit;
    @BindView(R.id.et_wx_account)
    EditText et_wx_account;
    @BindView(R.id.et_wx_pwd)
    EditText et_wx_pwd;
    @BindView(R.id.tv_wx_hint)
    TextView tv_wx_hint;
    private int id;
    private int type;

    @OnClick({R.id.tv_wx_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_wx_commit:
                String account = et_wx_account.getText().toString();
                String password = et_wx_pwd.getText().toString();
                if(TextUtils.isEmpty(account)){
                    G.showToast("手机号不能为空");
                } else if(!StringUtils.isMobile(account)){
                    G.showToast("请输入正确的手机号");
                } else if(et_wx_pwd.getVisibility() == View.VISIBLE && TextUtils.isEmpty(password)){
                    switch (type){
                        case ALTER_CODE:
                            G.showToast("微信验证码不能为空");
                            break;
                        case ALTER_INFO:
                            G.showToast("微信密码不能为空");
                            break;
                    }
                } else if(presenter != null) {
                    showProgress();
                    JSONObject jsonObject = new JSONObject();
                    switch (type){
                        case ALTER_CODE:
                            jsonObject.put("status", STATE_ON_HOOK_VERIFIED);
                            //用户已经验证，等待脚本验证
                            jsonObject.put("verification_code", password);
                            break;
                        case ALTER_INFO:
                            jsonObject.put("status", STATE_ON_HOOK_VERIFIED);
                            //因为账号异常(账号密码错误)，所以修改账号，修改后将等待明天验证
                            jsonObject.put("account", account);
                            jsonObject.put("password", password);
                            break;
                        case HAS_DEBLOCKED:
                            jsonObject.put("status", STATE_ON_HOOK_DEBLOCKED);
                            //用户确认已经解封，设置为等待服务器验证
                            break;
                    }
                    presenter.putWebchat(id, jsonObject);
                } else {
                    if(BuildConfig.DEBUG) G.showToast("功能不正常");
                }
                break;
        }
    }
    @Inject
    CommitWxCodeMakeMoneyPresenter presenter;

    private Activity activity;

    @Override
    protected int getLayoutId() {
        return R.layout.commit_wx_make_money;
    }

    @Override
    public void initView() {
        activity = this;
        id = getIntent().getIntExtra("id", 0);
        type = getIntent().getIntExtra("type", 0);
        String account = getIntent().getStringExtra("account");
        et_wx_account.setText(account);
        String title = "提示";
        switch (type){
            case ALTER_CODE:
                et_wx_account.setEnabled(false);
                et_wx_pwd.setHint("请输入微信验证码");
                et_wx_pwd.setInputType(InputType.TYPE_CLASS_NUMBER);
                title = "提交验证码";
                break;
            case ALTER_INFO:
                title = "修改挂机微信信息";
                break;
            case HAS_DEBLOCKED:
                et_wx_account.setEnabled(false);
                et_wx_pwd.setVisibility(View.GONE);
                tv_wx_hint.setVisibility(View.VISIBLE);
                break;
        }
        TitleBarUtils.getInstance()
                .setTitle(activity, title)
                .setTitleFinish(activity);
        DaggerCommitWxCodeMakeMoneyComponent.builder()
                .netComponent(SheepApp.getInstance().getNetComponent())
                .commitWxCodeMakeMoneyModule(new CommitWxCodeMakeMoneyModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void putWebchatUpdataView(BaseMessage baseMessage) {
        hideProgress();
        G.showToast(baseMessage.getData().toString());//提示信息在data里面
        Jump2View.getInstance().goLyingView(this, null);
    }

    @Override
    public void failPutWebchat2View(BaseMessage baseMessage) {
        hideProgress();
        G.showToast(baseMessage);
    }
}
