package com.app.simplitend.caregiverdashboard.activities;

import static androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG;
import static com.app.simplitend.caregiverdashboard.activities.EditProfileInfoActivity.IS_CAREGIVER;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricManager;

import com.app.simplitend.R;
import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.apputils.CaregiverDataCache;
import com.app.simplitend.apputils.RetrofitHelper;
import com.app.simplitend.databinding.ActivityCaregiverProfileBinding;
import com.app.simplitend.welcome.welcomecg.mvvm.CareGiverData;
import com.bumptech.glide.Glide;

public class CaregiverProfileActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    protected ActivityCaregiverProfileBinding binding;

    private CareGiverData careGiverData;

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

        initViews();

        clickEvents();
    }

    @Override
    protected void onResume() {
        super.onResume();
        CaregiverDataCache.getCaregiverData(this, (careGiverData1 -> {
            this.careGiverData = careGiverData1;

            setCareGiverDetails();
        }), true);
    }

    @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 setCareGiverDetails() {
        if (careGiverData == null) {
            Toast.makeText(this, "Couldn't load the data.", Toast.LENGTH_SHORT).show();
            return;
        }

        if (careGiverData.profile_photo != null) {
            // image
            Glide.with(this)
                    .load(RetrofitHelper.IMAGE_BASE_URL + careGiverData.profile_photo)
                    .placeholder(android.R.color.darker_gray)
                    .error(R.drawable.caregiver_img)
                    .into(binding.image);
        }

        binding.name.setText(careGiverData.first_name);
        binding.phoneNumber.setText(careGiverData.phone_number);
        binding.email.setText(careGiverData.email);
        binding.dob.setText(careGiverData.date_of_birth);

        int biometric  = AppUtil.getWantSecurityFlag(this);

        if (biometric == AppUtil.CG_SECURITY_NEEDED){
            binding.biometricCheck.setChecked(true);
        }else {
            binding.biometricCheck.setChecked(false);
        }

    }

    private void initViews() {
        checkIfBiometricAvailable();

        binding.biometricCheck.setOnCheckedChangeListener(this);
    }

    private void checkIfBiometricAvailable() {

        BiometricManager biometricManager = BiometricManager.from(this);

        switch (biometricManager.canAuthenticate(BIOMETRIC_STRONG)) {
            case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
            case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
                binding.biometricCheckView.setVisibility(View.GONE);
                break;
            case BiometricManager.BIOMETRIC_SUCCESS:
            case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
                binding.biometricCheckView.setVisibility(View.VISIBLE);
                break;
        }
    }

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

        binding.editBtn.setOnClickListener(v -> {
            Intent intent = new Intent(this, EditProfileInfoActivity.class);
            intent.putExtra(IS_CAREGIVER, true);
            startActivity(intent);
        });

        binding.changePwd.setOnClickListener(v -> {
            Intent intent = new Intent(this, CgChangePwdActivity.class);
            startActivity(intent);
        });

        binding.changePin.setOnClickListener(v -> {
            Intent intent = new Intent(this, ChangePinActivity.class);
            startActivity(intent);
        });

    }

    // biometric check change listener
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
        if (checked){
            AppUtil.setWantSecurityFlag(this, AppUtil.CG_SECURITY_NEEDED);
        }else{
            AppUtil.setWantSecurityFlag(this, AppUtil.CG_NO_SECURITY_NEEDED);
        }
    }
}