package com.acme.model.domain.sc.entity;


import com.acme.model.domain.ad.entity.Currency;
import com.acme.structure.BaseEntity;
import com.acme.structure.BaseEntityListener;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity(name = "Order")
@Table(name = "ORDERS")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "ORDERTYPE", discriminatorType = DiscriminatorType.STRING, length = 30)
@DiscriminatorValue("Order")
//this is for oracle db @SequenceGenerator(name = "ORDER_SEQ", sequenceName = "ORDER_SEQ", allocationSize = 1)
@EntityListeners( { BaseEntityListener.class })
public class Order extends BaseEntity
{
    private String orderType;
    private String customerNote;
    private double totalPrice;
    private Currency currency;
    
    //0:not set, 1:cash, 2:credit card, 3:multinet, 4:sodexho, 5:ticket
    private int paymentType;
    
    private CustomerUser customerUser;

    private Collection<OrderDetail> orderDetails;

    //0: creating, 1:standing, 2: completed, 3:cancelled
    private byte orderStatus;

    public final static byte ORDERSTATUS_CREATING = 0;
    public final static byte ORDERSTATUS_STANDNG = 1;
    public final static byte ORDERSTATUS_COMPLETED = 2;
    public final static byte ORDERSTATUS_CANCELLED = 3;

    public final static int PAYMENTTYPE_NOTSET = 0;
    public final static int PAYMENTTYPE_CASH = 1;
    public final static int PAYMENTTYPE_CREDITCARD = 2;
    public final static int PAYMENTTYPE_MULTINET = 3;
    public final static int PAYMENTTYPE_SODEXHO = 4;
    public final static int PAYMENTTYPE_TICKET = 5;


    public Order()
    {
        this.entityName = "Order";
        setOrderType(this.getEntityName());
        setPaymentType(PAYMENTTYPE_NOTSET);
        setOrderStatus(ORDERSTATUS_CREATING);
        
        orderDetails = new ArrayList<OrderDetail>();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) //, this is for oracle db, generator = "ORDER_SEQ")
    @Column(name = "ID")
    public int getId()
    {
        return id;
    }

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

    public void setOrderDetails(Collection<OrderDetail> orderDetails)
    {
        this.orderDetails = orderDetails;
    }

    @OneToMany(targetEntity = OrderDetail.class, fetch = (FetchType.EAGER), cascade = { CascadeType.ALL })
    @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name = "ORDERID")
    public Collection<OrderDetail> getOrderDetails()
    {
        return orderDetails;
    }

    public void setCustomerNote(String customerNote)
    {
        this.customerNote = customerNote;
    }

    @Column(name = "CUSTOMERNOTE")
    public String getCustomerNote()
    {
        return customerNote;
    }

    public void setTotalPrice(double totalPrice)
    {
        this.totalPrice = totalPrice;
    }

    @Column(name = "TOTALPRICE")
    public double getTotalPrice()
    {
        return totalPrice;
    }

    public void setCurrency(Currency currency)
    {
        this.currency = currency;
    }

    @ManyToOne(targetEntity = Currency.class, cascade = { })
    @JoinColumn(name = "CURRENCYID")
    public Currency getCurrency()
    {
        return currency;
    }

    public void setOrderStatus(byte orderStatus)
    {
        this.orderStatus = orderStatus;
    }

    @Column(name = "ORDERSTATUS")
    public byte getOrderStatus()
    {
        return orderStatus;
    }

    public void setOrderType(String orderType)
    {
        this.orderType = orderType;
    }

    @Column(name = "ORDERTYPE")
    public String getOrderType()
    {
        return orderType;
    }

    public void setPaymentType(int paymentType)
    {
        this.paymentType = paymentType;
    }

    @Column(name = "PAYMENTTYPE")
    public int getPaymentType()
    {
        return paymentType;
    }

    public void setCustomerUser(CustomerUser customerUser)
    {
        this.customerUser = customerUser;
    }

    @ManyToOne(targetEntity = Currency.class, cascade = { })
    @JoinColumn(name = "CUSTOMERUSERID")
    public CustomerUser getCustomerUser()
    {
        return customerUser;
    }

    @Override
    public String toString()
    {
        return "" + getId();
    }
}
