use crate::walk::attribute_column::AttributeColumn;
use cursive_table_view::TableViewItem;

#[derive(Clone)]
pub(crate) struct AttributeLine {
    name: String,
    index: usize,
    value: String,
    has_siblings: bool,
}

impl AttributeLine {
    pub fn new(name: String, index: usize, value: String, has_siblings: bool) -> Self {
        Self {
            name,
            index,
            value,
            has_siblings,
        }
    }
}

impl TableViewItem<AttributeColumn> for AttributeLine {
    fn to_column(&self, column: AttributeColumn) -> String {
        match column {
            AttributeColumn::Name => {
                if self.has_siblings {
                    format!("{}[{}]", self.name, self.index)
                } else {
                    self.name.clone()
                }
            }
            AttributeColumn::Value => self.value.clone(),
        }
    }

    fn cmp(&self, other: &Self, column: AttributeColumn) -> std::cmp::Ordering
    where
        Self: Sized,
    {
        match column {
            AttributeColumn::Name => {
                let name_comparison = self.name.cmp(&other.name);
                if name_comparison.is_eq() {
                    self.index.cmp(&other.index)
                } else {
                    name_comparison
                }
            }
            AttributeColumn::Value => self.value.cmp(&other.value),
        }
    }
}
