package cz.moneta.test.dsl.util.task.ufo.cb;

import cz.moneta.test.dsl.ufo.cb.landing.IKL402_S001_ClientPage;
import cz.moneta.test.dsl.ufo.cb.landing.KOM401_S001_ProposalParameters;
import cz.moneta.test.dsl.ufo.cb.landing.KOM402_S001_ProposalSignpost;
import cz.moneta.test.dsl.ufo.cb.landing.SME451_S001_UfoCbLandingPage;
import cz.moneta.test.dsl.ufo.cb.landing.proposals.Proposal;
import cz.moneta.test.dsl.util.DateUtils;

import java.time.LocalDate;
import java.util.function.Function;

import static cz.moneta.test.dsl.util.DateFormatPatterns.CZ_DATE_FORMAT;

public final class ProposalTasks {
    public ProposalTasks() {

    }

    public String getValidDrawingDate() {
        LocalDate localDate = LocalDate.now();
        localDate = localDate.plusMonths(1);
        localDate = localDate.withDayOfMonth(19);
        return DateUtils.getDateWithFormat(localDate, CZ_DATE_FORMAT);
    }

    public Function<IKL402_S001_ClientPage, KOM401_S001_ProposalParameters> createProposal() {
        return start -> start.clickNewProposal().clickOpportunity().clickContinue();
    }

    private Function<KOM401_S001_ProposalParameters, KOM401_S001_ProposalParameters> fillBasicParameters(Proposal proposal) {
        return start -> start.typeAmount(proposal.getAmount())
                .selectCharacterType(proposal.getCharacterType());
    }

    private Function<KOM401_S001_ProposalParameters, KOM401_S001_ProposalParameters> fillDates(Proposal proposal) {
        return start -> start.typeSignDate(proposal.getSignDate())
                .typeLastDrawingDate(getValidDrawingDate());
    }

    private Function<KOM401_S001_ProposalParameters, KOM401_S001_ProposalParameters> fillProposalLoanType(Proposal proposal) {
        return start -> start.selectProposalType(proposal.getProposalType())
                .selectLoanType(proposal.getLoanType());
    }

    private Function<KOM401_S001_ProposalParameters, KOM401_S001_ProposalParameters> fillProductTypeLoanPurpose(Proposal proposal) {
        return start -> start.selectProductType(proposal.getProductType())
                .selectLoanPurpose(proposal.getLoanPurpose());
    }

    public Function<KOM401_S001_ProposalParameters, KOM401_S001_ProposalParameters> fillProposalEuInvestment(Proposal proposal) {
        return start -> start.then(fillBasicParameters(proposal))
                .then(fillProposalLoanType(proposal))
                .selectLoanPurpose(proposal.getLoanPurpose())
                .then(fillDates(proposal))
                .selectSupport(proposal.getSupport())
                .selectRwaFlag(proposal.getRwaFlag());
    }

    public Function<KOM401_S001_ProposalParameters, KOM401_S001_ProposalParameters> fillProposalCreditLine(Proposal proposal) {
        return start -> start.then(fillBasicParameters(proposal))
                .then(fillProductTypeLoanPurpose(proposal))
                .selectCommitted(proposal.getCommitted())
                .then(fillDates(proposal));
    }

    public Function<KOM401_S001_ProposalParameters, KOM401_S001_ProposalParameters> fillProposalEuWc(Proposal proposal) {
        return start -> start.then(fillBasicParameters(proposal))
                .then(fillProposalLoanType(proposal))
                .then(fillProductTypeLoanPurpose(proposal))
                .selectCommitted(proposal.getCommitted())
                .selectMaturity(proposal.getMaturity())
                .then(fillDates(proposal));
    }

    public Function<KOM401_S001_ProposalParameters, KOM401_S001_ProposalParameters> fillProposalFlexiWc(Proposal proposal) {
        return start -> start.then(fillBasicParameters(proposal))
                .then(fillProposalLoanType(proposal))
                .then(fillProductTypeLoanPurpose(proposal))
                .selectCommitted(proposal.getCommitted())
                .selectMaturity(proposal.getMaturity())
                .then(fillDates(proposal));
    }

    public Function<KOM401_S001_ProposalParameters, KOM401_S001_ProposalParameters> fillProposalGeneralWarranty(Proposal proposal) {
        return start -> start.then(fillBasicParameters(proposal))
                .selectProposalType(proposal.getProposalType())
                .selectProductType(proposal.getProductType())
                .selectCommitted(proposal.getCommitted())
                .then(fillDates(proposal));
    }

    public Function<SME451_S001_UfoCbLandingPage, KOM401_S001_ProposalParameters> proposalSignpost(String proposalId) {
        return start -> start.selectProposal(proposalId)
                .clickProcessButton()
                .clickSignPost();
    }

    public Function<KOM401_S001_ProposalParameters, KOM401_S001_ProposalParameters> fillAll(Proposal proposal) {
        return start -> start.then(fillBasicParameters(proposal))
                .then(fillProposalLoanType(proposal))
                .then(fillProductTypeLoanPurpose(proposal))
                .then(fillDates(proposal))
                .selectSupport(proposal.getSupport())
                .selectInsurance(proposal.getInsurance());
    }

    public Function<KOM401_S001_ProposalParameters, KOM402_S001_ProposalSignpost> saveProposal(Proposal proposal) {
        return start -> start.clickSave()
                .clickContinueButton()
                .saveProposalId(proposal)
                .clickOkButton();
    }

    public Function<SME451_S001_UfoCbLandingPage, SME451_S001_UfoCbLandingPage> deleteProposal(String proposalId) {
        return start -> start.selectProposal(proposalId)
                .clickProcessButton()
                .clickSaveSignPost()
                .clickDeleteAndConfirm()
                .clickYes()
                .selectWho("Klient")
                .selectReason("Jiné")
                .clickDelete();
    }
}
