/*
 * 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 com.arrebol.taxiservicios.beans.predio;

import com.arrebol.taxiservicios.beans.admin.client.*;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.CustomerController;
import com.arrebol.taxiservicios.controller.catalog.LocationController;
import com.arrebol.taxiservicios.controller.catalog.RegimenFiscalController;
import com.arrebol.taxiservicios.controller.predio.PredioController;
import com.arrebol.taxiservicios.model.admin.Service;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.catalog.RegimenFiscal;
import com.arrebol.taxiservicios.model.core.Address;
import com.arrebol.taxiservicios.model.core.ContactNumber;
import com.arrebol.taxiservicios.model.core.Person;
import com.arrebol.taxiservicios.model.core.PersonCommercialInvoice;
import com.arrebol.taxiservicios.model.enums.AddressType;
import com.arrebol.taxiservicios.model.enums.PersonType;
import com.arrebol.taxiservicios.model.enums.PhoneType;
import com.arrebol.taxiservicios.model.predio.Predio;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.model.DualListModel;

/**
 *
 * @author Picasso
 */
@Named("predioAddBean")
@ViewScoped
public class PredioAddBean extends TaxiGenericBean implements Serializable {

    /**
     * Save/Update an customer in the system.
     */
   public void addCustomer() {
        logger.info("addCustomer");
        try {
            if (!isAddCustomerBtnOnclick()) {
                getPredio().setId(UUID.randomUUID().toString());
                
                getPredio().setCreatedOn(new Date());
                getPredio().setLocation(new Location(locationId));
            } else {
                getPredio().setLastUpdatedOn(new Date());
            }

            boolean success = getPredioController().addPredio(getPredio(), getLoggedUser(), !isAddCustomerBtnOnclick());

            if (success) {
                setAddCustomerBtnOnclick(true);
                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("customer.add.title"),
                        getBundlePropertyFile().getString("customer.add.success.desc"));
            } else {
                logger.error("Employee " + getPredio() + " was not " + (isAddCustomerBtnOnclick() ? "updated" : "saved"));
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("customer.add.title"),
                        getBundlePropertyFile().getString("customer.add.fail.desc"));
            }
        } catch (Exception e) {
            logger.error("addEmployee: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("customer.add.title"),
                    getBundlePropertyFile().getString("customer.add.exception.desc"));
        }
    }

    /**
     * Add RFC, Email, CURP, and Local/Mobile phones to customer.
     */
 
    private static final long serialVersionUID = -4594340936069728620L;

    final Logger logger = LogManager.getLogger(CustomerAddBean.class);

    private PredioController predioController;
    private Predio predio;
    private List<ContactNumber> contactNumbers;
    private Address address;
    private PersonCommercialInvoice customerCommercialInvoice;

    private boolean addCustomerBtnOnclick;
    
    private Date sixteenYearsOld;
    private DualListModel<Service> services;

    private LocationController locationController;

    private String locationId;
   
    private List<Location> locations;


    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            Calendar calendar = Calendar.getInstance();

            calendar.add(Calendar.YEAR, -15);
            setSixteenYearsOld(calendar.getTime());

            loadBundlePropertyFile();

            setPredioController(new PredioController());
            setLocationController(new LocationController());
         
            setLocations(getLocationController().fillLocationDatatable());
           
            setPredio(new Predio());
            setCustomerCommercialInvoice(new PersonCommercialInvoice());
            setAddress(new Address(AddressType.HOME));
            setAddCustomerBtnOnclick(false);
          

           
           
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    public LocationController getLocationController() {
        return locationController;
    }

    public void setLocationController(LocationController locationController) {
        this.locationController = locationController;
    }

    public String getLocationId() {
        return locationId;
    }

    public void setLocationId(String locationId) {
        this.locationId = locationId;
    }

    public List<Location> getLocations() {
        return locations;
    }

    public void setLocations(List<Location> locations) {
        this.locations = locations;
    }

    public Predio getPredio() {
        return predio;
    }

    public void setPredio(Predio predio) {
        this.predio = predio;
    }

    public PredioController getPredioController() {
        return predioController;
    }

    public void setPredioController(PredioController predioController) {
        this.predioController = predioController;
    }

  

    public List<ContactNumber> getContactNumbers() {
        if (null == contactNumbers) {
            contactNumbers = new ArrayList<>();
        }
        return contactNumbers;
    }

    public void setContactNumbers(List<ContactNumber> contactNumbers) {
        this.contactNumbers = contactNumbers;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public boolean isAddCustomerBtnOnclick() {
        return addCustomerBtnOnclick;
    }

    public void setAddCustomerBtnOnclick(boolean addCustomerBtnOnclick) {
        this.addCustomerBtnOnclick = addCustomerBtnOnclick;
    }

  
    public Date getSixteenYearsOld() {
        return sixteenYearsOld;
    }

    public void setSixteenYearsOld(Date sixteenYearsOld) {
        this.sixteenYearsOld = sixteenYearsOld;
    }

    public DualListModel<Service> getServices() {
        return services;
    }

    public void setServices(DualListModel<Service> services) {
        this.services = services;
    }
    
    public PersonCommercialInvoice getCustomerCommercialInvoice() {
        return customerCommercialInvoice;
    }

    public void setCustomerCommercialInvoice(PersonCommercialInvoice customerCommercialInvoice) {
        this.customerCommercialInvoice = customerCommercialInvoice;
    }

}
