package Common.Visual;
import Common.Database.Objects.DBForm.DBForm;
import Common.MainModule_;

import java.awt.*;
public interface SavedForm {
    default String getFormKey() {
        return null;
    }
    int getDefaultWidth();
    int getDefaultHeight();
    default void LoadWindowParameters() {
        if (this instanceof Window) {
            Window window = (Window) this;
            if ((getFormKey() != null) && MainModule_.instance.getDb().forms.Data.containsKey(getFormKey())) {
                DBForm dbForm = MainModule_.instance.getDb().forms.Data.get(getFormKey());
                dbForm.Apply(window);
                return;
            } else {
                window.setSize(getDefaultWidth(), getDefaultHeight());
            }
        }
    }
    default void SaveWindowParameters() {
        if ((this instanceof Window) && (getFormKey() != null)) {
            Window window = (Window) this;
            DBForm dbForm = null;
            try {
                if (MainModule_.instance.getDb().forms.containsKey(getFormKey())) {
                    dbForm = MainModule_.instance.getDb().forms.get(getFormKey());
                    dbForm.Fill(window);
                    MainModule_.instance.getDb().Update(dbForm);
                } else {
                    dbForm = new DBForm(getFormKey(), window);
                    MainModule_.instance.getDb().Insert(dbForm);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
