/*
 * Copyright (c) 2019 - 2026 AB Circle Limited. All rights reserved
 */

package com.abc.bluetoothdemo;

import android.os.Bundle;
import android.text.InputFilter;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceFragmentCompat;

public class PreferenceActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.settings, new SettingsFragment())
                .commit();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        // AB Circle Logo
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.mipmap.logo_04);
    }

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

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

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            EditTextPreference encryptionKeyPreference = findPreference("custom_encryption_key");
            encryptionKeyPreference.setOnBindEditTextListener(editText ->
                    editText.setFilters(new InputFilter[]{
                            (source, start, end, dest, dstart, dend) ->
                            {
                                for (int i = start; i < end; i++) {
                                    if (Character.digit(source.charAt(i), 16) == -1) {
                                        return "";
                                    }
                                }
                                return null;
                            },
                            new InputFilter.LengthFilter(32)
                    }));

            EditTextPreference rssiPreference = findPreference("rssi_filter");
            rssiPreference.setOnBindEditTextListener(editText ->
                    editText.setFilters(new InputFilter[]{
                            (source, start, end, dest, dstart, dend) ->
                            {
                                for (int i = start; i < end; i++) {
                                    if (Character.digit(source.charAt(i), 10) == -1) {
                                        return "";
                                    }
                                }
                                return null;
                            },
                            new InputFilter.LengthFilter(5)
                    }));

        }



    }
}