package com.app.simplitend.caregiverdashboard.activities;

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.app.simplitend.R;
import com.app.simplitend.databinding.ActivityPatientProfileShowerBinding;
import com.app.simplitend.patientprofile.medicalinfo.MedicalInfoFragment;
import com.app.simplitend.patientprofile.medreminder.ReminderFragment;
import com.app.simplitend.patientprofile.setuproutine.RoutineFragment;
import com.app.simplitend.welcome.welcomepatient.fragments.contacts.AddContactFragment;

public class PatientProfileShowerActivity extends AppCompatActivity {

    // view binding
    protected ActivityPatientProfileShowerBinding binding;

    public static final String PERSONAL_INFO = "personal_info_f";
    public static final String CONTACTS_F = "contacts_f";
    public static final String MED_REMINDER_F = "med_reminder_f";
    public static final String MED_INFO_F = "med_info_f";
    public static final String ACTIVITY_F = "activity_f";

    public static final String WHICH_FRAGMENT = "which_f";

    private Fragment current_fragment;

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

        String which_f = getIntent().getStringExtra(WHICH_FRAGMENT);
        if (which_f == null){
            // which_f is necessary
            Toast.makeText(this, "Couldn't load.", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        Fragment fragment = getFragment(which_f);

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

        addFragment(fragment, which_f);

    }

    @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 Fragment getFragment(String which_f) {
        Fragment fragment = null;

        switch (which_f){
            case CONTACTS_F:
                fragment = new AddContactFragment();
                break;
            case MED_REMINDER_F:
                fragment = new ReminderFragment();
                break;
            case MED_INFO_F:
                fragment = new MedicalInfoFragment();
                break;
            case ACTIVITY_F:
                fragment = new RoutineFragment();
                break;

        }

        return fragment;
    }

    private void addFragment(Fragment fragment, String tag) {
        try {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fcv_profile_shower, fragment, tag)
                    .commitAllowingStateLoss();
        } catch (Exception e) {
            // user may be out of context
        }
    }
}