package Common;
import Common.Utils.Utils_;
import Common.Visual.Controls.StableMenuItem;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
public class Properties {
    protected LinkedHashMap<String, JMenuItem> controls = new LinkedHashMap<>();
    private File file = null; //файл где хранятся настройки.
    public Properties() {
    }
    public Properties(File file_in) {
        setFile(file_in);
    }
    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }
    public void Update() {
        try {
            Utils_.jsonToFile(this, getFile());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public boolean updateField(String name, Object newValue) {
        try {
            Field field = getClass().getField(name);
            Object oldValue = field.get(this);
            //---
            if (newValue.equals(oldValue))
                return false;
            //--
            field.set(this, newValue);
            Update();
            return true;
            //--
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }
    public String getFieldDescription(String fieldName) {
        return "?";
    }
    public boolean controlAction(String fieldName, JMenuItem control
    ) {
        return false;
    }
    public JMenuItem getMenuItem(String fieldName) {
        final JMenuItem menuItem;
        if (controls.containsKey(fieldName))
            menuItem = controls.get(fieldName);
        else {
            menuItem = new StableMenuItem();
            Mark(fieldName, menuItem);
            menuItem.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (controlAction(fieldName, menuItem))
                        Mark(fieldName, menuItem);
                }
            });
            controls.put(fieldName, menuItem);
        }
        return menuItem;
    }
    public void Mark(String fieldName, JMenuItem control) {
        String description = getFieldDescription(fieldName);
        try {
            Object value = this.getClass().getField(fieldName).get(this);
            if (value instanceof Boolean) {
                Boolean flag = (Boolean) value;
                control.setText(description);
                control.setIcon(Utils_.getIcon(flag ? "/Common/icons/Pick.png" : "/Common/icons/NotPick.png"));
            } else {
                control.setText(description + " : " + value);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public void setControlVisible(String settingName, boolean flag) {
        if (controls.containsKey(settingName)) {
            controls.get(settingName).setVisible(flag);
        }
    }
}
