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

import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.SanctionController;
import com.arrebol.taxiservicios.controller.view.report.SavingDriverController;
import com.arrebol.taxiservicios.model.admin.Sanction;
import com.arrebol.taxiservicios.model.view.report.SavingDriver;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author Picasso
 */
@Named
@ViewScoped
public class SavingDriverBean extends TaxiGenericBean implements Serializable {

    /**
     *
     * @param economicNumber
     * @return
     */
    public List<SavingDriver> autoCompleEconomicNumber(String economicNumber) {
        try {
            return getController().autoCompleEconomicNumber("%" + economicNumber + "%");
        } catch (Exception e) {
            logger.error("autoCompleEconomicNumber", e);

            return new ArrayList<>();
        }
    }

    /**
     *
     * @param driverName
     * @return
     */
    public List<SavingDriver> autoCompleDriverName(String driverName) {
        try {
            return getController().autoCompleDriverNamer("%" + driverName + "%");
        } catch (Exception e) {
            logger.error("autoCompleDriverName", e);

            return new ArrayList<>();
        }
    }

    /**
     *
     * @param event
     */
    public void onAutoCompleteItemSelect(SelectEvent event) {
        try {
            getSavingDrivers().clear();
            SavingDriver tmpDriver = getController().findSavingDriverById(((SavingDriver) event.getObject()).getId());

            if (null != tmpDriver) {
                getSavingDrivers().add(tmpDriver);
                
                getSanction().clear();
                setSanction(getSanctionController().fillSanctionDatatableByDriver(((SavingDriver) event.getObject()).getId()));
            }
            
        } catch (Exception e) {
            logger.error("onAutoCompleteItemSelect", e);
        }
    }

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            setController(new SavingDriverController());
            setSanctionController(new SanctionController());
            //setSavingDrivers(getController().findAllSavingDriver());
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    private static final long serialVersionUID = 7031852980260669973L;

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

    private SavingDriverController controller;
    private SanctionController sanctionController;
    private List<SavingDriver> savingDrivers;
    private List<Sanction> sanction;
    private SavingDriver driver;

    public SavingDriverBean() {

    }

    public SavingDriverController getController() {
        return controller;
    }

    public void setController(SavingDriverController controller) {
        this.controller = controller;
    }

    public List<SavingDriver> getSavingDrivers() {
        if (null == savingDrivers) {
            savingDrivers = new ArrayList<>();
        }
        return savingDrivers;
    }

    public void setSavingDrivers(List<SavingDriver> savingDrivers) {
        this.savingDrivers = savingDrivers;
    }

    public SavingDriver getDriver() {
        return driver;
    }

    public void setDriver(SavingDriver driver) {
        this.driver = driver;
    }

    public SanctionController getSanctionController() {
        return sanctionController;
    }

    public void setSanctionController(SanctionController sanctionController) {
        this.sanctionController = sanctionController;
    }

    public List<Sanction> getSanction() {
        if (null == sanction) {
            sanction = new ArrayList<>();
        }
        return sanction;
    }

    public void setSanction(List<Sanction> sanction) {
        this.sanction = sanction;
    }

}
