/*
 * 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.admin.Taxi;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.catalog.Product;
import com.arrebol.taxiservicios.model.core.Person;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.enums.PaymentMethodType;
import com.arrebol.taxiservicios.model.enums.ServiceOrderType;
import com.arrebol.taxiservicios.model.stocktaking.ServiceOrder;
import com.arrebol.taxiservicios.model.stocktaking.ServiceOrderHasProduct;
import com.arrebol.taxiservicios.model.stocktaking.ServiceOrderHasProductId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Picasso
 */
public class ServiceOrderControllerTest {

    private static final String ECONOMIC_NUMER = "TES-TE-12";
    private static final String LIKE = "-12";
    private static final String USER_ID = "09ea84b8-b9d1-4a71-adfe-db653b2a9982";
    private static final String TAXI_ID = "9f11aa21-808d-4794-839b-bb9b4c570d8d";
    private static final String SERVICE_ORDER_ID = "9112f286-5d06-4cca-bd1e-64561606a831";
    private static final String PRODUCT_ONE_ID = "bd1ec027-265f-4d95-aaf8-7089108d43b7";
    private static final String PRODUCT_TWO_ID = "e54cc19f-4292-433c-a5e3-f9981c9190e3";
    private static final Integer QUANTITY = 1;
    private static final Double IVA = .16;

    private ServiceOrderController controller;

    public ServiceOrderControllerTest() {
    }

    @Before
    public void setUp() throws Exception {
        controller = new ServiceOrderController();
    }

    //@Test
    public void loadGrantsByUser() {
        try {
            List<String> economicNubers = controller.getEconomicNumbersLike(LIKE);

            assertNotEquals(0, economicNubers.size());
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void getTaxiDetails() {
        try {
            Object o = controller.getTaxiDetailsInnerJoin(ECONOMIC_NUMER);

            if (o instanceof Object[]) {
                Object[] t = (Object[]) o;
                for (Object object : t) {
                    System.out.println(object);
                }
            }

            assertNotNull(o);
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void getTaxiDetails2() {
        try {
            Taxi taxi = controller.getTaxiByEconomicNumber(ECONOMIC_NUMER);

            System.out.println(taxi.getVehicle().getName());
            System.out.println(taxi.getVehicle().getBrand().getName());
            System.out.println(taxi.getColor().getName());

            assertNotNull(taxi);
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void getAllServiceOrderActive() {
        try {
            List<ServiceOrder> serviceOrders = controller.getAllServiceOrderActive();

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

    //@Test
    public void createServiceOrder() {
        try {
            ServiceOrder so = new ServiceOrder(SERVICE_ORDER_ID);
            List<ServiceOrderHasProduct> hasProducts = new ArrayList<>();

            so.setActive(Boolean.TRUE);
            so.setCreatedBy(new User(USER_ID, ""));
            so.setCreatedOn(new Date());
            so.setKm(12345f);
            so.setName("Uno");
            so.setNameNumber(1);
            so.setServiceOrderType(ServiceOrderType.PENDING);
            so.setPaymentMethodType(PaymentMethodType.EMPTY);
            so.setTaxi(new Taxi(TAXI_ID));

            ServiceOrderHasProduct sohp = new ServiceOrderHasProduct(
                    new ServiceOrderHasProductId(
                            SERVICE_ORDER_ID,
                            PRODUCT_ONE_ID)
            );

            Double price = 3690 * .15 + 3690;
            Double subtotal = price * QUANTITY;

            sohp.setQuantity(QUANTITY);

            sohp.setPrice(price.floatValue());
            sohp.setTotal(subtotal.floatValue());
            sohp.setCreatedBy(new User(USER_ID, ""));
            sohp.setCreatedOn(new Date());

            Double iva = subtotal * IVA;
            Double total = subtotal + iva;

            so.setIva(iva.floatValue());
            so.setSubtotal(subtotal.floatValue());
            so.setTotal(total.floatValue());

            hasProducts.add(sohp);

            assertTrue(controller.createServiceOrder(so, hasProducts));
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void getMaxFolio() {
        try {
            assertNotNull(controller.getNextFolio());
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void getAllServiceOrderToAuthorize() {
        try {
            List<ServiceOrder> serviceOrders = controller.getAllServiceOrderNotIn(new Location());

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

    //@Test
    public void findAllProductByOrderId() {
        try {
            List<ServiceOrderHasProduct> details = controller.findAllProductByOrderId(SERVICE_ORDER_ID);
            System.out.println(details);
            assertFalse(details.isEmpty());
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void getAllServiceOrderActiveAndPending() {
        try {
            List<ServiceOrderType> types = new ArrayList<>();

            types.add(ServiceOrderType.PENDING);
            types.add(ServiceOrderType.APPROVED);

            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();

            start.set(2019, 1, 27, 0, 0, 0);
            end.set(2019, 1, 27, 23, 59, 59);

            List<ServiceOrder> orders = controller.getAllServiceOrderActiveAndStatusIn(types, start.getTime(), end.getTime(),new Location());

            System.out.println(orders);
            assertFalse(orders.isEmpty());
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void validateStock() {
        try {
            List<ServiceOrderHasProduct> details = controller.findAllProductByOrderId(SERVICE_ORDER_ID);

            Boolean[] greaterThan = {false};

            details
                    .stream()
                    .filter(detail -> detail.getQuantity() > detail.getProduct().getQuantity())
                    .forEach(detail -> {
                        greaterThan[0] = true;
                        System.out.println(detail);
                    });

            assertFalse(details.isEmpty());
            assertTrue(greaterThan[0]);
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void getAllProducts() {
        try {
            List<Product> products = controller.getAllProducts();
            System.out.println(products);
            assertFalse(products.isEmpty());
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void fillDriversComboBox() {
        try {
            List<Person> drivers = controller.fillDriversComboBox();
            assertFalse(drivers.isEmpty());
        } catch (Exception e) {
            assertFalse(true);
        }
    }

    //@Test
    public void loadServiceOrderById() {
        try {
            ServiceOrder serviceOrder = controller.loadServiceOrderById(SERVICE_ORDER_ID);
            assertNotNull(serviceOrder);
        } catch (Exception e) {
            assertFalse(true);
        }
    }

}
