package com.example.java.sample;



import static androidx.navigation.fragment.FragmentKt.findNavController;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.MenuHost;
import androidx.core.view.MenuProvider;
import androidx.lifecycle.Lifecycle;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import com.seikoinstruments.sdk.thermalprinter.PrinterManager;

import java.util.Locale;
import java.util.Objects;

public class SettingFragment extends PreferenceFragmentCompat {
    protected static final int SEND_TIMEOUT_MIN = 100;
    protected static final int SEND_TIMEOUT_MAX = 90000;
    protected static final int SEND_TIMEOUT_DEFAULT = 10000;
    protected static final int RECEIVE_TIMEOUT_MIN = 100;
    protected static final int RECEIVE_TIMEOUT_MAX = 90000;
    protected static final int RECEIVE_TIMEOUT_DEFAULT = 10000;
    protected static final int SOCKET_KEEPING_TIME_MIN = 60000;
    protected static final int SOCKET_KEEPING_TIME_MAX = 300000;
    protected static final int SOCKET_KEEPING_TIME_DEFAULT = 300000;

    private int mSendTimeout = SEND_TIMEOUT_DEFAULT;
    private int mReceiveTimeout = RECEIVE_TIMEOUT_DEFAULT;
    private int mSocketKeepingTime = SOCKET_KEEPING_TIME_DEFAULT;
    private int mCodePage = PrinterManager.CODE_PAGE_1252;
    private int mInternationalCharacter = PrinterManager.COUNTRY_USA;
    private int mSecureConnection = 0;

    private OnBackPressedCallback backPressedCallback = null;


    // Define the behavior when a button on the toolbar is pressed.
    private MenuProvider menuProvider = new MenuProvider() {
        @Override
        public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
            menuInflater.inflate(R.menu.tool_bar_menu, menu);
            menu.removeGroup(0);
        }

