package cz.moneta.test.regression.commercial.auto.smartauto;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.auto.smartauto.NewCalculationPage;
import cz.moneta.test.dsl.auto.smartauto.shared.*;
import cz.moneta.test.dsl.auto.smartauto.util.MonthUtil;
import cz.moneta.test.dsl.util.credentials.Keys.smart;
import cz.moneta.test.dsl.util.task.auto.smartauto.SmartAutoTasks;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import cz.moneta.test.regression.commercial.auto.smartauto.mockdata.SharedMock;
import org.junit.jupiter.api.Disabled;

import java.util.function.Function;

@TestScenario(name = "Successful created complete application.")
public class CompleteApplicationTest {

    @Disabled
    @JiraTestCase(id = "LAMD-T967")
    @TestCase(name = "Customer: businessman, Commodity: car, Product: C2C")
    public void tcCompleteApplicationBusinessmanCar(Harness harness, @Key(smart.auto.general) Credentials credentials) {
        SmartAutoTasks smartAutoTasks = harness.tasks().auto().smartAuto();
        CalculationData calculationData = prepareCalculationData(Customer.BUSINESSMAN_WITHOUT_VAT, PrGroup.CAR, Group.M1, Brand.SKODA, Model.FABIA, Product.AUTO_CREDIT_C2C, "TMBPH16Y013217915", null);

        ContactData contactData = SharedMock.prepareContactDataMan();
        IdentificationData identificationData = SharedMock.prepareIdentificationDataMan();
        CompanyData companyData = SharedMock.prepareCompanyData();

        harness.withSmartAuto()
                .openLoginPage()
                .then(smartAutoTasks.login().loginWithCredentials(credentials))
                .then(smartAutoTasks.calculationType().makeCalculation(calculationData))
                .then(smartAutoTasks.insuranceTasks().fillCustomerData(calculationData, smartAutoTasks, companyData))
                .then(smartAutoTasks.insuranceTasks().fillInsuranceData(calculationData, smartAutoTasks))
                .then(smartAutoTasks.calculationType().closeInsuranceModal())
                .then(smartAutoTasks.verification().checkNewCalculation())
                .then(smartAutoTasks.save().saveNewCalculationPage())
                .clickCreateApplication()
                .then(smartAutoTasks.contact().fillBusinessmanInformation(companyData))
                .then(smartAutoTasks.identification().fillFirstRepresentativeIdentification(contactData, identificationData))
                .then(smartAutoTasks.bankContact().fillBusinessmanBankContact("212345", BankCode.KOMERCNI_BANKA))
                .then(smartAutoTasks.financial().clickDoubleEntry())
                .then(smartAutoTasks.validation().validateCustomerPage())
                .then(smartAutoTasks.data().checkVehicleInformation(Brand.SKODA, Model.FABIA))
                .then(smartAutoTasks.data().checkAndAddAdditionalData(FilterFuel.PETROL, 5000, "Popis vozidla", ImportVehicle.NO, DealerIsOwner.NO))
                .then(smartAutoTasks.validation().validateVehiclePage())
                .then(smartAutoTasks.account().fillOthersPageForC2CBussiness(721456790, 19, BankCode.KOMERCNI_BANKA, "Testuji automaticky :)", "VSV Group s.r.o.", "24789712"))
                .then(smartAutoTasks.validation().validateOthersPage())
                .then(smartAutoTasks.application().sendApplication());
    }

