package com.bbitcn.silk.ui.activity;

import android.content.Intent;
import android.view.View;

import com.bbitcn.silk.R;
import com.bbitcn.silk.base.BaseActivity;
import com.bbitcn.silk.base.BaseDialog;
import com.bbitcn.silk.databinding.ActivityLoginBinding;
import com.bbitcn.silk.ui.dialog.ConfirmDialog;
import com.bbitcn.silk.ui.dialog.TextViewDialog;
import com.bbitcn.silk.ui.dialog.TipsDialog;
import com.bbitcn.silk.utils.MMKVUtil;
import com.bbitcn.silk.utils.MyUtil;
import com.bbitcn.silk.utils.global.Global;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.StringUtils;

/**
 * @Description 登录界面
 * @Author DuanKaiji
 * @CreateTime 2024年03月27日 15:12
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginActivityPresenter> {
    @Override
    public void initView() {
        presenter.setActivity(this);
        binding.tvVersion.setText("版本号：" + MyUtil.getVersionName(mContext));
        binding.tiUsername.setText(Global.getUserAccount());
    }

    @Override
    public void initListener() {
        super.initListener();
        binding.btnAbout.setOnClickListener(this);
        binding.btnLogin.setOnClickListener(this);
        binding.btnCheckUpdate.setOnClickListener(this);
        binding.btnDeviceAuth.setOnClickListener(this);
        binding.btnRetrievePassword.setOnClickListener(this);
        presenter.getNotice().observe(this, integer -> {
            if (integer == 0) {
                //登录成功
                finish();
                startActivity(new Intent(mContext, MainActivity.class));
            }
        });
        binding.llCompanyId.setOnLongClickListener(v -> {
            new TextViewDialog(mContext, "企业编号", null, new BaseDialog.CustomString() {
                @Override
                public void listener(String selString) {
                    MMKVUtil.put(Global.COMPANY_ID, selString);
                    binding.tvCompanyId.setText("企业编号： " + Global.getCompanyId());
                }
            }, false).show();
            return true;
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //每次进入登录界面，都要尝试获取企业编号
        binding.tvCompanyId.setText("企业编号： " + Global.getCompanyId());
    }


    @Override
    public void myClick(View view) {
        super.myClick(view);
        switch (view.getId()) {
            case R.id.btn_device_auth -> startActivity(new Intent(mContext, AuthActivity.class));
            case R.id.btn_about -> startActivity(new Intent(mContext, AboutActivity.class));
            case R.id.btn_check_update -> startActivity(new Intent(mContext, UpdateActivity.class));
            case R.id.btn_retrieve_password ->
                    new TipsDialog(mContext, "PDA暂不支持修改密码，请通过电脑登录您的账号后，在【个人中心】修改密码，电脑端修改后，手机端同步生效。").show();
            case R.id.btn_login -> {
                String username = binding.tiUsername.getText().toString();
                String password = binding.tiPassword.getText().toString();
                if (StringUtils.isEmpty(username)) {
                    new TipsDialog(mContext, "用户名不可为空").show();
                } else if (StringUtils.isEmpty(password)) {
                    new TipsDialog(mContext, "密码不可为空").show();
                } else {
                    presenter.login(username, password);
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        new ConfirmDialog(mContext, "提示", "是否退出程序？", () -> AppUtils.exitApp()).show();
    }
}
