use tui::text::{Span, Spans};
use tui::style::{Style, Modifier};

pub enum Command {
    Help,
    // Picking based on id
    Channel(u64),
    // Choose server based on hostname
    Server(String),
    // Send regular message
    Message(String),
    // Command that failed with some message
    Failure(String),
}

impl Command {
    // Pulls out channel id from a line
    // Examples: /chan 123
    // /channel 789
    // /channelswag 1
    fn parse_chan_id(s: &str) -> Option<u64> {
        let parts: Vec<&str> = s.split(" ").collect();
        return if parts.len() < 2 {
            None
        } else {
            let id_s = parts.get(1).unwrap();
            match id_s.parse::<u64>() {
                Ok(id) => Some(id),
                _ => None
            }
        }
    }

    fn parse_hostname(s: &str) -> Option<String> {
        let parts: Vec<&str> = s.split(" ").collect();
        return if parts.len() < 2{
            None
        } else{
            let hostname: String = (*parts.get(1).unwrap()).into();
            Some(hostname)
        }
    }

    pub fn from(s: String) -> Command {
        let s = s.trim();
        if s.starts_with("/chan") {
            match Command::parse_chan_id(s.as_ref()) {
                Some(id) => Command::Channel(id),
                None => Command::Failure("no valid id(u64) provided".into())
            }
        } else if s.starts_with("/serv") {
            match Command::parse_hostname(s.as_ref()) {
                Some(hostname) => Command::Server(hostname),
                None => Command::Failure("no hostname provided".into())
            }
        } else if s.starts_with("/help") {
            Command::Help
        }
        else {
            if s.starts_with("/") {
                Command::Failure("command not found".into())
            } else {
                Command::Message(s.into())
            }
        }
    }

    pub fn styled(&self) -> Spans {
        use Command::*;
        return match self {
            Help => Spans::from(vec![
                Span::styled("! /help\n", Style::default().add_modifier(Modifier::BOLD)),
                Span::styled("! /channel <u64>", Style::default().add_modifier(Modifier::BOLD)),
                Span::styled("! /server <u64>", Style::default().add_modifier(Modifier::BOLD)),
            ]),
            Channel(id) => Spans::from(vec![
                Span::styled(format!("! /channel "), Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(format!("{}", id)),
            ]),
            Server(hostname) => Spans::from(vec![
                Span::styled("! /server ", Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(hostname)
            ]),
            Message(msg) => Spans::from(vec![
                Span::styled("(You) ", Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(msg)
            ]),
            Failure(msg) => Spans::from(vec![
                Span::styled("! error ", Style::default().add_modifier(Modifier::BOLD)),
                Span::raw(msg)
            ])
        }
    }
}
