package com.app.simplitend.caregiverdashboard.activities.deactivateacc;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.apputils.CaregiverDataCache;
import com.app.simplitend.apputils.PatientDataCache;
import com.app.simplitend.databinding.ActivityDeactivateAccBinding;
import com.app.simplitend.welcome.activities.WelcomeActivity;
import com.app.simplitend.welcome.welcomecg.mvvm.CareGiverData;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.PatientData;
import com.google.android.material.radiobutton.MaterialRadioButton;

public class DeActivateAccountActivity extends AppCompatActivity implements AccountPresenter.AccountReDeActivateCallback {

    protected ActivityDeactivateAccBinding binding;

    private AccountPresenter accountPresenter;
    private ProgressDialog progressDialog;

    private CareGiverData careGiverData;

    private PatientData patientData;

    private boolean isPatient;
    public static final String IS_PATIENT_KEY = "is_patient_key";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDeactivateAccBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        isPatient = getIntent().getBooleanExtra(IS_PATIENT_KEY, false);

        if (isPatient){
            PatientDataCache.getPatientData(this, (patientData1 -> {
                this.patientData = patientData1;
            }), true);
        }else {
            CaregiverDataCache.getCaregiverData(this, (careGiverData1 -> {
                this.careGiverData = careGiverData1;
            }), true);
        }

        accountPresenter = AccountPresenter.getAccountPresenter();
        progressDialog = new ProgressDialog(this);

        initViews();

        clickEvents();

    }

    @Override
    protected void attachBaseContext(Context newBase) {
        final Configuration configuration = new Configuration(newBase.getResources().getConfiguration());
        configuration.fontScale = Math.min(configuration.fontScale, 1.1f);
        applyOverrideConfiguration(configuration);
        super.attachBaseContext(newBase);
    }

    private void initViews() {
        // first showing the confirmation page
        binding.firstView.setVisibility(View.VISIBLE);
        binding.secondView.setVisibility(View.GONE);

        smoothEditTextScroll();
    }

    private void clickEvents() {
        binding.dont.setOnClickListener(v -> onBackPressed());
        binding.dont2.setOnClickListener(v -> onBackPressed());

        binding.doit.setOnClickListener(v -> {
            // now showing the reason page
            binding.firstView.setVisibility(View.GONE);
            binding.secondView.setVisibility(View.VISIBLE);
        });

        binding.confirmDeactivate.setOnClickListener(v -> {

            int selected_radio_id = binding.reasonRadio.getCheckedRadioButtonId();
            if (selected_radio_id <= 0){
                Toast.makeText(this, "Kindly select a reason", Toast.LENGTH_SHORT).show();
                return;
            }

            String reason;
            try {
                reason = ((MaterialRadioButton) findViewById(selected_radio_id)).getText().toString();
            }catch (Exception e){
                reason = "Other";
            }

            int id;
            String token;

            if (isPatient){

                if (patientData == null){
                    Toast.makeText(this, "Couldn't load data", Toast.LENGTH_SHORT).show();
                    return;
                }

                progressDialog.setTitle("Please wait....");
                progressDialog.setMessage("while we de-activate your account");
                progressDialog.setCancelable(false);
                progressDialog.show();

                id = patientData.patientId;
                token = AppUtil.getPatientToken(this);
            }else{
                if (careGiverData == null){
                    Toast.makeText(this, "Couldn't load data", Toast.LENGTH_SHORT).show();
                    return;
                }

                // TODO: 25/10/23 use the reason

                progressDialog.setTitle("Please wait....");
                progressDialog.setMessage("while we de-activate your account");
                progressDialog.setCancelable(false);
                progressDialog.show();

                id = careGiverData.caregiver_xid;
                token = AppUtil.getCgToken(this);
            }

            accountPresenter.accountReDeActivate(AccountPresenter.ACC_DEACTIVATE_BY_USER,
                    id,
                    reason,
                    binding.commetInput.getText().toString(),
                    "Bearer " + token,
                    this);
        });

    }

    @SuppressLint("ClickableViewAccessibility")
    private void smoothEditTextScroll() {
        // scrolling edit text

        binding.commetInput.setOnTouchListener((v, event) -> {
            if (binding.commetInput.hasFocus()) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_SCROLL) {
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    return true;
                }
            }
            return false;
        });

    }

    @Override
    public void onAccountDeReActivate() {
        Toast.makeText(this, "Account Deactivated", Toast.LENGTH_SHORT).show();
        progressDialog.dismiss();

        AppUtil.cgSignOut(this);

        Intent intent = new Intent(this, WelcomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    public void onAccountDeReActivateFailed(Throwable throwable, String message) {
        progressDialog.dismiss();

        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}