use std::rc::Rc;

use super::{Environment, Node, NodeEnum, Precedence, if_else::Bool};

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct Equals {
    left: Rc<NodeEnum>,
    right: Rc<NodeEnum>,
}

impl Node for Equals {
    fn evaluate(&self, env: &mut Environment) -> Result<Rc<NodeEnum>, String> {
        let left = self.left.evaluate(env)?;
        let right = self.right.evaluate(env)?;

        match left == right {
            true => Ok(Rc::new(Bool::True.into())),
            false => Ok(Rc::new(Bool::False.into())),
        }
    }

    fn as_string(&self, env: Option<&Environment>) -> String {
        format!(
            "({}={})",
            self.left.as_string(env),
            self.right.as_string(env)
        )
    }

    fn precedence(&self) -> Precedence {
        Precedence::Primary
    }
}

impl Equals {
    pub fn new(left: Rc<NodeEnum>, right: Rc<NodeEnum>) -> Self {
        Self { left, right }
    }
}
