package com.android.chmo.ui.activity.me;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
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.Res;
import com.android.chmo.http.service.UserService;
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/8/16.
 */

public class ForgetPwdActivity extends BaseActivity {
    @BindView(R.id.phone_content)
    View phoneContent;
    @BindView(R.id.pwd_content)
    View pwdContent;

    @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;

    private VerifyCodeCounter codeCounter;

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



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

    @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(1,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.next)
    public void next() {
        final String phone = phoneEdit.getText().toString();
        String code = codeEdit.getText().toString();
        if(TextUtils.isEmpty(phone)) {
            showToast("请输入手机号");
            return;
        }
        if(!CommonUtils.isPhone(phone)) {
            showToast("手机号不正确");
            return;
        }

        if(TextUtils.isEmpty(code)){
            showToast("请输入验证码");
            return;
        }
        if(!code.equals(this.code)) {
            showToast("验证码不正确");
            return;
        }

        phoneContent.setVisibility(View.GONE);
        pwdContent.setVisibility(View.VISIBLE);
    }


    @OnClick(R.id.submit)
    public void submit() {
        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(password)){
            showToast("请输入密码");
            return;
        }

        if(password.length() < 6){
            showToast("密码长度不少于6位");
            return;
        }
        if(!password.equals(confirmPwd)) {
            showToast("确认密码不一致");
            return;
        }


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

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

    }



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