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

import com.arrebol.taxiservicios.model.core.Person;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.enums.StatusType;
import com.arrebol.taxiservicios.model.stocktaking.Invoice;
import com.arrebol.taxiservicios.model.stocktaking.InvoiceHasProduct;
import com.arrebol.taxiservicios.model.stocktaking.InvoiceHasProductId;
import com.arrebol.taxiservicios.model.stocktaking.ProductInvoiceBk;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Carlos Janitzio Zavala López.
 */
public class InvoiceControllerTest {

    private static final String INVOICE_ID = "4a872dc9-9e5e-4f53-942a-0ec55563da6e";
    private static final String INVOICE_ID_TO_DELETE = "e5c92ea6-becc-44ba-9a96-cb71425db8cb";
    private static final String FOLIO = "000000001";
    private static final String FOLIO_UPDATE = "000000003";
    private static final String COMMENT = "Saved in a JUnit testing";
    private static final String COMMENT_UPDATE = "Update in a JUnit testing";
    private static final String PROVIDER_ID = "154242ed-f382-4271-aee8-ba65c639b625";
    private static final String PROVIDER_ID_UPDATE = "5179835a-b0f6-4448-acb4-aaafcd223777";
    private static final String PRODUCT_ID_1 = "00596eaf-0300-48d5-bb7c-8f04b26d2be0";
    private static final String PRODUCT_ID_2 = "02b8059c-5c87-43b9-bf25-9e45f5ec7dd2";
    private static final String PRODUCT_ID_3 = "44a4e79d-82fa-411e-9fe7-4961b5048275";
    private static final User BY = new User("09ea84b8-b9d1-4a71-adfe-db653b2a9982", "");

    private InvoiceController controller;
    Invoice invoice;
    List<InvoiceHasProduct> products;

    Invoice saveInvoice;
    List<InvoiceHasProduct> saveProducts;

    public InvoiceControllerTest() {
    }

    @Before
    public void setUp() throws Exception {
        controller = new InvoiceController();
        products = new ArrayList<>();
        saveProducts = new ArrayList<>();
        invoice = new Invoice(INVOICE_ID, FOLIO, new Date(), COMMENT,
                1000f, 160f, 1160f, StatusType.APPROVED, Boolean.TRUE,
                new Person(PROVIDER_ID), BY, new Date());

        products.add(new InvoiceHasProduct(new InvoiceHasProductId(INVOICE_ID, PRODUCT_ID_1), 250f, 2, BY, new Date()));
        products.add(new InvoiceHasProduct(new InvoiceHasProductId(INVOICE_ID, PRODUCT_ID_2), 125f, 4, BY, new Date()));
    }

    //@Test
    public void saveInvoice() {
        try {
            //Create the invoice.
            saveInvoice = new Invoice(UUID.randomUUID().toString(), FOLIO, new Date(), COMMENT,
                    1000f, 160f, 1160f, StatusType.APPROVED, Boolean.TRUE,
                    new Person(PROVIDER_ID), BY, new Date());

            //Add products.
            saveProducts.add(new InvoiceHasProduct(new InvoiceHasProductId(saveInvoice.getId(), PRODUCT_ID_2), 125f, 5, BY, new Date()));
            saveProducts.add(new InvoiceHasProduct(new InvoiceHasProductId(saveInvoice.getId(), PRODUCT_ID_3), 250f, 10, BY, new Date()));

            //execute saveInvoice from controller.
            boolean sucess = controller.saveInvoice(saveInvoice, saveProducts);

            assertTrue(sucess);
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void updateInvoice() {
        try {
            invoice.setComment(COMMENT_UPDATE);
            invoice.setFolio(FOLIO_UPDATE);
            invoice.setPerson(new Person(PROVIDER_ID_UPDATE));
            invoice.setBuyDate(new Date());
            invoice.setLastUpdatedBy(BY);
            invoice.setLastUpdatedOn(new Date());

            boolean sucess = controller.updateInvoice(invoice);

            assertTrue(sucess);
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void loadInvoicebyId() {
        try {
            invoice = null;
            invoice = controller.loadInvoicebyId(INVOICE_ID);

            assertNotNull(invoice);

            List<InvoiceHasProduct> hasProducts = controller.fillInvoiceHasProductDatatable(INVOICE_ID);

            hasProducts.forEach(result -> {
                System.out.println("Name: " + result.getProduct().getName());
                System.out.println("Code: " + result.getProduct().getCode());
            });
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void seachInvoiceInBackupById() {
        try {

            List<ProductInvoiceBk> invoiceBks = controller.seachInvoiceInBackupById(INVOICE_ID);

            assertNotNull(invoiceBks);
            assertTrue(invoiceBks.isEmpty());
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void deleteInvoiceById() {
        try {
            List<ProductInvoiceBk> productInvoiceBks = controller.seachInvoiceInBackupById(INVOICE_ID_TO_DELETE);

            assertFalse(productInvoiceBks.isEmpty());

            assertTrue(controller.deleteInvoiceById(INVOICE_ID_TO_DELETE, productInvoiceBks));
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    @Test
    public void autoCompleVehicle() {
        try {
            List<String> results = controller.autoCompleVehicle("%GDL%");

            results.forEach(System.out::println);

            assertFalse(results.isEmpty());
        } catch (Exception e) {
            assertFalse(true);
        }
    }

}
