/*
 * 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.catalog;

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.catalog.BankCardController;
import com.arrebol.taxiservicios.controller.catalog.BankCardKardexController;
import com.arrebol.taxiservicios.model.catalog.BankCard;
import com.arrebol.taxiservicios.model.catalog.BankCardKardex;
import com.arrebol.taxiservicios.model.catalog.Color;
import com.arrebol.taxiservicios.model.enums.BankCardKardexType;
import com.arrebol.taxiservicios.model.enums.BasicType;
import com.arrebol.taxiservicios.services.catalog.BankCardService;
import com.arrebol.taxiservicios.services.catalog.ColorService;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author oscarvargas
 */
@Named("bankCardManager")
@ViewScoped
public class BankCardManager extends TaxiGenericBean implements Serializable, Datatable {

    @Override
    public void editRow(RowEditEvent event) {
        BankCard bank = (BankCard) event.getObject();
        if (bank != null) {
            bankCardCtrl.save(bank, getLoggedUser().getId());
            showMessage(FacesMessage.SEVERITY_INFO, "Registro Editado", "Se hizo el cambio correctamente.");
        }
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Edición Cancelada", ((BankCard) event.getObject()).getNumero());
    }

    @Override
    public void onRowReorder(ReorderEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Registro Movido", "De columna: " + (event.getFromIndex() + 1) + " a columna: " + (event.getToIndex() + 1));
    }

    @Override
    public void addRow() {
        BankCard bankCardObj = new BankCard();
        bankCardObj.setBank(getBank());
        bankCardObj.setTitular(getTitular());
        bankCardObj.setNumero(getNumero());
        bankCardObj.setBalance(getBalance());
        bankCardObj.setActiveStatus(BasicType.ENEBLED);
        bankCardObj.setLocation(getLoggedUser().getLocation());

        bankCardCtrl.save(bankCardObj, getLoggedUser().getId());
        
        BankCardKardex bankCardKardexObj = new BankCardKardex();
        
        bankCardKardexObj.setBankCard(bankCardObj);
        bankCardKardexObj.setAmount(getBalance());
        bankCardKardexObj.setAmountBefore(0D);
        bankCardKardexObj.setAmountAfter(0D);
        bankCardKardexObj.setKardexType(BankCardKardexType.INITIAL_BALANCE);
        bankCardKardexObj.setDescription("Saldo inicial");
        bankCardKardexObj.setLocation(bankCardObj.getLocation());
        
        bankCardKardexCtrl.save(bankCardKardexObj, getLoggedUser().getId());
        
        
        bankCard.add(bankCardObj);
        
        titular = "";
        numero = "";
        bank = "";
        FacesMessage msg = new FacesMessage("Nueva tarjeta bancaria", "Se agregó correctamente");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    @Override
    public void deleteRow() {
        bankCardCtrl.delete(selectedBankCard, getLoggedUser().getId());
        bankCard.remove(selectedBankCard);
        selectedBankCard = null;
        showMessage(FacesMessage.SEVERITY_INFO, "Tarjeta bancaria Eliminada", "Se eliminó correctamente.");
    }

    public List<BankCard> getBankCard() {
        return bankCard;
    }

    public void setBankCard(List<BankCard> bankCard) {
        this.bankCard = bankCard;
    }

    public BankCard getSelectedBankCard() {
        return selectedBankCard;
    }

    public void setSelectedBankCard(BankCard selectedBankCard) {
        this.selectedBankCard = selectedBankCard;
    }

    public String getBank() {
        return bank;
    }

    public void setBank(String bank) {
        this.bank = bank;
    }

    public String getTitular() {
        return titular;
    }

    public void setTitular(String titular) {
        this.titular = titular;
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }
    
    public String getId() {
        return id;
    }

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

    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }
    
    

    @PostConstruct
    public void init() {
        bankCardCtrl = new BankCardController();
        bankCardKardexCtrl = new BankCardKardexController();
        bankCard = bankCardCtrl.fillDatatable(getLoggedUser().getLocation());
    }

    private static final long serialVersionUID = -2400590341745672164L;

    private List<BankCard> bankCard;
    private BankCard selectedBankCard;

    private String id;
    private String bank;
    private String titular;
    private String numero;
    private Double balance;

    private BankCardController bankCardCtrl;
    private BankCardKardexController bankCardKardexCtrl;

}
