package cz.moneta.test.dsl.util.task.auto.smartauto.newcalculation;

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.util.task.auto.smartauto.SmartAutoTasks;

import java.util.function.Function;

public class CalculationTypeTasks {

    private final Harness harness;
    private SmartAutoTasks smartAutoTasks;

    public CalculationTypeTasks(Harness harness) {
        this.harness = harness;
        this.smartAutoTasks = harness.tasks().auto().smartAuto();
    }

    public Function<NewCalculationPage, NewCalculationPage> makeCalculation(CalculationData calculationData) {
        return start -> start
                .clickCustomer(calculationData.getCustomer())
                .clickVehicleCategoryValue(Category.VEHICLES)
                .then(smartAutoTasks.prClassTasks().choosePrClass(calculationData.getPrClass()))
                .then(smartAutoTasks.prGroupTasks().choosePrGroup(calculationData.getPrGroup()))
                .then(smartAutoTasks.brandModel().chooseBrandAndModel(calculationData.getBrand(), calculationData.getModel()))
                .then(smartAutoTasks.group().chooseGroup(calculationData.getGroup()))
                .typePrice(calculationData.getPrice())
                .clickVat(calculationData.getVat())
                .then(smartAutoTasks.usage().chooseUsage(calculationData.getUsage()))
                .then(smartAutoTasks.date().chooseRegistrationDate(calculationData.getMonth()))
                .then(smartAutoTasks.financialProduct().chooseFinancialProduct(calculationData.getProduct()));
    }

    public Function<NewCalculationPage, NewCalculationPage> makeSubsidyCalculation(CalculationData calculationData, CompanyData companyData, ContactData contactData, IdentificationData identificationData, SmartAutoTasks smartAutoTasks) {
        return start -> start
                .clickCustomer(calculationData.getCustomer())
                .clickVehicleCategoryValue(Category.VEHICLES)
                .then(smartAutoTasks.prClassTasks().choosePrClass(calculationData.getPrClass()))
                .then(smartAutoTasks.prGroupTasks().choosePrGroup(calculationData.getPrGroup()))
                .then(smartAutoTasks.brandModel().chooseBrandAndModel(calculationData.getBrand(), calculationData.getModel()))
                .then(smartAutoTasks.group().chooseGroup(calculationData.getGroup()))
                .typePrice(calculationData.getPrice())
                .clickVat(calculationData.getVat())
                .then(smartAutoTasks.usage().chooseUsage(calculationData.getUsage()))
                .then(smartAutoTasks.date().chooseRegistrationDate(calculationData.getMonth()))
                .then(smartAutoTasks.SubsidyTasks().chooseSubsidy(calculationData.getSubsidy()))
                .then(smartAutoTasks.financialProduct().chooseFinancialProduct(calculationData.getProduct()))
                .then(smartAutoTasks.insuranceTasks().fillCustomerData(calculationData, smartAutoTasks, contactData, identificationData, companyData))
                .typeFirstPayment(calculationData.getFirstPayment());
    }

    public Function<NewCalculationPage, NewCalculationPage> makeCalculationAgro(CalculationData calculationData) {
        return start -> start
                .clickCustomer(calculationData.getCustomer())
                .clickVehicleCategoryValue(Category.AGRO)
                .then(smartAutoTasks.prClassTasks().choosePrClass(calculationData.getPrClass()))
                .then(smartAutoTasks.prGroupTasks().choosePrGroup(calculationData.getPrGroup()))
                .then(smartAutoTasks.group().chooseGroup(calculationData.getGroup()))
                .then(smartAutoTasks.brandModel().chooseAgroBrand(calculationData.getBrand()))
                .then(smartAutoTasks.brandModel().chooseAgroModel(calculationData.getModel()))
                .then(smartAutoTasks.itemType().chooseItemType(calculationData.getItemType()))
                .typePrice(calculationData.getPrice())
                .clickVat(calculationData.getVat())
                .then(smartAutoTasks.usage().chooseUsage(calculationData.getUsage()))
                .then(smartAutoTasks.date().chooseAgroDate())
                .then(smartAutoTasks.agroDonationsTasks().chooseAgroDonations())
                .then(smartAutoTasks.financialProduct().chooseFinancialProductAdjust(calculationData));
    }

    public Function<NewCalculationPage, NewCalculationPage> makeCalculationAgroDeferredDrawing(CalculationData calculationData) {
        return start -> start
                .clickCustomer(calculationData.getCustomer())
                .clickVehicleCategoryValue(Category.AGRO)
                .then(smartAutoTasks.prClassTasks().choosePrClass(calculationData.getPrClass()))
                .then(smartAutoTasks.prGroupTasks().choosePrGroup(calculationData.getPrGroup()))
                .then(smartAutoTasks.group().chooseGroup(calculationData.getGroup()))
                .then(smartAutoTasks.brandModel().chooseAgroBrand(calculationData.getBrand()))
                .then(smartAutoTasks.brandModel().chooseAgroModel(calculationData.getModel()))
                .then(smartAutoTasks.itemType().chooseItemType(calculationData.getItemType()))
                .typePrice(calculationData.getPrice())
                .clickVat(calculationData.getVat())
                .then(smartAutoTasks.usage().chooseUsage(calculationData.getUsage()))
                .then(smartAutoTasks.calculationType().addDeferredDrawing())
                .then(smartAutoTasks.agroDonationsTasks().chooseAgroDonations())
                .then(smartAutoTasks.financialProduct().chooseFinancialProductAdjust(calculationData));
    }

