package danielcortes.xyz.controllers;

import danielcortes.xyz.data.Configuration;
import danielcortes.xyz.views.CajasView;
import danielcortes.xyz.views.InformesView;
import danielcortes.xyz.views.MainView;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class MainController {
    private MainView view;

    public MainController(MainView view){
        this.view = view;
        this.setupViewEvents();
        this.loadRestaurantName();
    }

    private void loadRestaurantName(){
        String nombre = Configuration.get("nombre_caja");
        ((TitledBorder)this.view.getButtonPanel().getBorder()).setTitle("Restaurant: " + nombre);
    }

    private void setupViewEvents(){
        this.view.getCajasButton().addActionListener(e -> {
            CajasView view = new CajasView();
            CajasController cajasController = new CajasController(view);

            this.executeView(view.getContentPanel(), "Caja", new Dimension(1280, 720));
        });
        this.view.getInformesMensualesButton().addActionListener(e -> {
            InformesView view = new InformesView();
            InformesController informesController = new InformesController(view);

            this.executeView(view.getContentPanel(), "Informes Mensuales", new Dimension(250, 500));
        });
    }

    private void executeView(JComponent view, String title, Dimension d){
        JFrame frame = new JFrame(title + ": " + Configuration.get("nombre_caja"));
        frame.setContentPane(view);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.setSize(d);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
