package ch.zhaw.gartenverwaltung.plantList;

import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.io.JsonPlantDatabase;
import ch.zhaw.gartenverwaltung.io.PlantDatabase;
import ch.zhaw.gartenverwaltung.types.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


import java.io.IOException;
import java.time.MonthDay;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Predicate;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class PlantListModelTest {
    PlantDatabase plantDatabase;
    List<Plant> examplePlantList;
    PlantListModel model;

    @BeforeEach
    void setUp() throws HardinessZoneNotSetException, IOException {
        createExamplePlantList();
        plantDatabase = mockPlantDatabase(examplePlantList);
        model = new PlantListModel(plantDatabase);
    }

    @AfterEach
    void tearDown() {
    }

    void createExamplePlantList(){
        examplePlantList = new ArrayList<>();
        examplePlantList.add(new Plant(
                20,
                "summertime onion",
                "Onion, (Allium cepa), herbaceous biennial plant in the amaryllis family (Amaryllidaceae) grown for its edible bulb. The onion is likely native to southwestern Asia but is now grown throughout the world, chiefly in the temperate zones. Onions are low in nutrients but are valued for their flavour and are used widely in cooking. They add flavour to such dishes as stews, roasts, soups, and salads and are also served as a cooked vegetable.",
                null,
                "15,30,2",
                0,
                "sandy to loamy, loose soil, free of stones",
                new ArrayList<>(),
                List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()),
                        new GrowthPhase(MonthDay.of(4, 3), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()),
                        new GrowthPhase(MonthDay.of(8, 5), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()),
                        new GrowthPhase(MonthDay.of(2, 8), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>()),
                        new GrowthPhase(MonthDay.of(10, 2), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())))
        );
        examplePlantList.add(new Plant(
                0,
                "Potato",
                "The potato is a tuber, round or oval, with small white roots called  'eyes', that are growth buds. The size varies depending on the variety; the colour of the skin can be white, yellow or even purple.",
                null,
                "35",
                6,
                "sandy",
                new ArrayList<>(),
                List.of(new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.HARVEST, HardinessZone.ZONE_8A, new ArrayList<>()),
                        new GrowthPhase(MonthDay.of(6, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())))
        );
        examplePlantList.add(new Plant(
                1,
                "Early Carrot",
                "Carrot, (Daucus carota), herbaceous, generally biennial plant of the Apiaceae family that produces an edible taproot. Among common varieties root shapes range from globular to long, with lower ends blunt to pointed. Besides the orange-coloured roots, white-, yellow-, and purple-fleshed varieties are known.",
                null,
                "5,35,2.5",
                0,
                "sandy to loamy, loose soil, free of stones",
                new ArrayList<>(),
                List.of(new GrowthPhase(MonthDay.of(4, 4), MonthDay.of(12, 4), 0, new WateringCycle(0, 0, null), GrowthPhaseType.PLANT, HardinessZone.ZONE_8A, new ArrayList<>())))
        );
    }

    PlantDatabase mockPlantDatabase(List<Plant> plantList) throws HardinessZoneNotSetException, IOException {
        PlantDatabase plantDatabase = mock(JsonPlantDatabase.class);
        when(plantDatabase.getPlantList(HardinessZone.ZONE_8A)).thenReturn(plantList);
        when(plantDatabase.getPlantList(HardinessZone.ZONE_1A)).thenReturn(new ArrayList<>());
        when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 0)).thenReturn(Optional.of(plantList.get(1)));
        when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 1)).thenReturn(Optional.of(plantList.get(2)));
        when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 20)).thenReturn(Optional.of(plantList.get(0)));
        when(plantDatabase.getPlantById(HardinessZone.ZONE_8A, 2)).thenReturn(Optional.empty());

        return plantDatabase;
    }

    void checkCurrentZone(HardinessZone expectedZone) {
        assertEquals(expectedZone, model.getCurrentZone());
    }

    @Test
    void setCurrentZone() {
        checkCurrentZone(HardinessZone.ZONE_8A); // TODO change to get default zone from config
        model.setCurrentZone(HardinessZone.ZONE_1A);
        checkCurrentZone(HardinessZone.ZONE_1A);
        model.setCurrentZone(HardinessZone.ZONE_8A);
        checkCurrentZone(HardinessZone.ZONE_8A);
    }

    @Test
    void getPlantList() throws HardinessZoneNotSetException, IOException {
        model.setCurrentZone(HardinessZone.ZONE_1A);
        List<Plant> plantListResult = model.getPlantList(HardinessZone.ZONE_8A);
        checkCurrentZone(HardinessZone.ZONE_8A);
        assertEquals(examplePlantList.size(), plantListResult.size());
        assertEquals(examplePlantList.get(2), plantListResult.get(0));
        assertEquals(examplePlantList.get(1), plantListResult.get(1));
        assertEquals(examplePlantList.get(0), plantListResult.get(2));

        assertEquals(0, model.getPlantList(HardinessZone.ZONE_1A).size());
    }

    @Test
    void getSortedPlantList() throws HardinessZoneNotSetException, IOException {
        model.setCurrentZone(HardinessZone.ZONE_1A);
        List<Plant> plantListResult = model.getSortedPlantList(HardinessZone.ZONE_8A, PlantListModel.sortByName);
        checkCurrentZone(HardinessZone.ZONE_8A);
        assertEquals(examplePlantList.size(), plantListResult.size());
        assertEquals(examplePlantList.get(2), plantListResult.get(0));
        assertEquals(examplePlantList.get(1), plantListResult.get(1));
        assertEquals(examplePlantList.get(0), plantListResult.get(2));

        plantListResult = model.getSortedPlantList(HardinessZone.ZONE_8A, PlantListModel.SortById);
        assertEquals(examplePlantList.size(), plantListResult.size());
        assertEquals(examplePlantList.get(1), plantListResult.get(0));
        assertEquals(examplePlantList.get(2), plantListResult.get(1));
        assertEquals(examplePlantList.get(0), plantListResult.get(2));
    }

    @Test
    void getFilteredPlantList() throws HardinessZoneNotSetException, IOException {
        model.setCurrentZone(HardinessZone.ZONE_1A);
        Predicate<Plant> predicate = plant -> plant.name().toUpperCase(Locale.ROOT).contains("E");
        List<Plant> plantListResult = model.getFilteredPlantList(HardinessZone.ZONE_8A, predicate);
        checkCurrentZone(HardinessZone.ZONE_8A);
        assertEquals(2, plantListResult.size());
        assertEquals(examplePlantList.get(2), plantListResult.get(0));
        assertEquals(examplePlantList.get(0), plantListResult.get(1));
    }

    @Test
    void getFilteredPlantListById() throws HardinessZoneNotSetException, IOException {
        model.setCurrentZone(HardinessZone.ZONE_1A);
        List<Plant> plantListResult = model.getFilteredPlantListById(HardinessZone.ZONE_8A, 2L);
        assertEquals(0, plantListResult.size());
        plantListResult = model.getFilteredPlantListById(HardinessZone.ZONE_8A, 20L);
        assertEquals(1, plantListResult.size());
        assertEquals(examplePlantList.get(0), plantListResult.get(0));
    }

    @Test
    void getFilteredPlantListByString() throws HardinessZoneNotSetException, IOException {
        model.setCurrentZone(HardinessZone.ZONE_1A);
        List<Plant> plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "#2");
        assertEquals(0, plantListResult.size());
        plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "#20");
        assertEquals(1, plantListResult.size());
        assertEquals(examplePlantList.get(0), plantListResult.get(0));
        plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "#a2");
        assertEquals(0, plantListResult.size());
        plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "onion");
        assertEquals(1, plantListResult.size());
        assertEquals(examplePlantList.get(0), plantListResult.get(0));
        plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "white roots");
        assertEquals(1, plantListResult.size());
        assertEquals(examplePlantList.get(1), plantListResult.get(0));
        plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "apple");
        assertEquals(0, plantListResult.size());
        plantListResult = model.getFilteredPlantListByString(HardinessZone.ZONE_8A, "");
        assertEquals(3, plantListResult.size());
    }

    @Test
    void getFilteredPlantListByPlantingSaison() throws HardinessZoneNotSetException, IOException {
        model.setCurrentZone(HardinessZone.ZONE_1A);
        List<Plant> plantListResult = model.getFilteredPlantListByPlantingSaison(HardinessZone.ZONE_8A, MonthDay.of(4, 4), MonthDay.of(8, 4));
        assertEquals(2, plantListResult.size());
        assertEquals(examplePlantList.get(2), plantListResult.get(0));
        assertEquals(examplePlantList.get(1), plantListResult.get(1));
    }

    @Test
    void getFilteredPlantListByHarvestSaison() throws HardinessZoneNotSetException, IOException {
        model.setCurrentZone(HardinessZone.ZONE_1A);
        List<Plant> plantListResult = model.getFilteredPlantListByHarvestSaison(HardinessZone.ZONE_8A, MonthDay.of(4, 4), MonthDay.of(8, 4));
        assertEquals(2, plantListResult.size());
        assertEquals(examplePlantList.get(1), plantListResult.get(0));
        assertEquals(examplePlantList.get(0), plantListResult.get(1));
    }


}
