package cz.moneta.test.regression.channels.digital.web;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.forte.components.ForteRoles;
import cz.moneta.test.dsl.forte.pages.appointments.AppointmentDetail;
import cz.moneta.test.dsl.monetaportal.components.Web2Branch;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.data.client.NonClient;
import cz.moneta.test.dsl.util.task.monetaportal.responses.GetContactsResponse;
import cz.moneta.test.dsl.util.task.monetaportal.responses.Opportunity;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import org.junit.jupiter.api.Assertions;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import static cz.moneta.test.dsl.monetaportal.enums.SearchBranchName.CESKE_BUDEJOVICE;
import static cz.moneta.test.harness.constants.HarnessConfigConstants.SELENIUM_GRID_PLATFORM;

@TestScenario(name = "Check W2B")
public class W2BTest {

    @TestCase(name = "W2B retail", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "NWP-T761")
    public void tcW2B(Harness harness) {
        int testHash = new Date().hashCode();
        NonClient client = harness.data().newNonClient()
                .withEmail("automat" + testHash + "@test.cz")
                .withMobileNumber("602475132");

        String note = "Message" + testHash;

        harness.withMonetaPortal()
                .openHomePage()
                .acceptCookies()
                .openLoansMenu()
                .openExpressLoanPage()
                .clickArrangeAppointmentAtBranch()
                .fillW2BSearchBranch(CESKE_BUDEJOVICE.getValue())
                .clickW2BSearchResultBranches(CESKE_BUDEJOVICE)
                .clickW2BArrangeAppointment()
                .clickAppointmentDate().clickFirstOption()
                .clickAppointmentTime().clickFirstOption()
                .storeAppointmentDateTime("d.M.yyyy")
                .clickW2BContinue()
                .fillW2BClientFullName(client.fullName())
                .fillW2BClientPhoneNumber(client.mobileNumber())
                .fillW2BClientEmail(client.email())
                .fillW2BMessage(note)
                .clickW2BSubmit()
                .checkW2BSuccessMessage();

        // TODO: Absolutely temporary solution!!!
        harness.putConfig(SELENIUM_GRID_PLATFORM, "Windows");
        harness.putConfig("selenium.webdriver.chrome.options", "--kiosk-printing --no-first-run --disable-gpu --enable-logging --v=1 --disable-crash-reporter --disable-extensions --disable-dev-shm-usage --window-size=1920,1080 --proxy-server=http://wsa.mbid.cz:8008 --proxy-bypass-list=*.verisign.com,fonts.googleapis.com,assets.adobedtm.com,smetrics.internetbanka.cz,cdn.mxpnl.com,demdex.net,forgetest*,fortetest*,forteppe* --acceptInsecureCerts=true --ignore-certificate-errors=true --use-fake-device-for-media-stream --use-fake-ui-for-media-stream");

        AppointmentDetail detail = harness
                .withForte()
                .openForteHomePage()
                .clickAppointments()
                .selectRoleMenu(ForteRoles.BANKER)
                .selectOm(CESKE_BUDEJOVICE.getValue())
                .clickNotAssignedAppointments()
                .fillClientFilter(client.fullName())
                .fillNoteFilter(note)
                .clickSubmitFilter()
                .clickFirstAppointmentDetail();

        Assertions.assertEquals(client.fullName(), detail.getClientName());
        Assertions.assertEquals("+420" + client.mobileNumber(), detail.getClientPhoneNumber());
        Assertions.assertEquals(client.email(), detail.getClientEmail());
        Assertions.assertEquals("Expres", detail.getAppointmentProduct());
        Assertions.assertEquals(harness.get("APPOINTMENT_DATE"), detail.getAppointmentDate());
        Assertions.assertEquals(harness.get("APPOINTMENT_TIME"), detail.getAppointmentTimeFrom());
        Assertions.assertEquals("Schůzka z nového webu - Web", detail.getAppointmentSource());
        Assertions.assertEquals(note, detail.getAppointmentNote());
    }

    @TestCase(name = "W2B business", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "NWP-T762")
    public void tcW2B_business(Harness harness) {
        int testHash = new Date().hashCode();
        String branchId = "86001340";
        NonClient client = harness.data().newNonClient()
                .withEmail("automattest" + testHash + "@test.cz")
                .withMobileNumber("602475132")
                .withIco("07470975");

        String note = "Message" + testHash;

        Web2Branch w2b = harness.withMonetaPortal()
                .openHomePage()
                .acceptCookies()
                .openLoansMenu()
                .openExpressLoanPage()
                .clickArrangeAppointmentAtBranch()
                .fillW2BSearchBranch(CESKE_BUDEJOVICE.getValue())
                .clickW2BSearchResultBranches(CESKE_BUDEJOVICE)
                .clickW2BArrangeAppointment()
                .clickHasIco()
                .clickAppointmentDate().clickFirstOption()
                .clickAppointmentTime().clickFirstOption()
                .storeAppointmentDateTime("yyyy-MM-dd")
                .clickW2BContinue()
                .fillW2BClientICO(client.ico())
                .fillW2BClientFullName(client.fullName())
                .fillW2BClientPhoneNumber(client.mobileNumber())
                .fillW2BClientEmail(client.email())
                .fillW2BMessage(note);

        String now = DateUtils.getDateWithFormat(new Date(), "yyyy-MM-dd HH:mm:ss");

        w2b.clickW2BSubmit()
                .checkW2BSuccessMessage();

        List<Opportunity> opportunities = harness.tasks().monetaPortal().wso2Tasks()
                .getOpportunitiesForClient(now, client, branchId).RS_DATA.stream().filter(o ->
                        Objects.equals(o.CONTACT_NAME, client.fullName())).toList();

        Assertions.assertFalse(opportunities.isEmpty(),
                "Opportunity with ico: " + client.ico() + ", branch " + branchId + " and with name " + client.fullName() + " doesn't exists.");

        String expectedDate = harness.get("APPOINTMENT_DATE").toString();
        String actualDate = opportunities.get(0).CONTACT_DATE;
        Assertions.assertTrue(actualDate.contains(expectedDate), "expected: " + expectedDate + ", actual: " + actualDate);

        String expectedTime = harness.get("APPOINTMENT_TIME").toString();
        String actualTime = opportunities.get(0).CONTACT_TIME_NOTE;
        Assertions.assertTrue(expectedTime.contains(actualTime), "expected: " + expectedTime + ", actual: " + actualTime);

        Assertions.assertEquals(opportunities.get(0).DESCRIPTION,note);

        GetContactsResponse contacts = harness.tasks().monetaPortal().wso2Tasks().getContacts(client);
        Assertions.assertFalse(contacts.RS_DATA.stream().filter(c ->
                Objects.equals(c.FIRST_NAME, client.firstName()) &&
                        Objects.equals(c.LAST_NAME, client.lastName()) &&
                        Objects.equals(c.MOBILE, "+420" + client.mobileNumber()) &&
                        Objects.equals(c.EMAIL, client.email())).toList().isEmpty());
    }
}

