/*
 * 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.beans.catalog;

import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.util.DateWrapper;
import com.arrebol.taxiservicios.model.catalog.Holiday;
import com.arrebol.taxiservicios.services.catalog.HolidayService;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author oscarvargas
 */
@Named("holidayManager")
@ViewScoped
public class HolidayManager extends TaxiGenericBean implements Serializable {
    
    public void clear(){
        setHolidayDate(DateWrapper.today().asDate());
        setDescription("");
    }

    public void editRow(RowEditEvent event) {
        Holiday holiday = (Holiday) event.getObject();
        if (holiday != null) {
            service.saveHolidayService(holiday);
        }
    }

    public void onRowCancel(RowEditEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Edición Cancelada", ((Holiday) event.getObject()).getDescription());
    }

    public void addRow() {
        Holiday holiday = new Holiday();

        holiday.setHoliday(getHolidayDate());
        holiday.setDescription(getDescription());

        service.saveHolidayService(holiday);
        
        getHolidays().add(holiday);
        
        setHolidayDate(DateWrapper.today().asDate());
        setDescription("");
    }

    public void deleteRow() {
        service.deleteHolidayService(getSelectedHoliday());
        
        getHolidays().remove(getSelectedHoliday());

        setSelectedHoliday(null);
    }

    @PostConstruct
    public void init() {
        setHolidays(service.fillHolidayDatatableService());
    }

    private static final long serialVersionUID = 5498436667467788176L;

    private List<Holiday> holidays;
    private Holiday selectedHoliday;

    private String id;
    private Date holidayDate;
    private String description;

    @ManagedProperty(value = "#{holidayService}")
    private HolidayService service;

    public List<Holiday> getHolidays() {
        return holidays;
    }

    public void setHolidays(List<Holiday> holidays) {
        this.holidays = holidays;
    }

    public Holiday getSelectedHoliday() {
        return selectedHoliday;
    }

    public void setSelectedHoliday(Holiday selectedHoliday) {
        this.selectedHoliday = selectedHoliday;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getHolidayDate() {
        return holidayDate;
    }

    public void setHolidayDate(Date holidayDate) {
        this.holidayDate = holidayDate;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public HolidayService getService() {
        return service;
    }

    public void setService(HolidayService service) {
        this.service = service;
    }

}
