/*
 * 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 plate.wellmann.shop.persistence;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;

/**
 *
 * @author Patrick
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Item.findByID",
            query = "SELECT t FROM Item t WHERE t.id = :id"),
    @NamedQuery(name = "Item.findByUser",
            query = "SELECT t FROM Item t WHERE t.creator = :creator")
})
public class Item extends AbstractEntity {

    private String name;
    
    private String datacheck;
    
    private Paper paper;

    @OneToMany(mappedBy = "item", fetch = FetchType.LAZY)
    private List<Color> colors;
    
    @ManyToOne
    private Customer creator;

    private Customer manager;
    
    private BigInteger quantity;
    
    private BigInteger price;

    @OneToMany(mappedBy = "item", fetch = FetchType.LAZY)
    private List<Comment> comments;

    @NotNull
    @Enumerated(EnumType.STRING)
    private TicketStatus status;
    
    @NotNull
    @Enumerated(EnumType.STRING)
    private PayMethod payMethod;

    private final LocalDateTime creationDate;

    private LocalDateTime lastChangeDate;
    
    @ManyToMany(fetch = FetchType.EAGER)
    private List<Customer> watchers;

    @OneToMany(mappedBy = "ticket", fetch = FetchType.LAZY)
    private List<Log> log;

    private boolean payed;
    
    private Format format;
    
    private String finishing;
    
    private boolean roundedCorners;
    
    private int numberAdresses;
    
    public enum PayMethod {
        BILL,
        AHEAD,
        CREDITCARD,
        PAYPAL
    }

    public enum TicketStatus {
        NEW,
        PENDING,
        ACTIVE,
        CLOSED,
        REOPENED
    }

    public Item(LocalDateTime creationDate) {
        this.creationDate = creationDate;
        this.status = TicketStatus.NEW;
    }

    public BigInteger getQuantity() {
        return quantity;
    }

    public void setQuantity(BigInteger quantity) {
        this.quantity = quantity;
    }

    public Paper getPaper() {
        return paper;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }

    public List<Color> getColors() {
        return colors;
    }

    public void setColors(List<Color> colors) {
        this.colors = colors;
    }

    public Customer getCreator() {
        return creator;
    }

    public void setCreator(Customer customer) {
        this.creator = customer;
    }

    public Customer getManager() {
        return manager;
    }

    public void setManager(Customer manager) {
        this.manager = manager;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    public TicketStatus getStatus() {
        return status;
    }

    public void setStatus(TicketStatus status) {
        this.status = status;
    }

    public LocalDateTime getLastChangeDate() {
        return lastChangeDate;
    }

    public void setLastChangeDate(LocalDateTime lastChangeDate) {
        this.lastChangeDate = lastChangeDate;
    }

    public List<Customer> getWatchers() {
        return watchers;
    }

    public void setWatchers(List<Customer> watchers) {
        this.watchers = watchers;
    }

    public LocalDateTime getCreationDate() {
        return creationDate;
    }

    public List<Log> getLog() {
        return log;
    }

    public void addLogEntry(String entry) {
        if (log == null) {
            log = new ArrayList<>();
        }
        log.add(new Log(entry, this));
    }

    public boolean isPayed() {
        return payed;
    }

    public void setPayed(boolean payed) {
        this.payed = payed;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigInteger getPrice() {
        return price;
    }

    public PayMethod getPayMethod() {
        return payMethod;
    }

    public void setPayMethod(PayMethod payMethod) {
        this.payMethod = payMethod;
    }

    public void setPrice(BigInteger price) {
        this.price = price;
    }

    public Format getFormat() {
        return format;
    }

    public void setFormat(Format format) {
        this.format = format;
    }
    
    public String getPriceEuro(){
        if (price == null) {
            return "error";
        }
        //return price.remainder(new BigInteger("100")).toString();
        String digits = price.toString();
        return digits.substring(0, digits.length()-2);
    }
    
    public String getPriceCent(){
        String digits = price.toString();
        int length = digits.length();
        return digits.substring(length-2, length);
    }
    
    public String fullPriceEuro(){
        return getPriceEuro()+","+getPriceCent()+" €";
    }

    public String getFinishing() {
        return finishing;
    }

    public void setFinishing(String finishing) {
        this.finishing = finishing;
    }

    public String getDatacheck() {
        return datacheck;
    }

    public void setDatacheck(String datacheck) {
        this.datacheck = datacheck;
    }

    public boolean isRoundedCorners() {
        return roundedCorners;
    }

    public void setRoundedCorners(boolean roundedCorners) {
        this.roundedCorners = roundedCorners;
    }

    public int getNumberAdresses() {
        return numberAdresses;
    }

    public void setNumberAdresses(int numberAdresses) {
        this.numberAdresses = numberAdresses;
    }
}