    @Disabled
    @JiraTestCase(id = "LAMD-T968")
    @TestCase(name = "Customer: consumer, Commodity: motorcycle, Product: Standard")
    public void tcCompleteApplicationConsumerMotorcycle(Harness harness, @Key(smart.auto.general) Credentials credentials) {
        SmartAutoTasks smartAutoTasks = harness.tasks().auto().smartAuto();
        CalculationData calculationData = prepareCalculationData(Customer.CONSUMER, PrGroup.MOTORCYCLE, Group.LC, Brand.HARLEY, Model.SOFTAIL, Product.MOTO_CREDIT_STANDARD, "1HD1JRV15DB031048", FilterFuel.PETROL);
        calculationData.setYear(Year._2023).setMonth("Prosinec").setPrClass(PrClass.CARUSED);

        ContactData contactData = SharedMock.prepareContactDataWoman();
        IdentificationData identificationData = SharedMock.prepareIdentificationDataWoman();
        CompanyData companyData = SharedMock.prepareCompanyData();

        harness.withSmartAuto()
                .openLoginPage()
                .then(smartAutoTasks.login().loginWithCredentials(credentials))
                .then(smartAutoTasks.calculationType().makeCalculationWithRegistrationDate(calculationData))
                .then(smartAutoTasks.insuranceTasks().fillCustomerData(calculationData, smartAutoTasks, contactData, identificationData))
                .then(smartAutoTasks.insuranceTasks().fillInsuranceData(calculationData, smartAutoTasks))
                .then(smartAutoTasks.calculationType().closeInsuranceModal())
                .then(editInstalmentComputation())
                .then(smartAutoTasks.verification().checkNewCalculation())
                .clickCreateApplication()
                .then(smartAutoTasks.contact().fillContactInformation(contactData))
                .then(smartAutoTasks.identification().fillIdentification(identificationData))
                .then(smartAutoTasks.bankContact().fillBankContact("212345", BankCode.KOMERCNI_BANKA))
                .then(smartAutoTasks.personalData().fillPersonalData(MaritalStatus.SINGLE, 2, HousingKind.OWN_FLAT, Education.COLLEGE))
                .then(smartAutoTasks.employment().fillEmploymentCertainPeriodNoticePeriod("01.01.2018", "01.01.2035", "01.01.2035", "SW Developer"))
                .then(smartAutoTasks.revenuesAndExpenses().fillRevenuesAndExpensesNoSource(300000, 500000, 10000, 10000))
                .then(smartAutoTasks.company().fillCompanyRegistered(companyData))
                .then(smartAutoTasks.save().saveCustomerPage())
                .then(smartAutoTasks.validation().validateCustomerPage())
                .then(smartAutoTasks.data().checkVehicleInformation(Brand.HARLEY, Model.SOFTAIL))
                .then(smartAutoTasks.search().searchVehicleWithFilters(FilterFuel.PETROL, null, FilterBodywork.OTHERS, FilterGearbox.MANUAL, FilterDoors.ZERO, Model.SOFTAIL_BREAKOUT))
                .then(smartAutoTasks.data().checkAndAddAdditionalData(FilterFuel.PETROL, 5000, "Popis vozidla", ImportVehicle.NO, DealerIsOwner.NO))
                .then(smartAutoTasks.save().saveVehiclePage())
                .then(smartAutoTasks.validation().validateVehiclePage())
                .then(smartAutoTasks.account().fillOthersPageForStandard(721456790, "Testuji automaticky :)"))
                .then(smartAutoTasks.save().saveOthersPage())
                .then(smartAutoTasks.validation().validateOthersPage())
                .then(smartAutoTasks.application().sendApplication());
    }

    @JiraTestCase(id = "LAMD-T943")
    @TestCase(name = "Customer: businessman, Commodity: caravan, Product: Back loan")
    public void tcCompleteApplicationBusinessmanCaravan(Harness harness, @Key(smart.auto.general) Credentials credentials) {
        SmartAutoTasks smartAutoTasks = harness.tasks().auto().smartAuto();
        CalculationData calculationData = prepareCalculationData(Customer.BUSINESSMAN_WITHOUT_VAT, PrGroup.CAR, Group.M1, Brand.FIAT, Model.BRAVO, Product.AUTO_CREDIT_BACK_LOAN, "ZFA18200004677459", null);

        calculationData.setUsage(Usage.CARAVAN);
        calculationData.setYear(Year._2019).setMonth("Prosinec").setPrClass(PrClass.CARUSED);

        ContactData contactData = SharedMock.prepareContactDataMan();
        IdentificationData identificationData = SharedMock.prepareIdentificationDataMan();
        CompanyData companyData = SharedMock.prepareCompanyData();

        harness.withSmartAuto()
                .openLoginPage()
                .then(smartAutoTasks.login().loginWithCredentials(credentials))
                .then(smartAutoTasks.calculationType().makeCalculationWithRegistrationDate(calculationData))
                .then(smartAutoTasks.insuranceTasks().fillCustomerData(calculationData, smartAutoTasks, companyData))
                .then(smartAutoTasks.insuranceTasks().fillInsuranceData(calculationData, smartAutoTasks))
                .then(smartAutoTasks.calculationType().closeInsuranceModalNoInsurance())
                .then(smartAutoTasks.verification().checkNewCalculation())
                .clickCreateApplication()
                .then(smartAutoTasks.contact().fillBusinessmanInformation(companyData))
                .then(smartAutoTasks.identification().fillFirstRepresentativeIdentification(contactData, identificationData))
                .then(smartAutoTasks.bankContact().fillBusinessmanBankContact("212345", BankCode.KOMERCNI_BANKA))
                .then(smartAutoTasks.financial().fillDoubleEntry(100000, 50000, 100000, 100000, 100000))
                .then(smartAutoTasks.validation().validateCustomerPage())
                .then(smartAutoTasks.data().checkVehicleInformation(Brand.FIAT, Model.BRAVO))
                .then(smartAutoTasks.data().checkAndAddAdditionalData(FilterFuel.OIL, 5000, "Popis vozidla", ImportVehicle.NO, DealerIsOwner.NO))
                .then(smartAutoTasks.validation().validateVehiclePage())
                .then(smartAutoTasks.account().fillOthersPageForStandard(721456790, "Testuji automaticky :)"))
                .then(smartAutoTasks.validation().validateOthersPage())
                .then(smartAutoTasks.application().sendApplication());
    }

