#[derive(Debug, Clone, Default)]
pub struct TrendValue {
    alpha: f64,
    current_ema: Option<f64>,
    previous_ema: Option<f64>,
}

impl TrendValue {
    pub fn new(period: usize) -> Self {
        let alpha = 2.0 / period as f64;
        TrendValue {
            alpha,
            current_ema: None,
            previous_ema: None,
        }
    }

    pub fn update_value(&mut self, new_value: f64) {
        if self.current_ema.is_none() {
            self.current_ema = Some(new_value);
        } else {
            self.previous_ema = self.current_ema;
            self.current_ema =
                Some(new_value * self.alpha + self.previous_ema.unwrap() * (1.0 - self.alpha));
        }
    }

    pub fn current_ema(&self) -> f64 {
        self.current_ema.unwrap_or_default()
    }

    pub fn previous_ema(&self) -> f64 {
        self.previous_ema.unwrap_or_default()
    }
}
