package com.example.chemical.ui.plan.unlock;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.GsonUtils;
import com.example.chemical.ChemicalApp;
import com.example.chemical.databinding.ActivityUnlockBinding;
import com.example.chemical.ui.SplashActivity;
import com.example.chemical.ui.common.BaseCountDownActivity;
import com.example.chemical.ui.plan.PlanAddActivity;
import com.example.chemical.ui.plan.PlanAddData;
import com.example.chemical.ui.verify.DoubleVerifyActivity;
import com.example.chemical.utils.UiManager;
import com.rc.httpcore.bean.UserValidationBean;

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

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

    protected UnlockAdapter unlockAdapter;

    @NonNull
    @Override
    protected ActivityUnlockBinding createViewBinding() {
        return binding = ActivityUnlockBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initViews(@Nullable Bundle savedInstanceState) {
        super.initViews(savedInstanceState);
        Intent getIntent = getIntent();
        if (null != getIntent && getIntent.hasExtra("data")) {
            String json = getIntent.getStringExtra("data");
            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 View.OnClickListener() {
                    @Override
                    public void onClick(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(v -> activityHelp.reTryUnlock());
                binding.saveBT.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        activityHelp.save();
                    }
                });
            }
        }
        // 退出
        binding.tvOutLogin.setOnClickListener(v -> callLogoutApi(aBoolean -> {
            ChemicalApp.userData = null;
            ChemicalApp.subjectId = null;
            UiManager.INSTANCE.switcherCashier(UnlockActivity.this, SplashActivity.class);
        }));
        binding.exit.setOnClickListener(v -> 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;
        }
    }
}
