package org.innovateuk.ifs.controller;

import org.innovateuk.ifs.BaseControllerIntegrationTest;
import org.innovateuk.ifs.IntegrationTestConfig;
import org.innovateuk.ifs.alert.resource.AlertResource;
import org.innovateuk.ifs.alert.resource.AlertType;
import org.innovateuk.ifs.alert.builder.AlertResourceBuilder;
import org.innovateuk.ifs.user.resource.Role;
import org.innovateuk.ifs.user.resource.UserResource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

import static java.time.ZonedDateTime.now;
import static java.util.Collections.singletonList;
import static org.innovateuk.ifs.alert.resource.AlertType.MAINTENANCE;
import static org.innovateuk.ifs.commons.security.SecuritySetter.basicSecurityUser;
import static org.innovateuk.ifs.user.builder.UserResourceBuilder.newUserResource;
import static org.innovateuk.ifs.alert.builder.AlertResourceBuilder.newAlertResource;
import static org.junit.Assert.*;
@SpringBootTest(classes= IntegrationTestConfig.class)
@ActiveProfiles("integration-test")
public class AlertControllerIntegrationTest extends BaseControllerIntegrationTest<AlertController> {

    private UserResource systemMaintenanceUser;

    @Before
    public void setUp() throws Exception {
        systemMaintenanceUser = newUserResource().withRolesGlobal(singletonList(Role.SYSTEM_MAINTAINER)).build();
    }

    @Autowired
    @Override
    protected void setControllerUnderTest(AlertController controller) {
        this.controller = controller;
    }

    @Test
    public void test_findAllVisible() throws Exception {
        // save new alerts with date ranges that should make them visible now
        ZonedDateTime now = now();
        ZonedDateTime oneSecondAgo = now.minusSeconds(1);
        ZonedDateTime oneDayAgo = now.minusDays(1);
        ZonedDateTime oneHourAhead = now.plusHours(1);
        ZonedDateTime oneDayAhead = now.plusDays(1);

        setLoggedInUser(systemMaintenanceUser);
        AlertResource created1 = controller.create(newAlertResource()
                .withValidFromDate(oneDayAgo).withId(12L)
                .withValidToDate(oneDayAhead)
                .build()).getSuccess();


        AlertResource created2 = controller.create(newAlertResource()
                .withValidFromDate(oneSecondAgo).withId(13L)
                .withValidToDate(oneHourAhead)
                .build()).getSuccess();

        setLoggedInUser(basicSecurityUser);
        List<AlertResource> found = controller.findAllVisible().getSuccess();

        assertEquals(2, found.size());
        assertEquals(created1, found.get(0));
        assertEquals(created2, found.get(1));
    }

    @Test
    public void test_findAllVisibleByType() throws Exception {
        // save new alerts with date ranges that should make them visible now
        ZonedDateTime now = now();
        ZonedDateTime oneSecondAgo = now.minusSeconds(1);
        ZonedDateTime oneDayAgo = now.minusDays(1);
        ZonedDateTime oneHourAhead = now.plusHours(1);
        ZonedDateTime oneDayAhead = now.plusDays(1);

        setLoggedInUser(systemMaintenanceUser);
        AlertResource created1 = controller.create(newAlertResource()
                .withValidFromDate(oneDayAgo)
                .withValidToDate(oneDayAhead)
                .build()).getSuccess();



        AlertResource created2 = controller.create(newAlertResource()
                .withValidFromDate(oneSecondAgo)
                .withValidToDate(oneHourAhead)
                .build()).getSuccess();

        setLoggedInUser(basicSecurityUser);
        List<AlertResource> found = controller.findAllVisibleByType(MAINTENANCE).getSuccess();

        assertEquals(2, found.size());
        assertEquals(created1, found.get(0));
        assertEquals(created2, found.get(1));
    }


    @Test
    public void test_create() throws Exception {
        setLoggedInUser(systemMaintenanceUser);

        AlertResource alertResource = AlertResourceBuilder.newAlertResource()
                .build();

        AlertResource created = controller.create(alertResource).getSuccess();
        assertNotNull(created.getId());
        assertEquals("Sample message", created.getMessage());
        assertEquals(MAINTENANCE, created.getType());
        assertEquals(LocalDateTime.parse("2016-05-06T21:00:00.00").atZone(ZoneId.of("UTC")), created.getValidFromDate());
        assertEquals(LocalDateTime.parse("2016-05-06T21:05:00.00").atZone(ZoneId.of("UTC")), created.getValidToDate());

        // check it can also be found
        assertNotNull(controller.findById(created.getId()));
    }

    @Test
    public void test_delete() throws Exception {
        setLoggedInUser(systemMaintenanceUser);

        // save a new alert
        AlertResource alertResource = AlertResourceBuilder.newAlertResource()
                .withId()
                .build();

        AlertResource created = controller.create(alertResource).getSuccess();

        // check that it can be found
        assertNotNull(created.getId());
        alertResource.setId(created.getId());
        assertEquals(alertResource, controller.findById(created.getId()).getSuccess());

        // now delete it
        controller.delete(created.getId()).getSuccess();

        // make sure it can't be found
        assertTrue(controller.findById(created.getId()).isFailure());
    }

    @Test
    public void test_deleteAllByType() throws Exception {
        setLoggedInUser(systemMaintenanceUser);

        controller.deleteAllByType(AlertType.MAINTENANCE);

        // make sure they can't be found
        assertTrue(controller.findAllVisibleByType(MAINTENANCE).getSuccess().isEmpty());
    }
}

@@ -916,16 +916,102 @@
 ENANCE;%0A
+import static org.innovateuk.ifs.base.amend.BaseBuilderAmendFunctions.clearUniqueIds;%0A
 import s
@@ -1082,24 +1082,24 @@
 curityUser;%0A
-
 import stati
@@ -1695,24 +1695,50 @@
 )).build();%0A
+        clearUniqueIds();%0A
     %7D%0A%0A    @
@@ -2444,20 +2444,8 @@
 Ago)
-.withId(12L)
 %0A   
@@ -2591,32 +2591,32 @@
 AlertResource()%0A
+
                 
@@ -2651,20 +2651,8 @@
 Ago)
-.withId(13L)
 %0A   

Fix integration test