package app.GUI;

import app.DAO.CalendarEvent;
import app.DataUtil;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.timeline.TimelineEvent;
import org.primefaces.model.timeline.TimelineModel;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Petr Flégl on 1.4.2017.
 */
@ManagedBean
@Named

public class CalendarView {

    private Date date;
    private String popis;
    private boolean visible;
    private TimelineModel model;

    @PostConstruct
    public void init(){
        model = new TimelineModel();
        //TODO natahnout data do modelu z DB
    }

    public void onDateSelect(SelectEvent event) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected", format.format(event.getObject())));
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getPopis() {
        return popis;
    }

    public void setPopis(String popis) {
        this.popis = popis;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public TimelineModel getModel() {
        return model;
    }

    public void setModel(TimelineModel model) {
        this.model = model;
    }

    public void save(){
        RequestContext requestContext = RequestContext.getCurrentInstance();
        CalendarEvent event = new CalendarEvent();
        event.setPopis(popis);
        event.setTimeStamp(DataUtil.dateToTimestamp(date));
        event.setVisible(visible);

        if(visible)
            model.add(new TimelineEvent(popis,date));

    }

}
