package com.app.simplitend.caregiverdashboard.activities;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.app.simplitend.R;
import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.apputils.CaregiverDataCache;
import com.app.simplitend.caregiverdashboard.activities.deactivateacc.DeActivateAccountActivity;
import com.app.simplitend.caregiverdashboard.mvvm.CaregiverMainViewModel;
import com.app.simplitend.caregiverdashboard.mvvm.CgHomeContracts;
import com.app.simplitend.databinding.ActivityCgSettingsBinding;
import com.app.simplitend.databinding.NotificationSetupBottomSheetBinding;
import com.app.simplitend.welcome.activities.WelcomeActivity;
import com.google.android.material.bottomsheet.BottomSheetDialog;

public class CaregiverSettingsActivity extends AppCompatActivity implements CgHomeContracts.SignOutCallback {

    private ActivityCgSettingsBinding binding;

    private CaregiverMainViewModel viewModel;
    private ProgressDialog progressDialog;

    private BottomSheetDialog nSetupDialog;
    protected NotificationSetupBottomSheetBinding nSBinding;

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

        viewModel = new ViewModelProvider(this).get(CaregiverMainViewModel.class);
        progressDialog = new ProgressDialog(this);

        nSBinding = NotificationSetupBottomSheetBinding.inflate(getLayoutInflater());
        nSetupDialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
        nSetupDialog.setContentView(nSBinding.getRoot());

        setupNotifications();

        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 setupNotifications() {
        nSBinding.patientMedication.setChecked(AppUtil.getCgNotificationPref(this, AppUtil.MEDICATIONS_NOTIFICATIONS));
        nSBinding.patientMedRefill.setChecked(AppUtil.getCgNotificationPref(this, AppUtil.MEDICATION_REFILL_NOTIFICATIONS));
        nSBinding.patientActivity.setChecked(AppUtil.getCgNotificationPref(this, AppUtil.ACTIVITY_NOTIFICATIONS));
        nSBinding.patientGeofence.setChecked(AppUtil.getCgNotificationPref(this, AppUtil.GEOFENCE_NOTIFICATIONS));
        nSBinding.patientSos.setChecked(AppUtil.getCgNotificationPref(this, AppUtil.SOS_NOTIFICATIONS));
        nSBinding.patientDirection.setChecked(AppUtil.getCgNotificationPref(this, AppUtil.DIRECTIONS_NOTIFICATIONS));

        nSBinding.patientMedication.setOnCheckedChangeListener((compoundButton, b) -> {
            AppUtil.setCgNotificationPref(this, AppUtil.MEDICATIONS_NOTIFICATIONS, b);
        });

        nSBinding.patientMedRefill.setOnCheckedChangeListener((compoundButton, b) -> {
            AppUtil.setCgNotificationPref(this, AppUtil.MEDICATION_REFILL_NOTIFICATIONS, b);
        });

        nSBinding.patientActivity.setOnCheckedChangeListener((compoundButton, b) -> {
            AppUtil.setCgNotificationPref(this, AppUtil.ACTIVITY_NOTIFICATIONS, b);
        });

        nSBinding.patientGeofence.setOnCheckedChangeListener((compoundButton, b) -> {
            AppUtil.setCgNotificationPref(this, AppUtil.GEOFENCE_NOTIFICATIONS, b);
        });

        nSBinding.patientSos.setOnCheckedChangeListener((compoundButton, b) -> {
            AppUtil.setCgNotificationPref(this, AppUtil.SOS_NOTIFICATIONS, b);
        });

        nSBinding.patientDirection.setOnCheckedChangeListener((compoundButton, b) -> {
            AppUtil.setCgNotificationPref(this, AppUtil.DIRECTIONS_NOTIFICATIONS, b);
        });
    }

    private void initViews() {

    }

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

        binding.logout.setOnClickListener(v -> {
            AppUtil.showAlert(this,
                    "Are you sure?", "Do you want to sign out?",
                    getString(R.string.no),
                    ((dialogInterface, i) -> {
                    }),
                    getString(R.string.yes),
                    ((dialogInterface, i) -> {
                        progressDialog.setTitle("Please wait...");
                        progressDialog.setMessage("while we sign you out");
                        progressDialog.setCancelable(false);
                        progressDialog.show();

                        viewModel.signOut("Bearer " + AppUtil.getCgToken(this),
                                this);
                    }));
        });

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

        binding.tNC.setOnClickListener(v -> {
            AppUtil.openLink(this, getString(R.string.terms_n_conditions_link),
                    getString(R.string.terms_conditions_));
        });

        binding.privacy.setOnClickListener(v -> {
            AppUtil.openLink(this, getString(R.string.privacy_policy_link),
                    getString(R.string.privacy_policy));
        });

        binding.setupNoti.setOnClickListener(v -> {
            nSetupDialog.show();
        });

    }

    @Override
    public void onSignOutSuccess() {
        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 onSignOutFailed(Throwable t, String message) {
        progressDialog.dismiss();

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