package com.mayi.fastdevelop.commonpage.login;


import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.mayi.fastdevelop.R;
import com.mayi.fastdevelop.api.UserAPI;
import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.bean.UserInfo;
import com.mayi.fastdevelop.commonpage.main.MainActivity;
import com.mayi.fastdevelop.comnon.Configure;
import com.mayi.fastdevelop.comnon.Key;
import com.mayi.fastdevelop.comnon.UserManager;
import com.mayi.fastdevelop.util.ActivitysManager;
import com.mayi.fastdevelop.util.RegUtil;
import com.mayi.fastdevelop.util.SpUtil;
import com.mayi.fastdevelop.util.SystemUtil;


/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity<LoginPresenter, UserAPI> implements LoginContract.View, View.OnClickListener {

    EditText mEditAccount;
    EditText mEditPwd;
    Button mBtnLogin;
    ImageView mImgEye;
    ImageView img_logo;
    private int icon_open_eyes, icon_close_eyes;

    private boolean bShowPwd;

    @Override
    public LoginPresenter getPresenterInstance() {
        return new LoginPresenter();
    }

    @Override
    public UserAPI getModelInstance() {
        return new UserAPI();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if (Configure.ICON_LOGIN_PWD_CLOSE_EYES == 0) {
            icon_close_eyes = R.mipmap.icon_close_eyes;
        } else {
            icon_close_eyes = Configure.ICON_LOGIN_PWD_CLOSE_EYES;
        }

        if (Configure.ICON_LOGIN_PWD_OPEN_EYES == 0) {
            icon_open_eyes = R.mipmap.icon_open_eyes;
        } else {
            icon_open_eyes = Configure.ICON_LOGIN_PWD_OPEN_EYES;
        }

        initComponent();
    }

    protected void initComponent() {
        img_logo = findViewById(R.id.img_logo);
        mEditAccount = findViewById(R.id.edit_account);
        mEditPwd = findViewById(R.id.edit_pwd);
        mImgEye = findViewById(R.id.img_eye);
        mBtnLogin = findViewById(R.id.btn_login);
        mImgEye.setOnClickListener(this);
        mBtnLogin.setOnClickListener(this);
        img_logo.setBackgroundResource(Configure.APP_LOGO);
        UserManager.clearUserInfo();
        mBtnLogin.setEnabled(mEditAccount.getText().toString().trim().length() == getResources().getInteger(R.integer.phone_length) && mEditPwd
                .getText().toString().trim().length() >= getResources().getInteger(R.integer.pwd_min_length));
        mEditAccount.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) {
                mBtnLogin.setEnabled(s.length() == getResources().getInteger(R.integer.phone_length) && mEditPwd.getText().toString().trim().length
                        () >= getResources()
                        .getInteger(R.integer.pwd_min_length));
            }
        });
        mEditPwd.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) {
                mBtnLogin.setEnabled(mEditAccount.getText().toString().trim().length() == getResources().getInteger(R.integer.phone_length) && s
                        .length() >= getResources()
                        .getInteger(R.integer.pwd_min_length));
            }
        });
        mEditAccount.setText(SpUtil.get(Key.ACCOUNT, ""));
    }

    @Override
    public void loginSuccess(UserInfo userInfo) {
        dismssLoadingDialog();
        int tipId = R.string.login_failt;
        if (userInfo != null) {
            tipId = R.string.login_success;
            SpUtil.set(Key.ACCOUNT, mEditAccount.getText().toString());
            UserManager.setUserInfo(userInfo);
            gotoActivityAndFinish(getMainActivity());
        }
        showToast(getString(tipId));
    }

    @Override
    public void loginFailure(String msg) {
        dismssLoadingDialog();
        showToast(TextUtils.isEmpty(msg) ? getString(R.string.login_failt) : msg);
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.btn_login) {
            if (RegUtil.isPhone(mEditAccount.getText().toString())) {
                showLoadingDialog();
                getPresenter().login(mEditAccount.getText().toString(), mEditPwd.getText().toString());
            } else {
                showToast(getString(R.string.error_mobile));
            }

        } else if (i == R.id.img_eye) {
            bShowPwd = !bShowPwd;
            mImgEye.setImageResource(bShowPwd ? icon_open_eyes : icon_close_eyes);
            mEditPwd.setKeyListener(DigitsKeyListener.getInstance(getString(R.string.digits)));/*只能输入数字和字母*/
            SystemUtil.setEditTextPwdShow(mEditPwd, bShowPwd);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        try {
            ActivitysManager.finishAllActivity();
            @SuppressLint("WrongConstant") ActivityManager manager = (ActivityManager) getSystemService("activity");
            manager.killBackgroundProcesses(getPackageName());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Class getMainActivity() {
        return MainActivity.class;
    }

}
