package com.wd.workoffice.ui.activity.login;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.bean.workEnum.CodeType;
import com.wd.workoffice.contract.LoginContract;
import com.wd.workoffice.presenter.LoginPresenter;
import com.wd.workoffice.ui.activity.MainActivity;
import com.wd.workoffice.ui.activity.mes.MesLoginActivity;
import com.wd.workoffice.util.WorkUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.utils.ToastUtil;

/**
 * 关于登录页
 */
public class LoginActivity extends WorkBaseActivity implements LoginContract.View {


    @BindView(R.id.rb_pwd)
    RadioButton rbPwd;
    @BindView(R.id.rb_phone)
    RadioButton rbPhone;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.ll_pwd)
    LinearLayout llPwd;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.tv_forget)
    TextView tvForget;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    private int type;
    private LoginPresenter loginPresenter;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        loginPresenter = new LoginPresenter();
        loginPresenter.onAttach(this);
        changeType(0);
        btnLogin.setEnabled(false);
    }

    @Override
    public void initData() {
    }

    @Override
    protected void initEvent() {
        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(etPhone.getText().toString())) {
                    btnLogin.setEnabled(true);
                } else {
                    btnLogin.setEnabled(false);
                }
            }
        });
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(etCode.getText().toString())) {
                    btnLogin.setEnabled(true);
                } else {
                    btnLogin.setEnabled(false);
                }
            }
        });
        etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(etPassword.getText().toString())) {
                    btnLogin.setEnabled(true);
                } else {
                    btnLogin.setEnabled(false);
                }
            }
        });
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(etAccount.getText().toString())) {
                    btnLogin.setEnabled(true);
                } else {
                    btnLogin.setEnabled(false);
                }
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_login;
    }


    @OnClick({R.id.rb_pwd, R.id.rb_phone, R.id.btn_login, R.id.tv_code, R.id.tv_forget, R.id.tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_pwd:
                changeType(0);
                break;
            case R.id.rb_phone:
                changeType(1);
                break;
            case R.id.tv_code:
                String phone = etPhone.getText().toString();
                loginPresenter.getCode(phone, CodeType.LOGIN.getName());
                break;
            case R.id.btn_login:
                Map<String, Object> param = new HashMap<>();
                String loginType;
                if (type == 0) {
                    loginType = "userNew";
                    String account = etAccount.getText().toString();
                    String pwd = etPassword.getText().toString();
                    param.put("principal", account);
                    param.put("credentials", pwd);
                } else {
                    loginType = "phone";
                    String p = etPhone.getText().toString();
                    String code = etCode.getText().toString();
                    param.put("phone", p);
                    param.put("code", code);
                }
                param.put("clientId", "android");
                param.put("clientSecret", "android");
                btnLogin.setClickable(false);
                loginPresenter.login(loginType, param);
                break;
            case R.id.tv_forget:
                startActivity(ForgetOneActivity.class);
                break;
            case R.id.tv_register:
                startActivity(RegisterFirstActivity.class);
                break;
        }
    }

    /**
     * @param t 0 密码 1 手机号
     */
    private void changeType(int t) {
        if (t == 0) {
            llPhone.setVisibility(View.GONE);//显示
            llPwd.setVisibility(View.VISIBLE);//隐藏
            type = 0;
            if (!TextUtils.isEmpty(etPassword.getText().toString()) && !TextUtils.isEmpty(etAccount.getText().toString())) {
                btnLogin.setEnabled(true);
            } else {
                btnLogin.setEnabled(false);
            }
        } else {
            llPhone.setVisibility(View.VISIBLE);//显示
            llPwd.setVisibility(View.GONE);//隐藏
            type = 1;
          if (!TextUtils.isEmpty(etPhone.getText().toString()) && !TextUtils.isEmpty(etCode.getText().toString())) {
                btnLogin.setEnabled(true);
            } else {
                btnLogin.setEnabled(false);
            }
        }
    }

    @Override
    public void codeSuccess() {
        WorkUtils.timerSeckill(tvCode, 60 * 1000, 1000, 0);
        ToastUtil.showLong("验证码发送成功，请注意查收！");
    }

    @Override
    public void loginSuccess() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    public void onError(String message) {
        super.onError(message);
        btnLogin.setClickable(true);
    }
}
