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

pub trait ValueChange {
    fn update_value(&mut self, new_val: f64, beta: Option<f64>);
}

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

impl ValueChange for TrendValue {
    fn update_value(&mut self, new_value: f64, beta: Option<f64>) {
        if self.current_ema.is_none() {
            self.current_ema = Some(new_value);
        } else {
            self.previous_ema = self.current_ema;
            let gamma = self.alpha * beta.unwrap_or(1.0);
            self.current_ema = Some(new_value * gamma + self.previous_ema.unwrap() * (1.0 - gamma));
        }
    }
}
