package com.itant.shiwushu.ui.forget;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.itant.shiwushu.R;
import com.itant.shiwushu.base.LAFBaseActivity;
import com.itant.shiwushu.manager.ToastManager;

/**
 * Created by Jason on 2018/10/2.
 */

public class ForgetActivity extends LAFBaseActivity implements View.OnClickListener, ForgetContract.View {
    private EditText et_email;
    private EditText et_code;
    private EditText et_password;
    private Button btn_code;

    private ForgetPresenter presenter;

    private static final int WHAT_RE_GET_CODE = 0;
    private int totalWait = 60;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            btn_code.setText(totalWait+"S");
            if (totalWait > 0) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessage(WHAT_RE_GET_CODE);
                    }
                }, 1000);
            } else {
                btn_code.setEnabled(true);
                btn_code.setText("验证码");
                totalWait = 60;
            }
            totalWait--;
            return false;
        }
    });

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);

        findViewById(R.id.aciv_back).setOnClickListener(this);
        et_email = findViewById(R.id.et_email);
        et_password = findViewById(R.id.et_password);
        et_code = findViewById(R.id.et_code);
        findViewById(R.id.btn_reset).setOnClickListener(this);

        btn_code = findViewById(R.id.btn_code);
        btn_code.setOnClickListener(this);

        presenter = new ForgetPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.aciv_back:
                // 返回
                finish();
                break;

            case R.id.btn_reset:
                // 重置密码
                resetPassword();
                break;

            case R.id.btn_code:
                // 获取验证码
                String email = et_email.getText().toString();
                if (TextUtils.isEmpty(email)) {
                    ToastManager.getInstance().toastShort("请输入邮箱");
                    return;
                }

                if (!email.contains("@")) {
                    ToastManager.getInstance().toastShort("邮箱格式有误");
                    return;
                }
                setLoading(true);
                presenter.getCode(email);
                break;
        }
    }

    /**
     *  重置密码
     */
    private void resetPassword() {
        String email = et_email.getText().toString();
        String code = et_code.getText().toString();
        String password = et_password.getText().toString();

        if (TextUtils.isEmpty(email)) {
            ToastManager.getInstance().toastShort("请输入邮箱");
            return;
        }

        if (!email.contains("@")) {
            ToastManager.getInstance().toastShort("邮箱格式有误");
            return;
        }

        if (TextUtils.isEmpty(code)) {
            ToastManager.getInstance().toastShort("请输入6为验证码");
            return;
        }



        if (TextUtils.isEmpty(password)) {
            ToastManager.getInstance().toastShort("请输入新密码");
            return;
        }

        setLoading(true);
        presenter.resetPassword(email, code, password);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.setView(null);
        totalWait = -1;
        handler.removeMessages(WHAT_RE_GET_CODE);
    }

    @Override
    public void onRequestComplete() {
        setLoading(false);
    }

    @Override
    public void onResetSuccess(String message) {
        ToastManager.getInstance().toastShort(message);
        finish();
    }

    @Override
    public void onResetFailed(String message) {
        ToastManager.getInstance().toastShort(message);
    }

    @Override
    public void onGetCodeSuccess(String message) {
        ToastManager.getInstance().toastShort(message);
        btn_code.setEnabled(false);
        handler.sendEmptyMessage(WHAT_RE_GET_CODE);
    }

    @Override
    public void onGetCodeFailed(String message) {
        ToastManager.getInstance().toastShort(message);
    }
}
