/*
 * SPDX-License-Identifier: MIT
 */
package org.ta4j.core.rules.elliott;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.ta4j.core.BarSeries;
import org.ta4j.core.Indicator;
import org.ta4j.core.indicators.elliott.ElliottPhase;
import org.ta4j.core.indicators.elliott.ElliottScenario;
import org.ta4j.core.indicators.elliott.ElliottScenarioSet;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.num.DecimalNumFactory;
import org.ta4j.core.num.NaN;
import org.ta4j.core.num.Num;
import org.ta4j.core.num.NumFactory;

class ElliottScenarioValidRuleTest {

    private BarSeries series;
    private NumFactory numFactory;

    @BeforeEach
    void setUp() {
        numFactory = DecimalNumFactory.getInstance();
        series = ElliottScenarioRuleTestSupport.buildSeries(numFactory);
    }

    @Test
    void validRuleRequiresScenarioAndPrice() {
        ElliottScenario scenario = ElliottScenarioRuleTestSupport.buildBullishScenario(numFactory, ElliottPhase.WAVE3,
                0.8);
        ElliottScenarioSet set = ElliottScenarioSet.of(List.of(scenario), series.getEndIndex());
        Indicator<ElliottScenarioSet> indicator = ElliottScenarioRuleTestSupport.fixedScenarioIndicator(series, set);
        Indicator<Num> close = new ClosePriceIndicator(series);

        ElliottScenarioValidRule rule = new ElliottScenarioValidRule(indicator, close);
        assertThat(rule.isSatisfied(series.getEndIndex(), null)).isTrue();

        ElliottScenarioSet emptySet = ElliottScenarioSet.empty(series.getEndIndex());
        ElliottScenarioValidRule missingRule = new ElliottScenarioValidRule(
                ElliottScenarioRuleTestSupport.fixedScenarioIndicator(series, emptySet), close);
        assertThat(missingRule.isSatisfied(series.getEndIndex(), null)).isFalse();

        Indicator<Num> nanPrice = ElliottScenarioRuleTestSupport.constantIndicator(series, NaN.NaN);
        ElliottScenarioValidRule nanRule = new ElliottScenarioValidRule(indicator, nanPrice);
        assertThat(nanRule.isSatisfied(series.getEndIndex(), null)).isFalse();
    }
}
