package com.android.chmo.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.app.ChmoApplication;
import com.android.chmo.app.SpManager;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.RegisterRes;
import com.android.chmo.http.service.UserService;
import com.android.chmo.ui.activity.me.ProtocolActivity;
import com.android.chmo.utils.CodeUtils;
import com.android.chmo.utils.CommonUtils;
import com.android.chmo.utils.VerifyCodeCounter;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/31.
 */

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.nickName)
    EditText nickNameEdit;
    @BindView(R.id.phone)
    EditText phoneEdit;
    @BindView(R.id.password)
    EditText passwordEdit;
    @BindView(R.id.confirm_password)
    EditText confirmPwdEdit;
    @BindView(R.id.code)
    EditText codeEdit;

    @BindView(R.id.getCode)
    TextView getCodeBtn;
    @BindView(R.id.checkbox)
    ImageView checkBox;

    private boolean protocolCheck = false;

    private VerifyCodeCounter codeCounter;

    private String code = ChmoApplication.getApp().getCode();

    @Override
    protected int getContentResId() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {

    }

    @OnClick(R.id.getCode)
    public void getCode() {
        String phone = phoneEdit.getText().toString();
        if(TextUtils.isEmpty(phone)) {
            showToast("请输入手机号");
            return;
        }
        if(!CommonUtils.isPhone(phone)) {
            showToast("手机号不正确");
            return;
        }

        code = CodeUtils.randomCode();
        String codeStr = CodeUtils.getCodeStr(0,code);
        showLoading();
        UserService.getCode(phone,codeStr, new RequestCallback(){
            @Override
            public void onSuccess(String result) {
                hideLoading();
                ChmoApplication.getApp().setCode(code);
                codeCounter = new VerifyCodeCounter(60000,1000,getCodeBtn);
                codeCounter.start();
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
                showToast("获取验证码失败");
            }
        });

    }

    @OnClick(R.id.register)
    public void register() {
        String nickName = nickNameEdit.getText().toString();
        final String phone = phoneEdit.getText().toString();
        final String password = passwordEdit.getText().toString();
        String confirmPwd = confirmPwdEdit.getText().toString();
        String code = codeEdit.getText().toString();
        if(TextUtils.isEmpty(nickName)) {
            showToast("请输入昵称");
            return;
        }

        if(TextUtils.isEmpty(phone)) {
            showToast("请输入手机号");
            return;
        }
        if(!CommonUtils.isPhone(phone)) {
            showToast("手机号不正确");
            return;
        }
        if(TextUtils.isEmpty(password)){
            showToast("请输入密码");
            return;
        }

        if(password.length() < 6){
            showToast("密码长度不少于6位");
            return;
        }
        if(!password.equals(confirmPwd)) {
            showToast("确认密码不一致");
            return;
        }
        if(TextUtils.isEmpty(code)){
            showToast("请输入验证码");
            return;
        }
        if(!code.equals(this.code)) {
            showToast("验证码不正确");
            return;
        }

        if (!protocolCheck) {
            showToast("请先阅读并同意“用户协议”和“隐私协议”");
            return;
        }

        showLoading();
        UserService.register(nickName, phone, password, new RequestCallback(){
            @Override
            public void onSuccess(String result) {
                hideLoading();
                RegisterRes res = new Gson().fromJson(result, RegisterRes.class);
                if(res.msg.equals("fail")) {
                    showToast(TextUtils.isEmpty(res.value) ? "注册失败" : res.value);
                }else {
                    //注册成功
                    showToast("注册成功");
                    SpManager.getInstance().setString("LoginPhone", phone);
                    SpManager.getInstance().setString("LoginPwd", password);
                    finish();
                }
            }

            @Override
            public void onFailure(String error) {
                hideLoading();
                showToast("注册失败");
            }
        });

    }



    @OnClick(R.id.container)
    public void containerClick() {
        CommonUtils.closeKeybord(phoneEdit,this);
    }

    @OnClick(R.id.protocol)
    public void checkProtocol() {
        protocolCheck = !protocolCheck;
        checkBox.setImageResource(protocolCheck ? R.mipmap.checked : R.mipmap.check);
    }

    @OnClick(R.id.user_protocol)
    public void userProtocol() {
        Intent intent = new Intent(this, ProtocolActivity.class);
        intent.putExtra("title", "用户协议");
        intent.putExtra("url", "file:///android_asset/user_protocol.html");
        openPage(intent);
    }

    @OnClick(R.id.privacy_protocol)
    public void privacyProtocol() {
        Intent intent = new Intent(this, ProtocolActivity.class);
        intent.putExtra("title", "隐私协议");
        intent.putExtra("url", "file:///android_asset/privacy_protocol.html");
        openPage(intent);
    }
}
