package xn.hxp.ui.plan.unlock;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.rc.core.ui.activity.BaseActivity;
import com.rc.httpcore.HttpConfig;
import com.rc.httpcore.bean.UserValidationBean;
import com.rc.httpcore.client.HttpTool;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import xn.hxp.app.ChemicalApp;
import xn.hxp.databinding.ActivityUnlockBinding;
import xn.hxp.ui.plan.PlanAddData;
import xn.hxp.ui.verify.DoubleVerifyActivity;

public class UnlockActivity extends BaseActivity {
    protected ActivityUnlockBinding binding;
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;
    protected UnlockActivityHelp activityHelp;

    protected PlanAddData planAddData;
    // 验证通过的人员1
    private UserValidationBean userValidation1;
    // 验证通过的人员2
    private UserValidationBean userValidation2;

    protected UnlockAdapter unlockAdapter;
    private boolean isAuto;


    @Override
    protected ViewBinding setViewBinding() {
        return binding = ActivityUnlockBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onInit() {
        Intent getIntent = getIntent();
        if (null != getIntent && getIntent.hasExtra("data")) {
            String json = getIntent.getStringExtra("data");
            isAuto = getIntent.getBooleanExtra("isAuto", false);
            if (null != json && !TextUtils.isEmpty(json)) {
                planAddData = GsonUtils.fromJson(json, PlanAddData.class);
                activityHelp = new UnlockActivityHelp(this, planAddData);

                intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
                    if (result.getResultCode() == RESULT_OK) {
                        Intent intent = result.getData();
                        if (null != intent && intent.hasExtra("userValidation1") && intent.hasExtra("userValidation2")) {
                            userValidation1 = GsonUtils.fromJson(intent.getStringExtra("userValidation1"), UserValidationBean.class);
                            userValidation2 = GsonUtils.fromJson(intent.getStringExtra("userValidation2"), UserValidationBean.class);
                        }
                        binding.validateBT.setVisibility(View.GONE);
                        binding.unlockLL.setVisibility(View.VISIBLE);
                        activityHelp.unlock(userValidation1, userValidation2);
                    }
                });
                binding.validateBT.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                    @Override
                    public void onDebouncingClick(View v) {
                        Intent intent = new Intent(UnlockActivity.this, DoubleVerifyActivity.class);
                        String json = GsonUtils.toJson(planAddData);
                        intent.putExtra("data", json);
                        intentActivityResultLauncher.launch(intent);
                    }
                });

                binding.retryBT.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                    @Override
                    public void onDebouncingClick(View v) {
                        activityHelp.reTryUnlock();
                    }
                });
                binding.saveBT.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
                    @Override
                    public void onDebouncingClick(View v) {
                        activityHelp.save(isAuto);
                    }
                });
            }
        }

        try {
            // 日期
            binding.nowTime.setText(TimeUtils.getNowString(new SimpleDateFormat("MM月dd日 EEEE ", Locale.getDefault())));
            // 用户
            Glide.with(this).load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.userData.getAvatar()).into(binding.imageName);
            String userName = ChemicalApp.userData.getUserName();
            binding.tvName.setText(TextUtils.isEmpty(userName) ? "" : userName);
            // 实验室logo
            Glide.with(this).load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.confs.getCircularLogo()).into(binding.image);
            // 实验室名称
            String subName = ChemicalApp.subjectName;
            subName = TextUtils.isEmpty(subName) ? "" : subName;
            // 房间号
            String roomNum = ChemicalApp.subRoom;
            roomNum = TextUtils.isEmpty(roomNum) ? "" : roomNum;
            binding.deptName.setText(subName + "-" + roomNum);
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
        }
        // 退出
        binding.tvOutLogin.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                try {
                    HttpTool.logout();
                } catch (IOException e) {
                    LogUtils.e(Log.getStackTraceString(e));
                }
            }
        });
        binding.exit.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void cdFinish() {
        finish();
    }

    @Override
    protected void cdTime(int cd) {
        binding.exit.setText("结束" + cd + "s");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != intentActivityResultLauncher) {
            intentActivityResultLauncher.unregister();
        }
        if (null != activityHelp) {
            activityHelp.onDestroy();
            activityHelp = null;
        }
    }
}