    public Function<NewCalculationPage, NewCalculationPage> makeCalculationAgroWithoutTP(CalculationData calculationData) {
        return start -> start
                .clickCustomer(calculationData.getCustomer())
                .clickVehicleCategoryValue(Category.AGRO)
                .then(smartAutoTasks.prClassTasks().choosePrClass(calculationData.getPrClass()))
                .then(smartAutoTasks.prGroupTasks().choosePrGroup(calculationData.getPrGroup()))
                .then(smartAutoTasks.group().chooseGroup(calculationData.getGroup()))
                .typePrice(calculationData.getPrice())
                .clickVat(calculationData.getVat())
                .then(smartAutoTasks.usage().chooseUsage(calculationData.getUsage()))
                .then(smartAutoTasks.date().chooseAgroDate())
                .then(smartAutoTasks.brandModel().chooseAgroBrandWithoutTP(calculationData.getBrand()))
                .then(smartAutoTasks.brandModel().chooseAgroModel(calculationData.getModel()))
                .then(smartAutoTasks.brandModel().clickAgroModel(calculationData.getModel()))
                .then(smartAutoTasks.agroDonationsTasks().chooseAgroDonations())
                .then(smartAutoTasks.financialProduct().chooseFinancialProductAdjust(calculationData));
    }

    public Function<NewCalculationPage, NewCalculationPage> makeCalculationAgroWithoutTPDeferredDrawing(CalculationData calculationData) {
        return start -> start
                .clickCustomer(calculationData.getCustomer())
                .clickVehicleCategoryValue(Category.AGRO)
                .then(smartAutoTasks.prClassTasks().choosePrClass(calculationData.getPrClass()))
                .then(smartAutoTasks.prGroupTasks().choosePrGroup(calculationData.getPrGroup()))
                .then(smartAutoTasks.group().chooseGroup(calculationData.getGroup()))
                .typePrice(calculationData.getPrice())
                .clickVat(calculationData.getVat())
                .then(smartAutoTasks.usage().chooseUsage(calculationData.getUsage()))
                .then(smartAutoTasks.calculationType().addDeferredDrawing())
                .then(smartAutoTasks.brandModel().chooseAgroBrandWithoutTP(calculationData.getBrand()))
                .then(smartAutoTasks.brandModel().chooseAgroModel(calculationData.getModel()))
                .then(smartAutoTasks.brandModel().clickAgroModel(calculationData.getModel()))
                .then(smartAutoTasks.agroDonationsTasks().chooseAgroDonations())
                .then(smartAutoTasks.financialProduct().chooseFinancialProductAdjust(calculationData));
    }

    public Function<NewCalculationPage, NewCalculationPage> makeCalculationWithRegistrationDate(CalculationData calculationData) {
        return start -> start
                .clickCustomer(calculationData.getCustomer())
                .then(smartAutoTasks.prClassTasks().choosePrClass(calculationData.getPrClass()))
                .then(smartAutoTasks.prGroupTasks().choosePrGroup(calculationData.getPrGroup()))
                .then(smartAutoTasks.brandModel().chooseBrandAndModel(calculationData.getBrand(), calculationData.getModel()))
                .then(smartAutoTasks.group().chooseGroup(calculationData.getGroup()))
                .typePrice(calculationData.getPrice())
                .clickVat(calculationData.getVat())
                .then(smartAutoTasks.usage().chooseUsage(calculationData.getUsage()))
                .then(smartAutoTasks.date().chooseRegistrationDate(calculationData.getYear(), calculationData.getMonth()))
                .then(smartAutoTasks.financialProduct().chooseFinancialProduct(calculationData.getProduct()));
    }

    public Function<NewCalculationPage, NewCalculationPage> fillInsuranceCustomerData(ContactData customer, IdentificationData customerData) {
        return start -> start
                .clickVehicleCarInsurance()
                .typeVehicleInsuranceCustomerName(customer.getName())
                .typeVehicleInsuranceCustomerSurname(customer.getSurname())
                .typeVehicleInsuranceCustomerBirthdate(customerData.getBirthdate().getValue())
                .typeVehicleInsuranceCustomerPIN(customerData.getPin().getValue())
                .typeVehicleInsuranceCustomerNationality(customerData.getNationality().getValue())
                .clickVehicleInsuranceCustomerNationality()
                .typeVehicleInsuranceCustomerAddress(customer.getAddress())
                .clickVehicleInsuranceCustomerAddress();
    }

    public Function<NewCalculationPage, NewCalculationPage> fillInsuranceFirmData(CompanyData company) {
        return start -> start
                .clickVehicleCarInsurance()
                .typeVehicleInsuranceFirmName(company.getCompany().toString())
                .clickVehicleInsuranceFirmSearch()
                .clickVehicleInsuranceFirmField();
    }

