package com.sxyz.rfid.ui.login;

import android.annotation.SuppressLint;
import android.os.Bundle;
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.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.orhanobut.hawk.Hawk;
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.greendao.GreenDaoManager;
import com.sxyz.rfid.greendao.model.Out;
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.view.ILoginView;
import com.sxyz.rfid.ui.MainActivity;
import com.sxyz.rfid.ui.MainActivity2;
import com.sxyz.rfid.ui.login.bean.UserBean;
import com.sxyz.rfid.utils.LocalUtils;
import com.sxyz.rfid.utils.ToastUtil;

import java.util.List;

import sql.com.yz.db.DaoSession;

/**
 * @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;

    @Override
    public void initParms(Bundle mBundle) {

    }

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

    @Override
    protected void initView() {
    }

    @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);
    }

    @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."));
                }
            }
        }
    }

    /**
     * 登录成功
     */
    @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(MainActivity2.class);//跳转首页
        ToastUtil.showShort(R.string.login_success);
    }

    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;
    }
}




