package com.example.myapplication.ui;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreference;

import android.os.Bundle;
import android.preference.ListPreference;

import com.example.myapplication.BuildConfig;
import com.example.myapplication.R;
import com.example.myapplication.util.SharedPreferencesHelper;

import java.util.Objects;

public class dfuHostSettingsActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dfu_host_settings);
        if (savedInstanceState == null)
        {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.preferencesLayout, new SettingsFragment())
                    .commit();
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
        {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

    }

    public static class SettingsFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceClickListener
    {
        private Preference prefAppAbt;
        private ListPreference prefDev;
        private SwitchPreference prefShowLog;
        private SharedPreferencesHelper sharedPreferencesHelper;
        @Override
        public boolean onPreferenceClick(@NonNull Preference preference)
        {
            return false;
        }

        @Override
        public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey)
        {
            setPreferencesFromResource(R.xml.dfu_host_preferences, rootKey);
            PrefChkChangeListener chkChangeListener = new PrefChkChangeListener();
            PrefOnClickListener onClickListener = new PrefOnClickListener();

            this.prefAppAbt = findPreference(R.string.PREF_APP_ABOUT_KEY);
            this.prefAppAbt.setSummary(BuildConfig.VERSION_NAME);
            this.prefAppAbt.setOnPreferenceClickListener(onClickListener);
            this.prefShowLog = findPreference(R.string.PREF_LOG_EN_KEY);
            this.prefShowLog.setOnPreferenceChangeListener(chkChangeListener);
            //this.prefDev =
            this.applyPrefToUi();
        }

        public <T extends Preference> T findPreference(@StringRes int res)
        {
            return findPreference(getString(res));
        }

        private void applyPrefToUi()
        {
            if(this.sharedPreferencesHelper  == null)
            {
                this.sharedPreferencesHelper = new SharedPreferencesHelper(requireContext());
            }
            final boolean isLogEn = (boolean) this.sharedPreferencesHelper.getPref(getString(R.string.PREF_LOG_EN),false);
            final int devSel = (int) this.sharedPreferencesHelper.getPref(getString(R.string.PREF_DEV),0);

            this.prefShowLog.setChecked(isLogEn);

//            this.prefDev.setSummary(getResources().getStringArray(R.array.DEV_LIST)[devSel]);
        }//End applyPrefToUi

        private class PrefChkChangeListener implements Preference.OnPreferenceChangeListener
        {
            @Override
            public boolean onPreferenceChange(@NonNull Preference preference, Object newValue)
            {
                if(preference.getKey().compareTo(getString(R.string.PREF_LOG_EN_KEY)) == 0)
                {

                    sharedPreferencesHelper.savePref(getString(R.string.PREF_LOG_EN),(boolean)newValue);
                    return true;
                }
                return false;
            }//End onPreferenceChange
        }

        private class PrefOnClickListener implements Preference.OnPreferenceClickListener
        {
            @Override
            public boolean onPreferenceClick(@NonNull Preference preference)
            {
                return false;
            }
        }
    }
}