package it.integry.integrywmsnative.gest.settings;

import android.content.Context;
import android.os.Bundle;
import androidx.preference.CheckBoxPreference;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import androidx.appcompat.view.ContextThemeWrapper;
import android.util.TypedValue;

import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.REST.consumers.ISimpleOperationCallback;
import it.integry.integrywmsnative.core.REST.consumers.PrinterRESTConsumer;
import it.integry.integrywmsnative.core.settings.SharedPrefKeys;
import it.integry.integrywmsnative.core.utility.UtilityArray;
import it.integry.integrywmsnative.core.interfaces.ITitledFragment;

import static br.com.zbra.androidlinq.Linq.stream;

public class SettingsPreferenceFragment extends PreferenceFragmentCompat implements ITitledFragment {
    @Override
    public void onCreatePreferences(Bundle bundle, String s) {

        PrinterRESTConsumer.getAvailablePrinters(new ISimpleOperationCallback<List<String>>() {
            @Override
            public void onSuccess(final List<String> availablePrinters) {
                initScreen(availablePrinters);
            }

            @Override
            public void onFailed(Exception ex) {

            }
        });



    }



    @Override
    public String getTitle(Context context) {
        return context.getText(R.string.settings_category).toString();
    }



    private void initScreen(List<String> availablePrinters) {
        // Load the Preferences from the XML file
        Context activityContext = getActivity();

        PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(activityContext);
        setPreferenceScreen(preferenceScreen);

        TypedValue themeTypedValue = new TypedValue();
        activityContext.getTheme().resolveAttribute(R.attr.preferenceTheme, themeTypedValue, true);
        ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(activityContext, themeTypedValue.resourceId);

        // We instance each Preference using our ContextThemeWrapper object
        PreferenceCategory preferenceCategory = new PreferenceCategory(contextThemeWrapper);
        preferenceCategory.setTitle("Category test");

        EditTextPreference editTextPreference = new EditTextPreference(contextThemeWrapper);
        editTextPreference.setKey("edittext");
        editTextPreference.setTitle("EditText test");

        CheckBoxPreference checkBoxPreference = new CheckBoxPreference(contextThemeWrapper);
        checkBoxPreference.setTitle("Checkbox test");
        checkBoxPreference.setKey("checkbox");
        checkBoxPreference.setChecked(true);

        availablePrinters.add(0, "Nessuna");
        String[] availablePritersArray = UtilityArray.listToArray(availablePrinters, String.class);

        ListPreference listPreferece = new ListPreference(contextThemeWrapper);
        listPreferece.setTitle("Current printer");
        listPreferece.setKey(SharedPrefKeys.ACCETTAZIONE_PRINTER);
        listPreferece.setEntries(availablePritersArray);
        listPreferece.setEntryValues(availablePritersArray);

        // It's REALLY IMPORTANT to add Preferences with child Preferences to the Preference Hierarchy first
        // Otherwise, the PreferenceManager will fail to load their keys

        // First we add the category to the root PreferenceScreen
        getPreferenceScreen().addPreference(preferenceCategory);

        // Then their child to it
        preferenceCategory.addPreference(editTextPreference);
        preferenceCategory.addPreference(checkBoxPreference);
        preferenceCategory.addPreference(listPreferece);
    }

}