        @Override
        public boolean onMenuItemSelected(@NonNull MenuItem item) {
            if (item.getItemId() == android.R.id.home) {
                // Handle the back button event
                setSettings();
                requireActivity().removeMenuProvider(menuProvider);
                return true;
            }
            return false;
        }
    };

    // Define actions when various configuration items are changed in event listener.
    private final SharedPreferences.OnSharedPreferenceChangeListener mListener =
            new SharedPreferences.OnSharedPreferenceChangeListener() {
                public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

                    if (getString(R.string.key_send_timeout).equals(key)) {
                        // send timeout
                        EditTextPreference txtSendTimeout
                                = (EditTextPreference) getPreferenceScreen().findPreference(getString(R.string.key_send_timeout));
                        if (txtSendTimeout != null) {
                            String strSendTimeout = txtSendTimeout.getText();
                            int intSendTimeout = StringUtil.getInt(strSendTimeout);
                            int validSendTimeout = setSendTimeout(intSendTimeout);
                            if (intSendTimeout != validSendTimeout) {
                                mSendTimeout = validSendTimeout;
                                txtSendTimeout.setText(Integer.toString(validSendTimeout));
                                Toast.makeText(getActivity(), R.string.invalid_value, Toast.LENGTH_SHORT).show();
                            } else {
                                mSendTimeout = intSendTimeout;
                                txtSendTimeout.setSummary(txtSendTimeout.getText() + getString(R.string.timeout_unit));
                            }
                        }

                    } else if (getString(R.string.key_receive_timeout).equals(key)) {
                        // receive timeout
                        EditTextPreference txtReceiveTimeout
                                = (EditTextPreference) getPreferenceScreen().findPreference(getString(R.string.key_receive_timeout));
                        if (txtReceiveTimeout != null) {
                            String strReceiveTimeout = txtReceiveTimeout.getText();
                            int intReceiveTimeout = StringUtil.getInt(strReceiveTimeout);
                            int validReceiveTimeout = setReceiveTimeout(intReceiveTimeout);
                            if (intReceiveTimeout != validReceiveTimeout) {
                                mReceiveTimeout = validReceiveTimeout;
                                txtReceiveTimeout.setText(Integer.toString(validReceiveTimeout));
                                Toast.makeText(getActivity(), R.string.invalid_value, Toast.LENGTH_SHORT).show();
                            } else {
                                mReceiveTimeout = intReceiveTimeout;
                                txtReceiveTimeout.setSummary(txtReceiveTimeout.getText() + getString(R.string.timeout_unit));
                            }
                        }

                    } else if (getString(R.string.key_socket_keeping_time).equals(key)) {
                        // socket keeping time
                        EditTextPreference txtSocketKeepingTime
                                = (EditTextPreference) getPreferenceScreen().findPreference(getString(R.string.key_socket_keeping_time));
                        if (txtSocketKeepingTime != null) {
                            String strSocketKeepingTime = txtSocketKeepingTime.getText();
                            int intSocketKeepingTime = StringUtil.getInt(strSocketKeepingTime);
                            int validSocketKeepingTime = setSocketKeepingTime(intSocketKeepingTime);
                            if (intSocketKeepingTime != validSocketKeepingTime) {
                                mSocketKeepingTime = validSocketKeepingTime;
                                txtSocketKeepingTime.setText(Integer.toString(validSocketKeepingTime));
                                Toast.makeText(getActivity(), R.string.invalid_value, Toast.LENGTH_SHORT).show();
                            } else {
                                mSocketKeepingTime = intSocketKeepingTime;
                                txtSocketKeepingTime.setSummary(txtSocketKeepingTime.getText() + getString(R.string.timeout_unit));
                            }
                        }

                    } else if (getString(R.string.key_international_character).equals(key)) {
                        // international character
                        ListPreference LstInternationalCharacter
                                = (ListPreference) getPreferenceScreen().findPreference(getString(R.string.key_international_character));
                        if (LstInternationalCharacter != null) {
                            String strInternationalCharacter = LstInternationalCharacter.getValue();
                            int intInternationalCharacter = StringUtil.getInt(strInternationalCharacter);
                            int validInternationalCharacter = setInternationalCharacter(intInternationalCharacter);
                            if (intInternationalCharacter != validInternationalCharacter) {
                                mInternationalCharacter = validInternationalCharacter;
                                LstInternationalCharacter.setValue(Integer.toString(validInternationalCharacter));
                                Toast.makeText(getActivity(), R.string.invalid_value, Toast.LENGTH_SHORT).show();
                            } else {
                                mInternationalCharacter = intInternationalCharacter;
                                LstInternationalCharacter.setSummary(LstInternationalCharacter.getEntry());
                            }
                        }

                    } else if (getString(R.string.key_code_page).equals(key)) {
                        // code page
                        ListPreference LstCodePage
                                = (ListPreference) getPreferenceScreen().findPreference(getString(R.string.key_code_page));
                        if (LstCodePage != null) {
                            String strCodePage = LstCodePage.getValue();
                            int intCodePage = StringUtil.getInt(strCodePage);
                            int validCodePage = setCodePage(intCodePage);
                            if (intCodePage != validCodePage) {
                                mCodePage = validCodePage;
                                LstCodePage.setValue(Integer.toString(validCodePage));
                                Toast.makeText(getActivity(), R.string.invalid_value, Toast.LENGTH_SHORT).show();
                            } else {
                                mCodePage = intCodePage;
                                LstCodePage.setSummary(LstCodePage.getEntry());
                            }
                        }
                    } else if (getString(R.string.key_secure_connection).equals(key)) {
                        // secure connection
                        ListPreference LstSecureConnection
                                = (ListPreference) getPreferenceScreen().findPreference(getString(R.string.key_secure_connection));
                        if (LstSecureConnection != null) {
                            String strSecureConnection = LstSecureConnection.getValue();
                            int intSecureConnection = StringUtil.getInt(strSecureConnection);
                            int validSecureConnection = setSecureConnection(intSecureConnection);
                            if (intSecureConnection != validSecureConnection) {
                                mSecureConnection = validSecureConnection;
                                LstSecureConnection.setValue(Integer.toString(validSecureConnection));
                                Toast.makeText(getActivity(), R.string.invalid_value, Toast.LENGTH_SHORT).show();
                            } else {
                                mSecureConnection = intSecureConnection;
                                LstSecureConnection.setSummary(LstSecureConnection.getEntry());
                            }
                        }
                    }
                }
            };


    @Override
    public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
        setPreferencesFromResource(R.xml.preference, rootKey);

        // Editor reflects current values retrieved from SDK.
        Bundle bundle = getArguments();
        mSocketKeepingTime = bundle.getInt(MainActivity.BUNDLE_KEY_SOCKET_KEEPING_TIME);
        mSendTimeout = bundle.getInt(MainActivity.BUNDLE_KEY_SEND_TIMEOUT);
        mReceiveTimeout = bundle.getInt(MainActivity.BUNDLE_KEY_RECEIVE_TIMEOUT);
        mCodePage = bundle.getInt(MainActivity.BUNDLE_KEY_CODE_PAGE);
        mInternationalCharacter = bundle.getInt(MainActivity.BUNDLE_KEY_INTERNATION_CHAR);
        mSecureConnection = bundle.getInt(MainActivity.BUNDLE_KEY_SECURE_CONNECTION);

        // From here, initial settings for various configuration items are made.
        final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
        SharedPreferences.Editor editor = pref.edit();

        // send timeout
        EditTextPreference txtSendTimeout
                = (EditTextPreference) getPreferenceScreen().findPreference(getString(R.string.key_send_timeout));
        if (txtSendTimeout != null) {
            String sendTimeout = String.valueOf(mSendTimeout);
            if (StringUtil.isEmpty(sendTimeout)) {
                sendTimeout = getString(R.string.send_timeout_default);
                editor.putString(getString(R.string.key_send_timeout), sendTimeout);
            } else {
                int intSendTimeout = StringUtil.getInt(sendTimeout);
                int validSendTimeout = setSendTimeout(intSendTimeout);
                if (intSendTimeout != validSendTimeout) {
                    sendTimeout = Integer.toString(validSendTimeout);
                }
                editor.putString(getString(R.string.key_send_timeout), sendTimeout);
                txtSendTimeout.setText(sendTimeout);
            }
            mSendTimeout = Integer.parseInt(sendTimeout);
            txtSendTimeout.setSummary(sendTimeout + getString(R.string.timeout_unit));
            txtSendTimeout.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                }
            });
        }

        // receive timeout
        EditTextPreference txtReceiveTimeout
                = (EditTextPreference) getPreferenceScreen().findPreference(getString(R.string.key_receive_timeout));
        if (txtReceiveTimeout != null) {
            String receiveTimeout = String.valueOf(mReceiveTimeout);
            if (StringUtil.isEmpty(receiveTimeout)) {
                receiveTimeout = getString(R.string.receive_timeout_default);
                editor.putString(getString(R.string.key_receive_timeout), receiveTimeout);
            } else {
                int intReceiveTimeout = StringUtil.getInt(receiveTimeout);
                int validReceiveTimeout = setReceiveTimeout(intReceiveTimeout);
                if (intReceiveTimeout != validReceiveTimeout) {
                    receiveTimeout = Integer.toString(validReceiveTimeout);
                }
                editor.putString(getString(R.string.key_receive_timeout), receiveTimeout);
                txtReceiveTimeout.setText(receiveTimeout);
            }
            mReceiveTimeout = Integer.parseInt(receiveTimeout);
            txtReceiveTimeout.setSummary(receiveTimeout + getString(R.string.timeout_unit));
            txtReceiveTimeout.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                }
            });
        }

        // socket keeping time
        EditTextPreference txtSocketKeepingTime
                = (EditTextPreference) getPreferenceScreen().findPreference(getString(R.string.key_socket_keeping_time));
        if (txtSocketKeepingTime != null) {
            String socketKeepingTime = String.valueOf(mSocketKeepingTime);
            if (StringUtil.isEmpty(socketKeepingTime)) {
                socketKeepingTime = getString(R.string.socket_keeping_time_default);
                editor.putString(getString(R.string.key_socket_keeping_time), socketKeepingTime);
            } else {
                int intSocketKeepingTime = StringUtil.getInt(socketKeepingTime);
                int validSocketKeepingTime = setSocketKeepingTime(intSocketKeepingTime);
                if (intSocketKeepingTime != validSocketKeepingTime) {
                    socketKeepingTime = Integer.toString(validSocketKeepingTime);
                }
                editor.putString(getString(R.string.key_socket_keeping_time), socketKeepingTime);
                txtSocketKeepingTime.setText(socketKeepingTime);
            }
            mSocketKeepingTime = Integer.parseInt(socketKeepingTime);
            txtSocketKeepingTime.setSummary(socketKeepingTime + getString(R.string.timeout_unit));
            txtSocketKeepingTime.setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
                @Override
                public void onBindEditText(@NonNull EditText editText) {
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                }
            });
        }

        // international character
        ListPreference LstInternationalCharacter
                = (ListPreference) getPreferenceScreen().findPreference(getString(R.string.key_international_character));
        if (LstInternationalCharacter != null) {
            CharSequence internationalCharacter = getInternationalCharacterString(mInternationalCharacter);
            if (StringUtil.isEmpty(internationalCharacter)) {
                if (Locale.JAPAN.equals(Locale.getDefault())) {
                    internationalCharacter = getString(R.string.international_character_default_jp);
                    editor.putString(getString(R.string.key_international_character), getString(R.string.international_character_default_jp_values));
                    mInternationalCharacter = Integer.parseInt(getString(R.string.international_character_default_jp_values));
                } else {
                    internationalCharacter = getString(R.string.international_character_default_en);
                    editor.putString(getString(R.string.key_international_character), getString(R.string.international_character_default_en_values));
                    mInternationalCharacter = Integer.parseInt(getString(R.string.international_character_default_en_values));
                }
            } else {
                String strInternationalCharacter = String.valueOf(mInternationalCharacter);
                int intInternationalCharacter = StringUtil.getInt(strInternationalCharacter);
                int validInternationalCharacter = setInternationalCharacter(intInternationalCharacter);
                if (intInternationalCharacter != validInternationalCharacter) {
                    internationalCharacter = getInternationalCharacterString(validInternationalCharacter);
                    editor.putString(getString(R.string.key_international_character), Integer.toString(validInternationalCharacter));
                    LstInternationalCharacter.setValue(Integer.toString(validInternationalCharacter));
                    mInternationalCharacter = validInternationalCharacter;
                } else {
                    editor.putString(getString(R.string.key_international_character), Integer.toString(intInternationalCharacter));
                    LstInternationalCharacter.setValue(Integer.toString(intInternationalCharacter));
                }
            }
            LstInternationalCharacter.setSummary(internationalCharacter);
        }

        // code page
        ListPreference LstCodePage
                = (ListPreference) getPreferenceScreen().findPreference(getString(R.string.key_code_page));
        if (LstCodePage != null) {
            CharSequence codePage = getCodePageString(mCodePage);
            if (StringUtil.isEmpty(codePage)) {
                if (Locale.JAPAN.equals(Locale.getDefault())) {
                    codePage = getString(R.string.code_page_default_jp);
                    editor.putString(getString(R.string.key_code_page), getString(R.string.code_page_default_jp_values));
                    mCodePage = Integer.parseInt(getString(R.string.code_page_default_jp_values));
                } else {
                    codePage = getString(R.string.code_page_default_en);
                    editor.putString(getString(R.string.key_code_page), getString(R.string.code_page_default_en_values));
                    mCodePage = Integer.parseInt(getString(R.string.code_page_default_en_values));
                }
            } else {
                String strCodePage = String.valueOf(mCodePage);
                int intCodePage = StringUtil.getInt(strCodePage);
                int validCodePage = setCodePage(intCodePage);
                if (intCodePage != validCodePage) {
                    codePage = getCodePageString(validCodePage);
                    editor.putString(getString(R.string.key_code_page), Integer.toString(validCodePage));
                    LstCodePage.setValue(Integer.toString(validCodePage));
                    mCodePage = validCodePage;
                } else {
                    editor.putString(getString(R.string.key_code_page), Integer.toString(intCodePage));
                    LstCodePage.setValue(Integer.toString(intCodePage));
                }
            }
            LstCodePage.setSummary(codePage);
        }

        // secure connection
        ListPreference LstSecureConnection
                = (ListPreference) getPreferenceScreen().findPreference(getString(R.string.key_secure_connection));
        if (LstSecureConnection != null) {
            CharSequence secureConnection = getSecureConnectionString(mSecureConnection);
            if (StringUtil.isEmpty(secureConnection)) {
                secureConnection = getString(R.string.secure_connection_default);
                editor.putString(getString(R.string.key_secure_connection), getString(R.string.secure_connection_default_values));
                mSecureConnection = Integer.parseInt(getString(R.string.secure_connection_default_values));
            } else {
                String strSecureConnection = String.valueOf(mSecureConnection);
                int intSecureConnection = StringUtil.getInt(strSecureConnection);
                int validSecureConnection = setSecureConnection(intSecureConnection);
                if (intSecureConnection != validSecureConnection) {
                    secureConnection = getSecureConnectionString(validSecureConnection);
                    editor.putString(getString(R.string.key_secure_connection), Integer.toString(validSecureConnection));
                    LstSecureConnection.setValue(Integer.toString(validSecureConnection));
                    mSecureConnection = validSecureConnection;
                } else {
                    editor.putString(getString(R.string.key_secure_connection), Integer.toString(intSecureConnection));
                    LstSecureConnection.setValue(Integer.toString(intSecureConnection));
                }
            }
            LstSecureConnection.setSummary(secureConnection);
        }

        editor.commit();
    }


    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if(!isAdded() ){
            return;
        }

        // This callback will only be called when MyFragment is at least Started.
        backPressedCallback = new OnBackPressedCallback(true /* enabled by default */) {
            @Override
            public void handleOnBackPressed() {
                // Handle the back button event.
                setSettings();
                requireActivity().removeMenuProvider(menuProvider);
            }
        };
        requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), backPressedCallback);

        // Set MenuProvider
        MenuHost menuHost = requireActivity();
        menuHost.addMenuProvider(menuProvider, getViewLifecycleOwner(), Lifecycle.State.RESUMED);
    }


    @Override
    public void onResume() {
        super.onResume();
        Objects.requireNonNull(getPreferenceScreen().getSharedPreferences()).registerOnSharedPreferenceChangeListener(mListener);
    }


    @Override
    public void onPause() {
        super.onPause();
        Objects.requireNonNull(getPreferenceScreen().getSharedPreferences()).unregisterOnSharedPreferenceChangeListener(mListener);
    }


    private int setSendTimeout(int sendTimeout) {
        int time;
        if (sendTimeout < SEND_TIMEOUT_MIN || sendTimeout > SEND_TIMEOUT_MAX) {
            time = SEND_TIMEOUT_DEFAULT;
        } else {
            time = sendTimeout;
        }

        return time;
    }


    private int setReceiveTimeout(int receiveTimeout) {
        int time;
        if (receiveTimeout < RECEIVE_TIMEOUT_MIN || receiveTimeout > RECEIVE_TIMEOUT_MAX) {
            time = RECEIVE_TIMEOUT_DEFAULT;
        } else {
            time = receiveTimeout;
        }

        return time;
    }


    private int setSocketKeepingTime(int socketKeepingTime) {
        int time;
        if (socketKeepingTime < SOCKET_KEEPING_TIME_MIN || socketKeepingTime > SOCKET_KEEPING_TIME_MAX) {
            time = SOCKET_KEEPING_TIME_DEFAULT;
        } else {
            time = socketKeepingTime;
        }

        return time;
    }


    private int setInternationalCharacter(int internationalCharacter) {
        int international;
        international = internationalCharacter;

        return international;
    }


    private int setCodePage(int codePage) {
        int code;
        code = codePage;

        return code;
    }

    private int setSecureConnection(int secureConnection) {
        int secure;
        secure = secureConnection;

        return secure;
    }


    private String getInternationalCharacterString(int code) {
        String internationalCharacter;
        String[] internationalCharacterList = getResources().getStringArray(R.array.international_character_list);
        switch (code) {
            case PrinterManager.COUNTRY_USA:
                internationalCharacter = internationalCharacterList[0];
                break;

            case PrinterManager.COUNTRY_FRANCE:
                internationalCharacter = internationalCharacterList[1];
                break;

            case PrinterManager.COUNTRY_GERMANY:
                internationalCharacter = internationalCharacterList[2];
                break;

            case PrinterManager.COUNTRY_ENGLAND:
                internationalCharacter = internationalCharacterList[3];
                break;

            case PrinterManager.COUNTRY_DENMARK_1:
                internationalCharacter = internationalCharacterList[4];
                break;

            case PrinterManager.COUNTRY_SWEDEN:
                internationalCharacter = internationalCharacterList[5];
                break;

            case PrinterManager.COUNTRY_ITALY:
                internationalCharacter = internationalCharacterList[6];
                break;

            case PrinterManager.COUNTRY_SPAIN:
                internationalCharacter = internationalCharacterList[7];
                break;

            case PrinterManager.COUNTRY_JAPAN:
                internationalCharacter = internationalCharacterList[8];
                break;

            case PrinterManager.COUNTRY_NORWAY:
                internationalCharacter = internationalCharacterList[9];
                break;

            case PrinterManager.COUNTRY_DENMARK_2:
                internationalCharacter = internationalCharacterList[10];
                break;

            case PrinterManager.COUNTRY_SPAIN_2:
                internationalCharacter = internationalCharacterList[11];
                break;

            case PrinterManager.COUNTRY_LATIN_AMERICA:
                internationalCharacter = internationalCharacterList[12];
                break;

            case PrinterManager.COUNTRY_ARABIA:
                internationalCharacter = internationalCharacterList[13];
                break;

            default:
                internationalCharacter = "";
                break;
        }

        return internationalCharacter;
    }


    private String getCodePageString(int code) {
        String codePage;
        String[] codePageList = getResources().getStringArray(R.array.code_page_list);
        switch (code) {
            case PrinterManager.CODE_PAGE_437:
                codePage = codePageList[0];
                break;

            case PrinterManager.CODE_PAGE_KATAKANA:
                codePage = codePageList[1];
                break;

            case PrinterManager.CODE_PAGE_850:
                codePage = codePageList[2];
                break;

            case PrinterManager.CODE_PAGE_860:
                codePage = codePageList[3];
                break;

            case PrinterManager.CODE_PAGE_863:
                codePage = codePageList[4];
                break;

            case PrinterManager.CODE_PAGE_865:
                codePage = codePageList[5];
                break;

            case PrinterManager.CODE_PAGE_857:
                codePage = codePageList[6];
                break;

            case PrinterManager.CODE_PAGE_737:
                codePage = codePageList[7];
                break;

            case PrinterManager.CODE_PAGE_1252:
                codePage = codePageList[8];
                break;

            case PrinterManager.CODE_PAGE_866:
                codePage = codePageList[9];
                break;

            case PrinterManager.CODE_PAGE_852:
                codePage = codePageList[10];
                break;

            case PrinterManager.CODE_PAGE_858:
                codePage = codePageList[11];
                break;

            case PrinterManager.CODE_PAGE_855:
                codePage = codePageList[12];
                break;

            case PrinterManager.CODE_PAGE_864:
                codePage = codePageList[13];
                break;

            case PrinterManager.CODE_PAGE_1250:
                codePage = codePageList[14];
                break;

            case PrinterManager.CODE_PAGE_1251:
                codePage = codePageList[15];
                break;

            case PrinterManager.CODE_PAGE_1253:
                codePage = codePageList[16];
                break;

            case PrinterManager.CODE_PAGE_1254:
                codePage = codePageList[17];
                break;

            default:
                codePage = "";
                break;
        }

        return codePage;
    }


    private String getSecureConnectionString(int secure) {
        String secureConnection;
        String[] secureConnectionList = getResources().getStringArray(R.array.secure_connection_list);
        switch (secure) {
            case 0:
                secureConnection = secureConnectionList[0];
                break;

            case 1:
                secureConnection = secureConnectionList[1];
                break;

            default:
                secureConnection = "";
                break;
        }

        return secureConnection;
    }


    private void setSettings(){
        //Send various setting values to the previous fragment.
        Bundle bundle = new Bundle();
        bundle.putInt(MainActivity.BUNDLE_KEY_SOCKET_KEEPING_TIME,mSocketKeepingTime);
        bundle.putInt(MainActivity.BUNDLE_KEY_SEND_TIMEOUT,mSendTimeout);
        bundle.putInt(MainActivity.BUNDLE_KEY_RECEIVE_TIMEOUT,mReceiveTimeout);
        bundle.putInt(MainActivity.BUNDLE_KEY_CODE_PAGE,mCodePage);
        bundle.putInt(MainActivity.BUNDLE_KEY_INTERNATION_CHAR,mInternationalCharacter);
        bundle.putInt(MainActivity.BUNDLE_KEY_SECURE_CONNECTION,mSecureConnection);

        getParentFragmentManager().setFragmentResult(MainActivity.REQUEST_KEY_SETTINGS, bundle);
        findNavController(SettingFragment.this).navigate(R.id.action_SettingsFragment_to_MainFragment);
    }
}