/*
 * Arrebol Consultancy copyright.
 *
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.model.admin;

import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.model.enums.ActionStatus;
import com.arrebol.apc.model.enums.ActiveStatus;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.GenericGenerator;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
@Entity
@Table(name = "APC_TRANSFER")
public class Transfer implements Serializable {

   private static final long serialVersionUID = -1304758262604329766L;

   @Id
   @GeneratedValue(generator = "uuid")
   @GenericGenerator(name = "uuid", strategy = "uuid2")
   @Column(name = "id", length = 36)
   private String id;

   @ManyToOne(fetch = FetchType.LAZY, optional = false)
   @JoinColumn(
           name = "id_user_transmitter",
           referencedColumnName = "id",
           nullable = false
   )
   private User userTransmitter;

   @ManyToOne(fetch = FetchType.LAZY, optional = false)
   @JoinColumn(
           name = "id_user_receiver",
           referencedColumnName = "id",
           nullable = false
   )
   private User userReceiver;

   @Enumerated(EnumType.STRING)
   @Column(name = "active_status", nullable = false)
   private ActiveStatus activeStatus;

   @Enumerated(EnumType.STRING)
   @Column(name = "action_status", nullable = false)
   private ActionStatus actionStatus;

   @Column(name = "amount_to_transfer", nullable = false)
   private BigDecimal amountToTransfer;

   @ManyToOne(fetch = FetchType.LAZY, optional = false)
   @JoinColumn(
           name = "id_office",
           referencedColumnName = "id",
           nullable = false
   )
   private Office office;

   @Column(name = "created_by", nullable = false, length = 36)
   private String createdBy;

   @Temporal(TemporalType.TIMESTAMP)
   @Column(name = "created_on", length = 19)
   private Date createdOn;

   @Column(name = "last_updated_by", length = 36)
   private String lastUpdatedBy;

   @Temporal(TemporalType.TIMESTAMP)
   @Column(name = "last_updated_on", length = 19)
   private Date lastUpdatedOn;

   public Transfer() {

   }

   /**
    *
    * @param userTransmitter
    * @param userReceiver
    * @param activeStatus
    * @param actionStatus
    * @param amountToTransfer
    * @param office
    * @param createdBy
    */
   public Transfer(User userTransmitter, User userReceiver, ActiveStatus activeStatus, ActionStatus actionStatus, BigDecimal amountToTransfer, Office office, String createdBy) {
      this.userTransmitter = userTransmitter;
      this.userReceiver = userReceiver;
      this.activeStatus = activeStatus;
      this.actionStatus = actionStatus;
      this.amountToTransfer = amountToTransfer;
      this.office = office;
      this.createdBy = createdBy;
      Date date = new Date();
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.HOUR, -7);
      date = calendar.getTime();
      this.createdOn = date;
   }

   /**
    *
    * @param id
    */
   public Transfer(String id) {
      this.id = id;
   }

   public Transfer(String id, User userTransmitter, User userReceiver, ActiveStatus activeStatus, ActionStatus actionStatus, BigDecimal amountToTransfer, Office office, String createdBy) {
      this.id = id;
      this.userTransmitter = userTransmitter;
      this.userReceiver = userReceiver;
      this.activeStatus = activeStatus;
      this.actionStatus = actionStatus;
      this.amountToTransfer = amountToTransfer;
      this.office = office;
      this.createdBy = createdBy;
      this.createdOn = new Date();
   }

   public Transfer(String id, User userTransmitter, User userReceiver, ActiveStatus activeStatus, ActionStatus actionStatus, BigDecimal amountToTransfer, Office office, String createdBy, Date createdOn) {
      this.id = id;
      this.userTransmitter = userTransmitter;
      this.userReceiver = userReceiver;
      this.activeStatus = activeStatus;
      this.actionStatus = actionStatus;
      this.amountToTransfer = amountToTransfer;
      this.office = office;
      this.createdBy = createdBy;
      this.createdOn = createdOn;
   }

   public String getId() {
      return id;
   }

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

   public User getUserTransmitter() {
      return userTransmitter;
   }

   public void setUserTransmitter(User userTransmitter) {
      this.userTransmitter = userTransmitter;
   }

   public User getUserReceiver() {
      return userReceiver;
   }

   public void setUserReceiver(User userReceiver) {
      this.userReceiver = userReceiver;
   }

   public ActiveStatus getActiveStatus() {
      return activeStatus;
   }

   public void setActiveStatus(ActiveStatus activeStatus) {
      this.activeStatus = activeStatus;
   }

   public ActionStatus getActionStatus() {
      return actionStatus;
   }

   public void setActionStatus(ActionStatus actionStatus) {
      this.actionStatus = actionStatus;
   }

   public BigDecimal getAmountToTransfer() {
      return amountToTransfer;
   }

   public void setAmountToTransfer(BigDecimal amountToTransfer) {
      this.amountToTransfer = amountToTransfer;
   }

   public String getCreatedBy() {
      return createdBy;
   }

   public void setCreatedBy(String createdBy) {
      this.createdBy = createdBy;
   }

   public Date getCreatedOn() {
      return createdOn;
   }

   public void setCreatedOn(Date createdOn) {
      this.createdOn = createdOn;
   }

   public String getLastUpdatedBy() {
      return lastUpdatedBy;
   }

   public void setLastUpdatedBy(String lastUpdatedBy) {
      this.lastUpdatedBy = lastUpdatedBy;
   }

   public Date getLastUpdatedOn() {
      return lastUpdatedOn;
   }

   public void setLastUpdatedOn(Date lastUpdatedOn) {
      this.lastUpdatedOn = lastUpdatedOn;
   }

   public Office getOffice() {
      return office;
   }

   public void setOffice(Office office) {
      this.office = office;
   }

   public String getUserReceiverRoutes() {
      String routes = "";
      routes = userReceiver.getHumanResource().getHumanResourceHasRoutes().stream().map((py) -> py.getRouteCtlg().getRoute()).collect(Collectors.joining(", "));

      return routes;
   }

   public String getUserTransmitterRoutes() {
      String routes = "";
      routes = userTransmitter.getHumanResource().getHumanResourceHasRoutes().stream().map((py) -> py.getRouteCtlg().getRoute()).collect(Collectors.joining(", "));

      return routes;
   }

   public boolean getAction(Date lastStableSmallBox) {
      Date date = createdOn;
      boolean action = true;
      try {
         SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");

         String dateStr = dt1.format(date);
         date = dt1.parse(dateStr);

         action = date.after(lastStableSmallBox);

      } catch (Exception ex) {
         Logger.getLogger(ExpenseCompany.class.getName()).log(Level.SEVERE, null, ex);
      }
      return action;
   }

   public boolean getActive() {
      if (activeStatus == ActiveStatus.ENEBLED) {
         return true;
      } else {
         return false;
      }
   }

   public String conditionEnabled() {
      if (getActiveStatus() == ActiveStatus.DISABLED) {
         return "grayRow";
      } else {
         return null;
      }
   }

   @Override
   public String toString() {
      return "Transfer{" + "activeStatus=" + activeStatus + ", actionStatus=" + actionStatus + ", createdOn=" + createdOn + '}';
   }

}
