use crate::trend_value::TrendValue;

#[derive(Debug, Clone, Default)]
pub struct EdgeValue {
    pub upper: f64,
    pub lower: f64,
    pub center: f64,
    pub width: f64,
    pub width_trend: TrendValue,
}

const CONSOLIDATION_FACTOR: f64 = 8.0;

impl EdgeValue {
    pub fn new(upper: f64, lower: f64, center: f64, period: usize) -> Self {
        let width = upper - lower;
        let mut width_trend = TrendValue::new(period, period * 2);
        width_trend.update_value(width);

        Self {
            upper,
            lower,
            center,
            width,
            width_trend,
        }
    }

    pub fn is_width_expanding(&self) -> bool {
        self.width > self.width_trend.current_ema()
    }

    pub fn is_tight_consolidation(&self, price: f64) -> bool {
        price > self.center - (self.width / CONSOLIDATION_FACTOR)
            && price < self.center + (self.width / CONSOLIDATION_FACTOR)
    }

    pub fn is_approaching_upper(&self, price: f64) -> bool {
        (self.upper - self.width / CONSOLIDATION_FACTOR < price)
            && (price < self.upper + self.width / CONSOLIDATION_FACTOR)
    }

    pub fn is_approaching_lower(&self, price: f64) -> bool {
        (self.lower - self.width / CONSOLIDATION_FACTOR < price)
            && (price < self.lower + self.width / CONSOLIDATION_FACTOR)
    }
}
