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


import com.acme.model.domain.ad.entity.Currency;
import com.acme.model.domain.ad.entity.UnitGroup;
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.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity(name = "Service")
@Table(name = "SERVICES")
//this is for oracle db @SequenceGenerator(name = "SERVICE_SEQ", sequenceName = "SERVICE_SEQ", allocationSize = 1)
@EntityListeners( { BaseEntityListener.class })
public class Service extends BaseEntity
{
    private String code;
    private String name;
    private String description;
    private UnitGroup unitGroup;
    private ServiceGroup serviceGroup;
    //private String imagePath;

    private double vatRate;
    private double unitPrice;
    private boolean isVatIncluded;
    private Currency currency;
    
    private boolean inUse;
    private Collection<ServiceContent> contents;
    
    //0: Active, 1: SoldOut, 2: NotAppropriateTime, 3: NotInStock
    private int sellingStatus;
    
    public final static byte SELLINGTATUS_ACTIVE=0;    
    public final static byte SELLINGSTATUS_SOLDOUT=1;
    public final static byte SELLINGSTATUS_NOTAPPROPRIATETIME=2;
    public final static byte SELLINGSTATUS_NOTINSTOCK=3;
    
    

    public Service()
    {
        entityName = "Service";
        
        this.contents=new ArrayList<ServiceContent>();
    }

    public Service(int id, String code, String name)
    {
        entityName = "Service";
        this.id = id;
        this.code = code;
        this.name = name;     
        
        this.contents=new ArrayList<ServiceContent>();
    }

    public Service(int id, String code, String name, UnitGroup unitGroup)
    {
        entityName = "Service";
        this.id = id;
        this.code = code;
        this.name = name;
        this.unitGroup = unitGroup;
    }

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

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

    @Column(name = "CODE", unique = true, length = 7)
    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    @Column(name = "NAME", length = 20)
    public String getName()
    {
        return name;
    }

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

    @Column(name = "DESCRIPTION", length = 20)
    public String getDescription()
    {
        return description;
    }

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

    @ManyToOne(targetEntity = UnitGroup.class, cascade = { })
    @JoinColumn(name = "UNITGROUPID")
    public UnitGroup getUnitGroup()
    {
        return unitGroup;
    }

    public void setUnitGroup(UnitGroup unitGroup)
    {
        this.unitGroup = unitGroup;
    }

    @ManyToOne(targetEntity = ServiceGroup.class, cascade = { })
    @JoinColumn(name = "SERVICEGROUPID")
    public ServiceGroup getServiceGroup()
    {
        return serviceGroup;
    }

    public void setServiceGroup(ServiceGroup serviceGroup)
    {
        this.serviceGroup = serviceGroup;
    }

    @Column(name = "INUSE")
    public boolean isInUse()
    {
        return inUse;
    }

    public void setInUse(boolean inUse)
    {
        this.inUse = inUse;
    }

    public void setVatRate(double vatRate)
    {
        this.vatRate = vatRate;
    }

    @Column(name = "VATRATE")
    public double getVatRate()
    {
        return vatRate;
    }

    public void setUnitPrice(double unitPrice)
    {
        this.unitPrice = unitPrice;
    }

    @Column(name = "UNITPRICE")
    public double getUnitPrice()
    {
        return unitPrice;
    }

    public void setIsVatIncluded(boolean isVatIncluded)
    {
        this.isVatIncluded = isVatIncluded;
    }

    @Column(name = "VATINCLUDED")
    public boolean isIsVatIncluded()
    {
        return isVatIncluded;
    }

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

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

    public void setSellingStatus(int sellingStatus)
    {
        this.sellingStatus = sellingStatus;
    }

    @Column(name = "SELLINGSTATUS")
    public int getSellingStatus()
    {
        return sellingStatus;
    }

    public void setContents(Collection<ServiceContent> contents)
    {
        this.contents = contents;
    }

    @OneToMany(targetEntity = ServiceContent.class, fetch = (FetchType.EAGER), cascade = { CascadeType.ALL })
    @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name = "SERVICEID")
    public Collection<ServiceContent> getContents()
    {
        return contents;
    }

    @Override
    public String toString()
    {
        return getCode() + "-" + getName();
    }
}
