package com.wdkl.ncs.s433.transbox;

import android.os.Bundle;
import android.serialport.SerialPortFinder;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;

public class SettingsActivity extends AppCompatActivity {
    private static Application mApplication;
    private static SerialPortFinder mSerialPortFinder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mApplication = (Application) getApplication();
        mSerialPortFinder = mApplication.mSerialPortFinder;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.settings, new SettingsFragment())
                    .commit();
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);

        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);

            final androidx.preference.ListPreference devices = (androidx.preference.ListPreference) findPreference("serialDeivce");
            String[] entries = mSerialPortFinder.getAllDevices();
            String[] entryValues = mSerialPortFinder.getAllDevicesPath();
            devices.setEntries(entries);
            devices.setEntryValues(entryValues);
        }
    }
}