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

import com.arrebol.taxiservicios.controller.util.DateWrapper;
import com.arrebol.taxiservicios.model.admin.Contract;
import com.arrebol.taxiservicios.model.admin.ContractHasService;
import com.arrebol.taxiservicios.model.admin.Taxi;
import com.arrebol.taxiservicios.model.catalog.Holiday;
import com.arrebol.taxiservicios.model.view.dashboard.MaintenanceView;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

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

    private TaxiController controller;
    public static final String TAXI_ID = "832d1f90-61de-4ab7-a6a9-0e2916fffead";
    public static final String DRIVER_ID = "146a52ce-df23-4fc6-954e-e19e414726d0";

    public TaxiControllerTest() {
    }

    @Before
    public void setUp() {
        controller = new TaxiController();
    }

    //@Test
    public void findAllTaxisAvailablesMaintenance() {
        try {
            List<MaintenanceView> maintenanceViews = controller.findAllTaxisAvailablesMaintenance();

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

            assertTrue(!maintenanceViews.isEmpty());
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void getAllTaxis() {
        try {
            List<Taxi> taxi = controller.fillTaxiDatatable();

            assertTrue(!taxi.isEmpty());
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void findAllTaxisAvailables() {
        try {
            List<Taxi> taxi = controller.findAllTaxisAvailables();

            assertTrue(!taxi.isEmpty());
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void getTaxiDetailsByTaxiId() {
        try {
            Taxi taxi = controller.getTaxiDetailsByTaxiId(TAXI_ID);

            List<String> ids = new ArrayList<>();

            ((Set<Contract>) taxi.getContracts()).forEach((Contract co) -> {
                ids.add(co.getId());
            });
            List<ContractHasService> chses = controller.getAllContractsForTaxi((Set<Contract>) taxi.getContracts());

            chses.forEach((ContractHasService obj) -> {
                System.out.println(obj.getService());
            });

            assertNotNull(taxi);
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void addTaxi() {
        try {
            Taxi taxi = new Taxi();
            taxi.setId(UUID.randomUUID().toString());
            taxi.setPlaca("sfssdgds");
            taxi.setEconomicNumber("jfjdj");
            taxi.setMotorNmumber("faaf");
            taxi.setSerialNumber("fafg");
            taxi.setYear("2019");
            taxi.setComments("gsdgsd");
            taxi.setCreatedOn(new Date());
            taxi.setActive(Boolean.TRUE);
            String vehicleId = "642789db-3eb4-41da-b6cd-f659bcb523df";
            String customerId = "0f8dab1a-59c1-4890-8820-544a9759765c";
            String colorId = "adaf44d8-0df8-428e-bbee-c600c43c55dc";
            String driverId = "";
            String username = "09ea84b8-b9d1-4a71-adfe-db653b2a9982";
            Boolean bandera = controller.addTaxi(taxi, username, true, vehicleId, driverId, customerId, colorId);

            assertTrue(bandera);

        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void delete() {
        try {
            assertTrue(controller.deteleToCollectByDriverId(DRIVER_ID));
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void findHolidaysByRangeOfDates() {
        try {
            Date startDate = DateWrapper.today().firstDateForMonth().midnigthTime().asDate();
            Date endDate = DateWrapper.today().lastDateForMonth().midnigthTime().asDate();

            List<Holiday> holidays = controller.findHolidaysByRangeOfDates(startDate, endDate);

            assertFalse(holidays.isEmpty());
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void availableDays() {
        try {
            Date startDate = DateWrapper.today().firstDateForMonth().midnigthTime().asDate();
            Date endDate = DateWrapper.today().lastDateForMonth().midnigthTime().asDate();

            Calendar d2 = Calendar.getInstance();

            d2.set(Calendar.DAY_OF_MONTH, 30);
            d2.set(Calendar.MONTH, 3);
            d2.set(Calendar.HOUR_OF_DAY, 0);
            d2.set(Calendar.MINUTE, 0);
            d2.set(Calendar.SECOND, 0);

            System.out.println("startDate: " + d2.getTime());
            System.out.println("endDate: " + endDate);

            List<Holiday> holidays = controller.findHolidaysByRangeOfDates(d2.getTime(), endDate);

            Calendar c = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();

            c.setTime(startDate);
            c2.setTime(endDate);

            List<Calendar> calendars = new ArrayList<>();

            holidays.forEach((holiday) -> {
                System.out.println(holiday.getHoliday());
            });

            for (int i = c.get(Calendar.DAY_OF_MONTH); i <= c2.get(Calendar.DAY_OF_MONTH); i++) {
                Calendar d = Calendar.getInstance();

                d.set(Calendar.DAY_OF_MONTH, i);
                d.set(Calendar.HOUR_OF_DAY, 0);
                d.set(Calendar.MINUTE, 0);
                d.set(Calendar.SECOND, 0);

                Holiday holiday = new Holiday(d.getTime());

                if (!holidays.contains(holiday)) {
                    calendars.add(d);
                }
            }

            calendars.forEach((calendar) -> {
                System.out.println(calendar.getTime());
            });

            assertFalse(holidays.isEmpty());
        } catch (Exception e) {
            e.printStackTrace();
            assertNotNull(null);
        }
    }

    //@Test
    public void deleteContractAndContractDetailsById() {
        try {
            assertTrue(controller.deleteContractAndContractDetailsById(new Contract("b0ed628b-95c0-4edc-a110-d4a2ce67dd20")));
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    //@Test
    public void findAllActiveDriversWithoutTaxi() {
        try {
            assertFalse(controller.findAllActiveDriversWithoutTaxi().isEmpty());
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

    @Test
    public void getDriverByTaxiId() {
        try {
            Taxi taxi = controller.getDriverByTaxiId(TAXI_ID);

            assertNotNull(taxi);
        } catch (Exception e) {
            assertNotNull(null);
        }
    }

}
