/*
 * MIT License
 *
 * Copyright (c) 2018-2019 Daniel Cortes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package danielcortes.xyz.controllers;

import danielcortes.xyz.informes.InformeEgresosToExcel;
import danielcortes.xyz.informes.InformeLibroDeVentasToExcel;
import danielcortes.xyz.informes.InformeResumenArqueo;
import danielcortes.xyz.informes.InformeResumenArqueoToExcel;
import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
import danielcortes.xyz.utils.SaveFile;
import danielcortes.xyz.views.InformesSideBar;
import danielcortes.xyz.views.dialogs.MonthSelectDialog;
import danielcortes.xyz.views.dialogs.TipoEgresoSelectDialog;
import java.time.YearMonth;
import org.apache.poi.ss.usermodel.Workbook;

public class InformesSideBarController {

  private InformesSideBar view;

  public InformesSideBarController(InformesSideBar view) {
    this.view = view;
    this.setupViewEvents();
  }

  public InformesSideBar getView() {
    return view;
  }

  private void setupViewEvents() {
    this.view.getInformeLibroDeVentasButton()
        .addActionListener(e -> generarInformeLibroDeVentasListener());
    this.view.getGenerarEgresosFacturasMateriaPrimaButton()
        .addActionListener(e -> generarInformeEgresosListener());
    this.view.getGenerarResumenArqueoButton()
        .addActionListener(e -> generarInformeResumenArqueoListener());
  }

  private void generarInformeLibroDeVentasListener() {
    YearMonth month = new MonthSelectDialog().execute();
    if (month == null) {
      return;
    }

    InformeLibroDeVentasToExcel informe = new InformeLibroDeVentasToExcel(month);
    Workbook wb = informe.generarInforme();

    SaveFile.saveToTempFileAndOpen(wb, "libro_de_ventas");
  }

  private void generarInformeEgresosListener() {
    TipoEgreso tipoEgreso = new TipoEgresoSelectDialog().execute();
    if (tipoEgreso == null) {
      return;
    }

    YearMonth month = new MonthSelectDialog().execute();
    if (month == null) {
      return;
    }

    InformeEgresosToExcel informe = new InformeEgresosToExcel(tipoEgreso, month);
    Workbook wb = informe.generarInforme();

    SaveFile.saveToTempFileAndOpen(wb, "informe_egresos");
  }

  private void generarInformeResumenArqueoListener() {
    YearMonth mes = new MonthSelectDialog().execute();
    if (mes == null) {
      return;
    }

    InformeResumenArqueo informe = InformeResumenArqueo.generate(mes);
    Workbook wb = new InformeResumenArqueoToExcel(informe).generarInforme();

    SaveFile.saveToTempFileAndOpen(wb, "resumen_arqueo");
  }

}
