//! Direction determination for the ZigZag state machine.

use crate::zigzag::types::BarInput;

/// Active direction of the current ZigZag leg.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Direction {
    Up,
    Down,
}

/// Internal state distinguishing initialized from uninitialized.
#[derive(Debug, Clone)]
pub(crate) enum Phase {
    Uninitialized(InitState),
    Active(Direction),
}

/// State tracked during the initialization phase (before first reversal).
#[derive(Debug, Clone)]
pub(crate) struct InitState {
    pub high: i64,
    pub high_index: usize,
    pub high_timestamp: i64,
    pub low: i64,
    pub low_index: usize,
    pub low_timestamp: i64,
}

impl InitState {
    pub fn from_bar(bar: &BarInput) -> Self {
        Self {
            high: bar.high,
            high_index: bar.index,
            high_timestamp: bar.timestamp_us,
            low: bar.low,
            low_index: bar.index,
            low_timestamp: bar.timestamp_us,
        }
    }

    pub fn update(&mut self, bar: &BarInput) -> bool {
        let mut changed = false;
        if bar.high > self.high {
            self.high = bar.high;
            self.high_index = bar.index;
            self.high_timestamp = bar.timestamp_us;
            changed = true;
        }
        if bar.low < self.low {
            self.low = bar.low;
            self.low_index = bar.index;
            self.low_timestamp = bar.timestamp_us;
            changed = true;
        }
        changed
    }
}
