package danielcortes.xyz.controllers;

import danielcortes.xyz.views.BaseLayout;
import danielcortes.xyz.views.CajasView;
import danielcortes.xyz.views.EstadoResultadoView;
import danielcortes.xyz.views.InformesSideBar;
import danielcortes.xyz.views.MainSideBar;
import java.awt.CardLayout;

public class BaseLayoutController extends BaseController {

  private final String MAIN_SIDEBAR = "MAIN";
  private final String INFORMES_SIDEBAR = "INFORMES";
  private final String CAJAS_MAIN = "CAJAS";
  private final String ESTADO_RESULTADO_MAIN = "ESTADO_RESULTADO";

  private BaseLayout baseLayoutView;

  private MainSideBarController mainSideBarController;
  private InformesSideBarController informesSideBarController;

  private CajasController cajasController;
  private EstadoResultadoController estadoResultadoController;

  public BaseLayoutController(BaseLayout baseLayoutView) {
    this.baseLayoutView = baseLayoutView;
    this.loadSideBarContents();
    this.loadMainPanelContents();
    this.setupViewEvents();
  }

  private void setupViewEvents() {
    CardLayout sideLayout = (CardLayout) this.baseLayoutView.getSidePanel().getLayout();
    CardLayout mainLayout = (CardLayout) this.baseLayoutView.getMainPanel().getLayout();

    this.mainSideBarController.getView().getInformesMensualesButton().addActionListener(
        e -> sideLayout.show(this.baseLayoutView.getSidePanel(), INFORMES_SIDEBAR)
    );
    this.mainSideBarController.getView().getCajasButton().addActionListener(
        e -> mainLayout.show(this.baseLayoutView.getMainPanel(), CAJAS_MAIN)
    );

    this.informesSideBarController.getView().getVolverButton().addActionListener(
        e -> sideLayout.show(this.baseLayoutView.getSidePanel(), MAIN_SIDEBAR)
    );

    this.informesSideBarController.getView().getEstadoResultadoButton().addActionListener(e -> {
      this.estadoResultadoController.update();
      mainLayout.show(this.baseLayoutView.getMainPanel(), ESTADO_RESULTADO_MAIN);
    });

  }

  private void loadSideBarContents() {
    this.loadMainSideBar();
    this.loadInformesSideBar();
  }

  private void loadMainSideBar() {
    MainSideBar mainSideBar = new MainSideBar();
    this.mainSideBarController = new MainSideBarController(mainSideBar);
    this.baseLayoutView.getSidePanel().add(mainSideBar.getContentPanel(), MAIN_SIDEBAR);
  }

  private void loadInformesSideBar() {
    InformesSideBar informesSideBar = new InformesSideBar();
    this.informesSideBarController = new InformesSideBarController(informesSideBar);
    this.baseLayoutView.getSidePanel().add(informesSideBar.getContentPanel(), INFORMES_SIDEBAR);
  }

  private void loadMainPanelContents() {
    this.loadCajasMainContent();
    this.loadEstadoResultadoMainContent();
  }

  private void loadCajasMainContent() {
    CajasView cajasView = new CajasView();
    this.cajasController = new CajasController(cajasView);
    this.baseLayoutView.getMainPanel().add(cajasView.getContentPanel(), CAJAS_MAIN);
  }

  private void loadEstadoResultadoMainContent() {
    EstadoResultadoView estadoResultadoView = new EstadoResultadoView();
    this.estadoResultadoController = new EstadoResultadoController(estadoResultadoView);
    this.baseLayoutView.getMainPanel()
        .add(estadoResultadoView.getContentPanel(), ESTADO_RESULTADO_MAIN);
  }

}
