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

import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.catalog.ProductBrandController;
import com.arrebol.taxiservicios.controller.catalog.ProductController;
import com.arrebol.taxiservicios.model.catalog.Brand;
import com.arrebol.taxiservicios.model.catalog.Product;
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;

/**
 *
 * @author oscarvargas
 */
@Named("productService")
@ViewScoped
public class ProductService extends TaxiGenericBean implements Serializable {

    public List<Product> fillDatatableProduct() {

        return productCtrl.fillProductDatatable();
    }

    /**
     *
     * @param isProductName
     * @param textToSearch
     * @return
     */
    public List<Product> searchProductsLikeField(boolean isProductName, String textToSearch) {
        try {
            return productCtrl.searchProductsLikeField(isProductName, textToSearch);
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    /**
     *
     * @param brandId
     * @return
     */
    public List<Product> searchProductByBrand(String brandId) {
        try {
            if ("AllBrands".equals(brandId)) {
                return productCtrl.fillProductDatatable();
            } else if (brandId.length() == 36) {
                return productCtrl.searchProductByBrand(brandId);
            } else {
                return new ArrayList<>();
            }
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    /**
     *
     * @param id
     * @return
     */
    public Product findProductById(String id) {
        try {
            return productCtrl.findProductById(id);
        } catch (Exception e) {
            return new Product();
        }
    }

    public boolean saveProductService(Product product, String brandId) {

        return productCtrl.saveProduct(product, getLoggedUser().getId(), brandId);
    }

    public void deleteProductService(Product product) {

        productCtrl.deleteProduct(product);
    }

    public List<Brand> getBrandesToProduct() {
        return brandCtrl.fillProductBrandComboBox();
    }

    private static final long serialVersionUID = -5625417286512415271L;

    private ProductController productCtrl;
    private ProductBrandController brandCtrl;

    @PostConstruct
    public void init() {
        productCtrl = new ProductController();
        brandCtrl = new ProductBrandController();
    }
}
