package Common.Visual.Windows;
import Common.Utils.Utils_;
import Common.Visual.SavedForm;
import Common.Visual.Themes.ThemeElement;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public abstract class Form extends JFrame implements ThemeElement, SavedForm {
    public Form() {
        if (getIconPath() != null) setIconImage(Utils_.getIcon(getIconPath()).getImage());
        SetListener();
        this.setTitle(Utils_.isWindows() ? getWTitleText() : getUTitleText());
        pack();
        setMinimumSize(new Dimension(getDefaultWidth(), getDefaultHeight()));
    }
    abstract protected JPanel getMainPanel();
    public String getIconPath() {
        return null;
    }
    public String getWTitleText() {
        return "";
    }
    public String getUTitleText() {
        return "";
    }
    protected void SetListener() {
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                Close();
            }
        });
    }
    @Override
    public int getDefaultWidth() {
        return 800;
    }
    @Override
    public int getDefaultHeight() {
        return 450;
    }
    /*
     *вызывать после перегрузки, чтобы отобразить окно.
     */
    public Component getRelative() {
        return null;
    }
    public void Show() {
        try {
            LoadWindowParameters();
        } catch (Exception e) {
            Utils_.MainLog.PrintException(e);
        }
        setContentPane(getMainPanel());
        setVisible(true);
    }
    public void Close() {
        try {
            SaveWindowParameters();
        } catch (Exception e) {
            Utils_.MainLog.PrintException(e);
        }
        setVisible(false);
        dispose();
        AfterClose();
    }
    public void AfterClose() {
    }
    @Override
    public void applyTheme() {
        //todo -> применение темы.
    }
}
