use super::split_args;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TaskStatus {
    Open,
    Done,
}

#[derive(Debug, Clone)]
pub struct TaskData {
    pub tags: Vec<String>,
    pub status: TaskStatus,
}

impl TaskData {
    pub fn parse_args(raw: &str) -> Self {
        let p = split_args(raw);
        let status = match p.attrs.get("status").map(|s| s.as_str()) {
            Some("done") => TaskStatus::Done,
            _ => TaskStatus::Open,
        };
        TaskData {
            tags: p.tags,
            status,
        }
    }

    pub fn is_done(&self) -> bool {
        self.status == TaskStatus::Done
    }
    pub fn is_open(&self) -> bool {
        !self.is_done()
    }

    pub fn status_str(&self) -> &'static str {
        match self.status {
            TaskStatus::Done => "done",
            TaskStatus::Open => "open",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_open() {
        let d = TaskData::parse_args("work");
        assert!(d.is_open());
        assert_eq!(d.tags, vec!["work"]);
    }

    #[test]
    fn test_done_status() {
        let d = TaskData::parse_args("work, status: done");
        assert!(d.is_done());
    }

    #[test]
    fn test_empty_args() {
        let d = TaskData::parse_args("");
        assert!(d.is_open());
        assert!(d.tags.is_empty());
    }
}
