package cz.utb.tadis.charts.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDate;

@Entity
@Table(name = "charts")
public class Chart {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Long authorId;

    @NotBlank
    private String title;

    @NotNull
    private LocalDate fromDate;

    @NotNull
    private LocalDate toDate;

    @Enumerated(EnumType.STRING)
    private ChartType type;

    @NotNull
    private boolean publicChart;

    @NotNull
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    @OneToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "chartDataId")
    private ChartData chartData;

    public Chart() {
    }

    public Chart(Long authorId, @NotBlank String title, @NotNull LocalDate fromDate, @NotNull LocalDate toDate, ChartType type, @NotNull boolean publicChart, @NotNull ChartData chartData) {
        this.authorId = authorId;
        this.title = title;
        this.fromDate = fromDate;
        this.toDate = toDate;
        this.type = type;
        this.publicChart = publicChart;
        this.chartData = chartData;
    }

    public Long getId() {
        return id;
    }

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

    public Long getAuthorId() {
        return authorId;
    }

    public void setAuthorId(Long authorId) {
        this.authorId = authorId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @JsonFormat(pattern="d.M.YYYY")
    public LocalDate getFromDate() {
        return fromDate;
    }

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
    public void setFromDate(LocalDate fromDate) {
        this.fromDate = fromDate;
    }

    @JsonFormat(pattern="d.M.YYYY")
    public LocalDate getToDate() {
        return toDate;
    }

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
    public void setToDate(LocalDate toDate) {
        this.toDate = toDate;
    }

    public ChartType getType() {
        return type;
    }

    public void setType(ChartType type) {
        this.type = type;
    }

    public boolean isPublicChart() {
        return publicChart;
    }

    public void setPublicChart(boolean publicChart) {
        this.publicChart = publicChart;
    }

    public ChartData getChartData() {
        return chartData;
    }

    public void setChartData(ChartData chartData) {
        this.chartData = chartData;
    }

}
