package com.bbitcn.silk.ui.activity;

import android.transition.Fade;
import android.transition.TransitionManager;
import android.view.View;

import com.bbitcn.silk.R;
import com.bbitcn.silk.base.BaseActivity;
import com.bbitcn.silk.databinding.ActivityAuthBinding;
import com.bbitcn.silk.ui.dialog.TipsDialog;
import com.bbitcn.silk.utils.global.Global;
import com.blankj.utilcode.util.EncryptUtils;
import com.blankj.utilcode.util.StringUtils;

/**
 * @Description 授权界面
 * @Author DuanKaiji
 * @CreateTime 2024年03月27日 15:12
 */
public class AuthActivity extends BaseActivity<ActivityAuthBinding, AuthActivityPresenter> {

    @Override
    public void initView() {
        if (!"未授权".equals(Global.getCompanyId())) {
            presenter.checkAuth();
        }
        binding.tvDeviceId.setText(Global.getDeviceId());
        presenter.getNotice().observe(this, integer -> {
            binding.clCheckSuccess.setVisibility(View.VISIBLE);
            binding.clCheck.setVisibility(View.GONE);
            binding.tvCompanyId.setText(Global.getCompanyId());
            binding.tvName.setText(Global.getRegName());
            binding.tvTel.setText(Global.getRegTel());
            if (integer == 1) {
                //授权成功
                binding.tvState.setText("已授权");
                binding.ivState.setImageResource(R.mipmap.check_success);
            } else if (integer == 2) {
                //待授权
                binding.tvState.setText("待授权");
                binding.ivState.setImageResource(R.mipmap.tips);
            }
        });
    }

    @Override
    public void initListener() {
        super.initListener();
        binding.ivBack.setOnClickListener(v -> finish());
        //申请授权
        binding.btnAuto.setOnClickListener(v -> {
            String id = binding.etId.getText().toString();
            String name = binding.etName.getText().toString();
            String tel = binding.etTel.getText().toString();
            String pwd = binding.etPwd.getText().toString();
            if (StringUtils.isEmpty(id) || StringUtils.isEmpty(name) || StringUtils.isEmpty(tel)) {
                new TipsDialog(mContext, "请先完善信息").show();
            } else {
                presenter.auth(id, name, tel, pwd);
            }
        });
        binding.rlDeviceId.setOnClickListener(v -> {
            //开始动画
            TransitionManager.beginDelayedTransition(binding.llInput, new Fade());
            binding.rlPwd.setVisibility(binding.rlPwd.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        });

    }
}
