package com.app.simplitend.patient_dashboard.fragments;

import static com.app.simplitend.appblocking.FUAActivity.IS_FROM_DASHBOARD;
import static com.app.simplitend.caregiverdashboard.activities.EditProfileInfoActivity.IS_FROM_PATIENT;
import static com.app.simplitend.caregiverdashboard.activities.PatientProfileShowerActivity.ACTIVITY_F;
import static com.app.simplitend.caregiverdashboard.activities.PatientProfileShowerActivity.CONTACTS_F;
import static com.app.simplitend.caregiverdashboard.activities.PatientProfileShowerActivity.MED_INFO_F;
import static com.app.simplitend.caregiverdashboard.activities.PatientProfileShowerActivity.MED_REMINDER_F;
import static com.app.simplitend.caregiverdashboard.activities.PatientProfileShowerActivity.WHICH_FRAGMENT;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.app.simplitend.R;
import com.app.simplitend.appblocking.FUAActivity;
import com.app.simplitend.apputils.PatientDataCache;
import com.app.simplitend.apputils.RetrofitHelper;
import com.app.simplitend.caregiverdashboard.activities.EditProfileInfoActivity;
import com.app.simplitend.caregiverdashboard.activities.PatientProfileShowerActivity;
import com.app.simplitend.databinding.ActivityPatProfileInfoBinding;
import com.app.simplitend.patient_dashboard.PatSettingsActivity;
import com.app.simplitend.welcome.welcomepatient.mvvm.models.PatientData;
import com.bumptech.glide.Glide;

import java.util.Locale;

public class PatientProfileInfoFragment extends Fragment {

    private ActivityPatProfileInfoBinding binding;

    private PatientData patientData;

    public PatientProfileInfoFragment(){
        // required
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = ActivityPatProfileInfoBinding.inflate(inflater, container, false);

        initViews();

        clickEvents();

        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        PatientDataCache.getPatientData(requireContext(), (patientData -> {
            this.patientData = patientData;

            setPatientDetails();
        }), true);
    }

    private void setPatientDetails() {
        if (patientData == null) {
            Toast.makeText(requireContext(), "Couldn't load patient data", Toast.LENGTH_SHORT).show();
            return;
        };

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

        binding.name.setText(patientData.first_name);
        binding.phoneNumber.setText(patientData.phone_number);
    }

    private void clickEvents() {

        binding.backBtn.setOnClickListener(v -> {
            Activity activity = getActivity();
            if (activity != null){
                activity.onBackPressed();
            }
        });

        binding.medication.setOnClickListener(v -> {
            gotoProfileShower(MED_REMINDER_F);
        });

        binding.contacts.setOnClickListener(v -> {
            gotoProfileShower(CONTACTS_F);
        });

        binding.medRecords.setOnClickListener(v -> {
            gotoProfileShower(MED_INFO_F);
        });

        binding.patActivities.setOnClickListener(v -> {
            gotoProfileShower(ACTIVITY_F);
        });

        binding.freqUsedApps.setOnClickListener(v -> {
            if (!Settings.canDrawOverlays(requireContext()) && "xiaomi".equals(Build.MANUFACTURER.toLowerCase(Locale.ROOT))) {
                final Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
                intent.setClassName("com.miui.securitycenter",
                        "com.miui.permcenter.permissions.PermissionsEditorActivity");
                intent.putExtra("extra_pkgname", requireActivity().getPackageName());

                new AlertDialog.Builder(requireContext())
                        .setTitle("Please Enable the additional permissions")
                        .setMessage("You couldn't use App whitelisting feature unless you turn the following permissions on:\n\n1. Display pop-up windows\n2. Open new windows while running in the background")
                        .setPositiveButton("Go to Settings", (dialog, which) -> startActivity(intent))
                        .setNegativeButton("Cancel", ((dialogInterface, i) -> dialogInterface.dismiss()))
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .show();
            }else{
                Intent intent = new Intent(requireActivity(), FUAActivity.class);
                startActivity(intent);
            }
        });

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

        binding.settings.setOnClickListener(v -> {
            Intent intent = new Intent(requireActivity(), PatSettingsActivity.class);
            startActivity(intent);
        });
    }

    private void initViews() {
    }

    private void gotoProfileShower(String which_f) {
        Intent intent = new Intent(requireActivity(), PatientProfileShowerActivity.class);
        intent.putExtra(WHICH_FRAGMENT, which_f);
        startActivity(intent);
    }
}
