use super::SqlNode;
use crate::physical_plan::SqlEvaluatorVisitor;
use crate::planner::query_tools::QueryTools;
use crate::planner::sql_templates::PlanSqlTemplates;
use crate::planner::symbols::DimensionKind;
use crate::planner::MemberSymbol;
use cubenativeutils::CubeError;
use std::any::Any;
use std::rc::Rc;

/// Renders a `Geo` dimension by combining its latitude and
/// longitude SQL expressions into a paired projection. Pass-through
/// for non-geo dimensions.
pub struct GeoDimensionSqlNode {
    input: Rc<dyn SqlNode>,
}

impl GeoDimensionSqlNode {
    pub fn new(input: Rc<dyn SqlNode>) -> Rc<Self> {
        Rc::new(Self { input })
    }

    pub fn input(&self) -> &Rc<dyn SqlNode> {
        &self.input
    }
}

impl SqlNode for GeoDimensionSqlNode {
    fn to_sql(
        &self,
        visitor: &SqlEvaluatorVisitor,
        node: &Rc<MemberSymbol>,
        query_tools: Rc<QueryTools>,
        node_processor: Rc<dyn SqlNode>,
        templates: &PlanSqlTemplates,
    ) -> Result<String, CubeError> {
        let res = match node.as_ref() {
            MemberSymbol::Dimension(ev) => {
                if let DimensionKind::Geo(geo) = ev.kind() {
                    let inner_visitor = visitor.with_arg_needs_paren_safe(false);
                    let latitude_str = geo.latitude().eval(
                        &inner_visitor,
                        node_processor.clone(),
                        query_tools.clone(),
                        templates,
                    )?;
                    let longitude_str = geo.longitude().eval(
                        &inner_visitor,
                        node_processor.clone(),
                        query_tools.clone(),
                        templates,
                    )?;
                    templates.concat_strings(&vec![latitude_str, format!("','"), longitude_str])?
                } else {
                    self.input.to_sql(
                        visitor,
                        node,
                        query_tools.clone(),
                        node_processor.clone(),
                        templates,
                    )?
                }
            }
            _ => {
                return Err(CubeError::internal(format!(
                    "GeoDimension node processor called for wrong node",
                )));
            }
        };
        Ok(res)
    }

    fn as_any(self: Rc<Self>) -> Rc<dyn Any> {
        self.clone()
    }

    fn childs(&self) -> Vec<Rc<dyn SqlNode>> {
        vec![self.input.clone()]
    }
}
