/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.taxiservicios.services.catalog;

import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.catalog.HolidayController;
import com.arrebol.taxiservicios.model.catalog.Holiday;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author oscarvargas
 */
@Named("holidayService")
@ViewScoped
public class HolidayService extends TaxiGenericBean implements Serializable {

    public List<Holiday> fillHolidayDatatableService() {
        logger.info("fillHolidayDatatableService");

        List<Holiday> holidays = new ArrayList<>();

        try {
            holidays = getController().fillHolidayDatatable();
        } catch (Exception e) {
            logger.error("fillHolidayDatatable: " + e);

            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("holiday.title"),
                    getBundlePropertyFile().getString("holiday.list.exception.desc"));
        }
        return holidays;
    }

    public void saveHolidayService(Holiday holiday) {
        logger.info("saveHolidayService");
        try {
            if (getController().saveHoliday(holiday, getLoggedUser().getId())) {
                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("holiday.title"),
                        getBundlePropertyFile().getString("holiday.save.success.desc"));
            } else {
                logger.error("deleteHoliday");

                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("holiday.title"),
                        getBundlePropertyFile().getString("holiday.save.fail.desc"));
            }
        } catch (Exception e) {
            logger.error("saveColorService: " + e);

            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("holiday.title"),
                    getBundlePropertyFile().getString("holiday.save.exception.desc"));
        }
    }

    public void deleteHolidayService(Holiday holiday) {
        logger.info("deleteHolidayService");
        try {
            if (getController().deleteHoliday(holiday)) {
                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("holiday.title"),
                        getBundlePropertyFile().getString("holiday.delete.success.desc"));
            } else {
                logger.error("deleteHoliday");

                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("holiday.title"),
                        getBundlePropertyFile().getString("holiday.delete.fail.desc"));
            }
        } catch (Exception e) {
            logger.error("deleteHoliday: " + e);

            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("holiday.title"),
                    getBundlePropertyFile().getString("holiday.delete.exception.desc"));
        }

    }

    private static final long serialVersionUID = -3278997775327507213L;

    final Logger logger = LogManager.getLogger(HolidayService.class);

    private HolidayController controller;

    @PostConstruct
    public void init() {
        loadBundlePropertyFile();
        setController(new HolidayController());
    }

    public HolidayController getController() {
        return controller;
    }

    public void setController(HolidayController controller) {
        this.controller = controller;
    }

}
