# flake8: noqa T001, E501
# -*- coding: utf-8 -*-
"""
Advanced Filtering Example

Use FilterCriteria to efficiently filter data by cycles, steps, and loops.
"""

import BTAnalysisPyInterface as bta


def main():
    bta.print("Demonstrating advanced filtering...")

    for source in bta.get_sources():
        # Example 1: Filter specific cycles
        filter_cycles = bta.FilterCriteria()
        filter_cycles.cycles = [1, 10, 20, 50, 100]

        bta.print(f"Example 1: Filtering cycles {filter_cycles.cycles}")

        graph1 = bta.add_graph(
            title="Selected Cycles - Voltage vs Capacity",
            xAxis="Capacity",
            y1Axis="Voltage",
            x_unit="s",
            y1_unit="V",
        )

        # Get filtered data
        capacity = source.get_column("Potentiostat measure", "Capacity", filter_cycles)
        voltage = source.get_column("Potentiostat measure", "U", filter_cycles)

        graph1.add_series("Filtered Cycles", capacity, voltage)
        bta.print(f"  → Plotted {capacity.size} points")

        # Example 2: Filter specific step (discharge only)
        filter_discharge = bta.FilterCriteria()
        filter_discharge.cycles = [1, 50, 100]
        filter_discharge.steps = [1]  # Step 1 is typically discharge

        bta.print(f"\nExample 2: Discharge step of cycles {filter_discharge.cycles}")

        graph2 = bta.add_graph(
            title="Discharge Profiles - Selected Cycles",
            xAxis="Time",
            y1Axis="Voltage",
            x_unit="s",
            y1_unit="V",
        )

        for cycle in filter_discharge.cycles:
            filter_single = bta.FilterCriteria()
            filter_single.cycles = [cycle]
            filter_single.steps = [1]

            time = source.get_column("Potentiostat measure", "Time", filter_single)
            voltage = source.get_column("Potentiostat measure", "U", filter_single)

            if time.size > 0:
                graph2.add_series(f"Cycle {cycle}", time, voltage)
                bta.print(f"  Cycle {cycle}: {time.size} points")

        # Example 3: Combine cycles, steps, and loops
        filter_complex = bta.FilterCriteria()
        filter_complex.cycles = [1, 2, 3]
        filter_complex.steps = [1, 2]
        filter_complex.loops = [1]

        bta.print(f"\nExample 3: Complex filter")
        bta.print(f"  Cycles: {filter_complex.cycles}")
        bta.print(f"  Steps: {filter_complex.steps}")
        bta.print(f"  Loops: {filter_complex.loops}")
        bta.print(f"  Filter active: {filter_complex.has_filter()}")

        # Count filtered points
        count = 0
        for point in source.data_points(
            "Potentiostat measure", ["Time"], filter_complex
        ):
            count += 1

        bta.print(f"  → Matched {count} points")

        # Example 4: Filter summaries
        filter_summary = bta.FilterCriteria()
        filter_summary.cycles = [1, 10, 20, 30, 40, 50]

        bta.print(f"\nExample 4: Filtered summary")

        summary = source.get_summary(bta.SummaryType.PER_CYCLE, filter_summary)
        bta.print(
            f"  → Got {summary.row_count} cycles (out of {len(filter_summary.cycles)})"
        )

        # Create comparison table
        table = bta.add_table(
            columns=["Cycle", "Q discharge", "Efficiency"],
            title="Selected Cycle Performance",
        )

        cycles = summary.get_column("Cycle number")
        q_discharge = summary.get_column("Q discharge")
        efficiency = summary.get_column("Efficiency")

        for i in range(summary.row_count):
            table.add_row(
                {
                    "Cycle": cycles.get_value(i),
                    "Q discharge": q_discharge.get_value(i) / 3.6,
                    "Efficiency": efficiency.get_value(i) * 100,
                }
            )

        bta.print("\n✓ Advanced filtering complete")
