//! A crate defining different output formats for search results.
use std::fmt::Write;

use super::{LogResult, FightOutcome};
use super::super::guilds;

/// An output format
pub trait Format: Sync + Send {
    /// Format a single log result
    fn format_result(&self, item: &LogResult) -> String;
}


/// The human readable, colored format.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct HumanReadable {
    pub show_guilds: bool,
}


impl Format for HumanReadable {
    fn format_result(&self, item: &LogResult) -> String {
        use colored::Colorize;
        let mut result = String::new();

        writeln!(result, "{}: {:?}", "File".green(), item.log_file).unwrap();
        let outcome = match item.outcome {
            FightOutcome::Success => "SUCCESS".green(),
            FightOutcome::Wipe => "WIPE".red(),
        };
        writeln!(
            result,
            "{}: {} - {}: {} {}",
            "Date".green(),
            item.time.format("%Y-%m-%d %H:%M:%S %a"),
            "Boss".green(),
            item.boss_name,
            outcome,
        ).unwrap();
        for player in &item.players {
            write!(
                result,
                "  {:2} {:20} {:19} {:12}",
                player.subgroup,
                player.account_name.yellow(),
                player.character_name.cyan(),
                player.profession,
            ).unwrap();
            if self.show_guilds {
                let guild = player.guild_id.as_ref().and_then(|id| guilds::lookup(id));
                if let Some(guild) = guild {
                    write!(
                        result,
                        " [{}] {}",
                        guild.tag().magenta(),
                        guild.name().magenta(),
                    ).unwrap();
                }
            }
            writeln!(result).unwrap();
        }
        result
    }
}


/// A format which outputs only the file-name
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct FileOnly;


impl Format for FileOnly {
    fn format_result(&self, item: &LogResult) -> String {
        let filename = item.log_file.to_string_lossy();
        format!("{}\n", filename)
    }
}