    @JiraTestCase(id = "LAMD-T1044")
    @TestCase(name = "Customer: consumer, Commodity: trailer, Product: C2C")
    public void tcCompleteApplicationConsumerTrailer(Harness harness, @Key(smart.auto.general) Credentials credentials) {
        SmartAutoTasks smartAutoTasks = harness.tasks().auto().smartAuto();
        CalculationData calculationData = prepareCalculationData(Customer.CONSUMER, PrGroup.TRAILER, Group.O1, Brand.AGADOS, Model.HANDY, Product.AUTO_CREDIT_C2C, "ZFA18200004677459", FilterFuel.WITHOUT_FUEL);
        calculationData.setUsage(Usage.OTHERS);
        calculationData.setYear(Year._2019).setMonth("Leden").setPrClass(PrClass.CARUSED);

        ContactData contactData = SharedMock.prepareContactDataWoman();
        IdentificationData identificationData = SharedMock.prepareIdentificationDataWoman();
        CompanyData companyData = SharedMock.prepareCompanyData();

        harness.withSmartAuto()
                .openLoginPage()
                .then(smartAutoTasks.login().loginWithCredentials(credentials))
                .then(smartAutoTasks.calculationType().makeCalculationWithRegistrationDate(calculationData))
                .then(smartAutoTasks.insuranceTasks().fillCustomerData(calculationData, smartAutoTasks, contactData, identificationData))
                .then(smartAutoTasks.insuranceTasks().fillInsuranceData(calculationData, smartAutoTasks))
                .then(smartAutoTasks.calculationType().closeInsuranceModalNoInsurance())
                .then(smartAutoTasks.verification().checkNewCalculation())
                .clickCreateApplication()
                .then(smartAutoTasks.contact().fillContactInformation(contactData))
                .then(smartAutoTasks.identification().fillIdentification(identificationData))
                .then(smartAutoTasks.bankContact().fillBankContact("212345", BankCode.KOMERCNI_BANKA))
                .then(smartAutoTasks.personalData().fillPersonalData(MaritalStatus.SINGLE, 2, HousingKind.OWN_FLAT, Education.COLLEGE))
                .then(smartAutoTasks.employment().fillEmploymentCertainPeriodNoticePeriod("01.01.2018", "01.01.2036", "01.01.2036", "SW Developer"))
                .then(smartAutoTasks.revenuesAndExpenses().fillRevenuesAndExpensesNoSource(300000, 500000, 100000, 100000))
                .then(smartAutoTasks.company().fillCompanyRegistered(companyData))
                .then(smartAutoTasks.validation().validateCustomerPage())
                .then(smartAutoTasks.data().doNotHaveVIN())
                .then(smartAutoTasks.data().checkVehicleInformation(Brand.AGADOS, Model.HANDY))
                .then(smartAutoTasks.data().fillFilterFuel2(FilterFuel.WITHOUT_FUEL, 5000, "Popis vozidla", ImportVehicle.NO, DealerIsOwner.NO))
                .then(smartAutoTasks.validation().validateVehiclePage())
                .then(smartAutoTasks.account().fillOthersPageForC2CConsumer(721456790, 19, BankCode.KOMERCNI_BANKA, "Testuji automaticky :)", "František", "Tester", "19.02.2002"))
                .then(smartAutoTasks.validation().validateOthersPage())
                .then(smartAutoTasks.application().sendApplication());
    }

    private Function<NewCalculationPage, NewCalculationPage> editInstalmentComputation() {
        return start -> start.editInstalmentComputation(((Integer) 30).toString())
                .lostFocus();
    }

    private CalculationData prepareCalculationData(Customer customer, PrGroup prGroup, Group group, Brand brand, Model model, Product product, String vin, FilterFuel fuel) {
        CalculationData calculationData = new CalculationData();
        return calculationData.setCustomer(customer)
                .setPrClass(PrClass.CARNEW)
                .setPrGroup(prGroup)
                .setGroup(group)
                .setBrand(brand)
                .setModel(model)
                .setVin(vin)
                .setFuel(fuel)
                .setWeight(0)
                .setVolume(0)
                .setNumberOfSeats(0)
                .setPower(0)
                .setPrice(500000)
                .setVat(Vat.NO)
                .setUsage(Usage.OTHERS)
                .setMonth(MonthUtil.getCurrentMonth())
                .setProduct(product);
    }
}