package com.sxyz.rfid.ui.login;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.hawk.Hawk;
import com.sxyz.rfid.App;
import com.sxyz.rfid.AppConfig;
import com.sxyz.rfid.R;
import com.sxyz.rfid.base.BaseActivity;
import com.sxyz.rfid.databinding.ActivityLoginBinding;
import com.sxyz.rfid.presenter.ILoginPresenter;
import com.sxyz.rfid.presenter.imp.LoginPresenterImpl;
import com.sxyz.rfid.presenter.model.LoginModel;
import com.sxyz.rfid.presenter.model.UserInfoModel;
import com.sxyz.rfid.presenter.view.ILoginView;
import com.sxyz.rfid.ui.MainActivity;
import com.sxyz.rfid.ui.login.bean.UserBean;
import com.sxyz.rfid.ui.review.offline.ReviewOfflineActivity;
import com.sxyz.rfid.utils.LocalUtils;
import com.sxyz.rfid.utils.ToastUtil;
import com.sxyz.rfid.widgets.DialogUtil;

/**
 * @Description: 登录
 * @Author: chunyuan.wu
 * @CreateDate: 2025/12/29 09:33:28
 * @Version: 1.0.0
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding> implements View.OnClickListener, ILoginView {
    private int eye = 0;
    private ILoginPresenter mILoginPresenter;
    private final static int COUNTS = 5;// 点击次数
    private final static long DURATION = 1000;// 规定有效时间
    private long[] mHits = new long[COUNTS];
    private DialogUtil mDialogUtil;

    @Override
    public void initParms(Bundle mBundle) {

    }

    @Override
    protected ActivityLoginBinding getViewBinding() {
        return ActivityLoginBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        mDialogUtil = new DialogUtil();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void setListener() {
        mILoginPresenter = new LoginPresenterImpl(this);
        mBinding.etUserName.addTextChangedListener(new EtUserName());
        mBinding.etPassword.addTextChangedListener(new EtPassWord());
        mBinding.ivClear.setOnClickListener(this);
        mBinding.ivEye.setOnClickListener(this);
        mBinding.tvLogin.setOnClickListener(this);
        mBinding.txtWellcome.setOnClickListener(this);
        mBinding.tvOffline.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        mBinding.etUserName.setText(LocalUtils.getUserName());
        mBinding.etPassword.setText(LocalUtils.getPassWord());
        if (!AppConfig.DEBUG) {
            mBinding.etUserName.setText("admin");
            mBinding.etPassword.setText("admin123");
//            mBinding.etUserName.setText("wcy");
//            mBinding.etPassword.setText("sxyz123456");
        }
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivClear: {//清除账号
                mBinding.etUserName.setText("");
                break;
            }
            case R.id.ivEye: {//查看密码
                if (eye == 0) {
                    mBinding.etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mBinding.ivEye.setImageResource(R.drawable.eye_closed_icon);
                    eye = 1;
                } else {
                    mBinding.etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mBinding.ivEye.setImageResource(R.drawable.eye_open_icon);
                    eye = 0;
                }
                break;
            }
            case R.id.tvLogin: {//登录
                String userName = mBinding.etUserName.getText().toString();
                String password = mBinding.etPassword.getText().toString();
                if (isNull(userName, password)) {
                    mILoginPresenter.doLogin(this, new UserBean(userName, password, "SXyz@192837."));
                }
            }
            case R.id.txtWellcome: {//欢迎页
                if (!AppConfig.DEBUG) {
                    continuousClick(COUNTS, DURATION);
                }
            }
            break;
            case R.id.tvOffline: {//离线执行盘点列表
                startActivity(ReviewOfflineActivity.class);//跳转离线执行盘点列表
                break;
            }
        }
    }

    /**
     * @description 点击5次数弹出切换ip弹窗
     * @author wuchunyuan
     * @time 2020/8/5 17:40
     */
    private void continuousClick(int count, long time) {
        //每次点击时，数组向前移动一位
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        //为数组最后一位赋值
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
        if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) {
            mHits = new long[COUNTS];//重新初始化数组
            Dialog mDialog = mDialogUtil.showIpDialog(this);
            mDialogUtil.setOnSwitchItemClickListener((ip, port) -> {
                Hawk.put(AppConfig.Ip.IP, ip);
                Hawk.put(AppConfig.Ip.PORT, port);
                ToastUtils.showLong(R.string.text_switch_ip_success_one_more_time_login_app);
                new Handler().postDelayed(() -> App.getInstance().exit(), 1000);
                mDialog.dismiss();
            });
        }
    }

    /**
     * 登录成功
     */
    @Override
    public void loginSuccess(LoginModel loginModel) {
        SPUtils.getInstance().put(LocalUtils.AUTO_USER_NAME, mBinding.etUserName.getText().toString());//保存账号
        SPUtils.getInstance().put(LocalUtils.AUTO_PWD, mBinding.etPassword.getText().toString());//保存密码
        SPUtils.getInstance().put(LocalUtils.TOKEN, loginModel.getToken());//AI TOKEN
        Hawk.put(LocalUtils.LOGIN_MODEL, loginModel);
        startActivityFinish(MainActivity.class);//跳转首页
        ToastUtil.showShort(R.string.login_success);
    }

    @Override
    public void getUserInfoSuccess(UserInfoModel userInfoModel) {

    }

    private class EtPassWord implements 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) {
            if (mBinding.etPassword.length() > 0) {
                mBinding.ivEye.setVisibility(View.VISIBLE);
            } else {
                mBinding.ivEye.setVisibility(View.GONE);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    private class EtUserName implements 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) {
            if (mBinding.etUserName.length() > 0) {
                mBinding.ivClear.setVisibility(View.VISIBLE);
            } else {
                mBinding.ivClear.setVisibility(View.GONE);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    private boolean isNull(String userName, String password) {
        if (TextUtils.isEmpty(userName)) {
            showToast(R.string.please_enter_your_account_number);
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            showToast(R.string.please_enter_your_password);
            return false;
        }
        return true;
    }
}




