package com.unis.sxzcl.ui.login;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.unis.model.Constant;
import com.unis.model.DataCallback;
import com.unis.model.UserModel;
import com.unis.model.bean.LoginBean;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentLoginBinding;
import com.unis.sxzcl.ui.home.MainActivity;
import com.unis.sxzcl.util.VersionCheckTool;
import com.xyxsbj.mylibrary.tool.ActivityTool;
import com.xyxsbj.mylibrary.tool.ETTool;
import com.xyxsbj.mylibrary.tool.SPTool;
import com.xyxsbj.mylibrary.tool.StringTool;

/**
 * 包名: com.unis.sxzcl.ui.login
 * 类名: LoginFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/11/12 11:16
 * 更新人:
 * 更新时间: 2020/11/12 11:16
 * 更新说明:
 * 版本号: 1.0
 */
public class LoginFragment extends BaseFragment<FragmentLoginBinding> {

    private String mName;
    private String mPassword;
    private VersionCheckTool mVersionCheckTool;

    @Override
    protected FragmentLoginBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentLoginBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        setStatusBarWhite(false);

        mBinding.btnLogin.setOnClickListener(v -> {
            //登录
            login();
        });
        mBinding.etName.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) {
                mBinding.layoutName.setError(null);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBinding.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) {
                mBinding.layoutPassword.setError(null);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBinding.etPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    login();
                }
                return false;
            }
        });
    }

    @Override
    protected void onFirstShow() {
        mBinding.etName.setText(SPTool.getString(Constant.KEY_LOGIN_NAME, null));
        mBinding.etPassword.setText(SPTool.getString(Constant.KEY_LOGIN_PWD, null));

        // 版本检查
        mVersionCheckTool = new VersionCheckTool(getContext())
                .setUpdateCallback((data, newest) -> {
                    if (!newest)// 不是新版本时启动对话框并下载
                        mVersionCheckTool.showUpdateDialog(data.get(0));
                })
                .setError(error -> {
                })
                .start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mVersionCheckTool != null)
            mVersionCheckTool.destroy();
        mVersionCheckTool = null;
    }

    /**
     * 登录
     */
    private void login() {
        if (!verify()) return;

        showProgress("正在登陆...", false);
        UserModel.get().login(mName, mPassword, new DataCallback<LoginBean>() {
            @Override
            public void onSuccess(LoginBean data) {
                if (!isActive()) return;
                hideProgress();
                SPTool.saveString(Constant.KEY_LOGIN_NAME, mName);
                SPTool.saveString(Constant.KEY_LOGIN_PWD, mPassword);
                ActivityTool.startActivity(getContext(), MainActivity.class);
                finish();
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                if (!isActive()) return;
                hideProgress();
            }
        });
    }

    /**
     * 输入校验
     */
    private boolean verify() {

        mName = ETTool.getData(mBinding.etName);
        if (StringTool.isEmpty(mName)) {
            mBinding.layoutName.setError("账号不得为空");
            return false;
        }
        mPassword = ETTool.getData(mBinding.etPassword);
        if (StringTool.isEmpty(mPassword)) {
            mBinding.layoutPassword.setError("密码不得为空");
            return false;

        }
        return true;
    }

}
