package com.tomastc.anickd;

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.keyence.autoid.sdk.scan.DecodeResult;
import com.tomastc.anickd.lib.Scanner;
import com.tomastc.anickd.lib.Settings;
import com.tomastc.anickd.lib.common.MyAlertDialog;
import com.tomastc.anickd.lib.common.Utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class PrinterSettingActivity extends Scanner {
    TextView txt_printer_name;
    Button btn_cancel, btn_save;

    public static Map<Integer, String> prop_map = new HashMap<>();

    public static final Map<Integer, String> prop_key = new HashMap<Integer, String>() {{
        put(R.id.txt_printer_name, "printer");
    }};

    private void init() {
        prop_map = new HashMap<Integer, String>() {{
            put(R.id.txt_printer_name, Settings.getPrinter());
        }};

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_printer_setting);
        default_view = (EditText) findViewById(R.id.txt_label_issue_tmp);
        init();

        txt_printer_name = (TextView)findViewById(R.id.txt_printer_name);
        txt_printer_name.setText(Settings.getPrinter());
        txt_printer_name.setTextColor(getResources().getColor(R.color.black));

        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btn_save = (Button) findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (txt_printer_name.getText().toString().equals(""))
                    return;

                Map<String, String> map = Settings.getSettingList();
                Iterator iter = prop_map.keySet().iterator();
                while (iter.hasNext()) {
                    Integer key = (Integer) iter.next();
                    map.put(prop_key.get(key), ((TextView) findViewById((int)key)).getText().toString());
                }
                Settings.updateSettingsFile(map, getApplicationContext().getFilesDir().getAbsolutePath());

                Settings.readSettingFile(getApplicationContext().getFilesDir());
                new MyAlertDialog(PrinterSettingActivity.this, "Alert",
                        "Settings saved.",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        });
            }
        });
    }

    @Override
    public void processData(String data, DecodeResult.Result result, String codeType) {
        if (data.trim().equals(""))
            return;

        try {
            Log.d(getString(R.string.tag), "data read:" + data);

            txt_printer_name.setText(data);
            txt_printer_name.setTextColor(getResources().getColor(R.color.gray));


            Utils.hideKeyboardFrom(getApplicationContext(), default_view);

        }catch (Exception e) {
            Log.e(getString(R.string.tag), "ERROR: " + e.getMessage());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {

    }
}