package com.app.simplitend.patient_dashboard;

import static com.app.simplitend.caregiverdashboard.activities.deactivateacc.DeActivateAccountActivity.IS_PATIENT_KEY;

import androidx.appcompat.app.AppCompatActivity;

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 com.onesignal.OneSignal;
import com.app.simplitend.R;
import com.app.simplitend.apputils.AppUtil;
import com.app.simplitend.caregiverdashboard.activities.deactivateacc.AccountPresenter;
import com.app.simplitend.caregiverdashboard.activities.deactivateacc.DeActivateAccountActivity;
import com.app.simplitend.caregiverdashboard.mvvm.CgHomeContracts;
import com.app.simplitend.databinding.ActivityPatSettingsBinding;
import com.app.simplitend.welcome.activities.WelcomeActivity;

public class PatSettingsActivity extends AppCompatActivity implements CgHomeContracts.SignOutCallback {

    private ActivityPatSettingsBinding binding;
    private ProgressDialog progressDialog;

    private AccountPresenter accountPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPatSettingsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        progressDialog = new ProgressDialog(this);
        accountPresenter = AccountPresenter.getAccountPresenter();

        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() {
        binding.notificationCheck.setChecked(OneSignal.getUser().getPushSubscription().getOptedIn());
        binding.notificationCheck.setOnCheckedChangeListener((compoundButton, check) -> {
            if (check){
                OneSignal.getUser().getPushSubscription().optIn();
            }else{
                OneSignal.getUser().getPushSubscription().optOut();
            }
        });
    }

    private void clickEvents() {

        binding.backBtn.setOnClickListener(v -> onBackPressed());

        binding.deActivate.setOnClickListener(v -> {
            Intent intent = new Intent(this, DeActivateAccountActivity.class);
            intent.putExtra(IS_PATIENT_KEY, true);
            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.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();

                        accountPresenter.signOut("Bearer " + AppUtil.getPatientToken(this),
                                this);
                    }));
        });
    }

    @Override
    public void onSignOutSuccess() {
        progressDialog.dismiss();

        AppUtil.patientSignOut(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();
    }
}