    public Function<NewCalculationPage, NewCalculationPage> fillInsuranceFirmDataFallBack(CompanyData company) {
        return start -> start
                .clickVehicleCarInsurance()
                .typeVehicleInsuranceFirmName(company.getCompany().toString())
                .clickVehicleInsuranceFirmSearch()
                .clickVehicleInsuranceFirmField()
                .typeCompanyName(company.getCompanyName())
                .clickCompanyType()
                .chooseCompanyType()
                .typeCompanyTownship(company.getCompanyCity())
                .typeCompanyCity(company.getCompanyCity())
                .typeCompanyIco(12312312)
                .typeCompanyResidenceNumber(company.getCompanyResidenceNumber())
                .typeCompanyZip(12345)
                .chooseCompanyResidenceType()
                .clickSave();
    }

    public Function<NewCalculationPage, NewCalculationPage> fillInsuranceData(CalculationData vehicleData) {
        return start -> start
                .clickVehicleInsuranceVehicleTab()
                .typeVehicleInsuranceVehicleVIN(vehicleData.getVin())
                .clickVehicleInsuranceVehicleVINSearch()
                .clickVehicleInsuranceVehicleType()
                .clickVehicleInsuranceInsuranceTab();
    }

    public Function<NewCalculationPage, NewCalculationPage> fillInsuranceDataNoVin(CalculationData vehicleData) {
        return start -> start
                .clickVehicleInsuranceVehicleTab()
                .clickVehicleInsuranceVehicleFuelType()
                .chooseVehicleInsuranceVehicleFuelType(vehicleData.getFuel())
                .clickVehicleInsuranceVehicleFilterSearch()
                .clickVehicleInsuranceVehicleType()
                .clickVehicleInsuranceInsuranceTab();
    }

    public Function<NewCalculationPage, NewCalculationPage> fillInsuranceDataTrailer(CalculationData vehicleData) {
        return start -> start
                .clickVehicleInsuranceVehicleTab()
                .clickVehicleInsuranceVehicleFuelType()
                .chooseVehicleInsuranceVehicleFuelType(vehicleData.getFuel())
                .clickVehicleInsuranceVehicleFilterSearch()
                .clickVehicleInsuranceVehicleType()
                .typeVehicleInsuranceVehicleVIN(vehicleData.getVin())
                .typeVehicleInsuranceVehiclePower(vehicleData.getPower())
                .typeVehicleInsuranceVehicleVolume(vehicleData.getVolume())
                .typeVehicleInsuranceVehicleNumberOfSeats(vehicleData.getNumberOfSeats());
    }

    public Function<NewCalculationPage, NewCalculationPage> fillInsuranceDataAgro(CalculationData vehicleData) {
        return start -> start
                .clickVehicleInsuranceVehicleTab()
                .typeVehicleInsuranceVehicleVolume(vehicleData.getVolume())
                .typeVehicleInsuranceVehiclePower(vehicleData.getPower())
                .typeVehicleInsuranceVehicleWeight(vehicleData.getWeight())
                .clickVehicleInsuranceVehicleFuelTypeAgro()
                .chooseVehicleInsuranceVehicleFuelTypeAgro(vehicleData.getFuel())
                .clickVehicleInsuranceInsuranceTab();
    }

    public Function<NewCalculationPage, NewCalculationPage> addPovInsurance() {
        return start -> start
                .clickVehicleInsuranceInsurancePovInsurance()
                .clickVehicleInsuranceInsurancePovInsuranceChoose();
    }

    public Function<NewCalculationPage, NewCalculationPage> addHavInsurance() {
        return start -> start
                .clickVehicleInsuranceInsuranceHavInsurance()
                .clickVehicleInsuranceInsuranceHavInsuranceChoose();
    }

    public Function<NewCalculationPage, NewCalculationPage> closeInsuranceModal() {
        return start -> start
                .scrollToChoose()
                .clickVehicleInsuranceInsuranceChoose()
                .waitForCalculationOutput();
    }
    public Function<NewCalculationPage, NewCalculationPage> closeInsuranceModalNoInsurance() {
        return start -> start
                .clickVehicleInsuranceInsuranceClose()
                .waitForCalculationOutput();
    }

    public Function<NewCalculationPage, NewCalculationPage> addAccidentInsurance(AccidentInsurance accidentInsurance) {
        return start -> start
                .clickVehicleAccidentInsurance()
                .clickVehicleAccidentInsuranceOption(accidentInsurance)
                .clickVehicleAccidentInsuranceChoose()
                .waitForCalculationOutput();
    }

    public Function<NewCalculationPage, NewCalculationPage> addBenefit(Benefit benefit) {
        return start -> start
                .clickVehicleBenefit()
                .clickVehicleBenefitOption(benefit)
                .clickVehicleBenefitChoose()
                .waitForCalculationOutput()
                .waitForCalculationInstalmentOutput();
    }

    public Function<NewCalculationPage, NewCalculationPage> addDeferredDrawing() {
        return NewCalculationPage::clickDeferredDrawing;
    }
}
