use crate::prelude::*;

pub(super) struct StoryGenerator {
    name: String,
}

impl report_generators::StoryReportGenerator for StoryGenerator {
    fn report_message(&self, message: &Message) {
        match message.severity {
            Severity::Error => log::error!(
                "{}:{}:{}:{}\n",
                self.name,
                message.filename,
                message.line,
                message.content
            ),
            Severity::Warning => log::warn!(
                "{}:{}:{}:{}\n",
                self.name,
                message.filename,
                message.line,
                message.content
            ),
            Severity::Info => log::info!(
                "{}:{}:{}:{}\n",
                self.name,
                message.filename,
                message.line,
                message.content
            ),
        }
    }
}

impl StoryGenerator {
    fn new(name: String) -> StoryGenerator {
        StoryGenerator { name }
    }
}

pub(super) struct Generator {}

impl report_generators::Generator for Generator {
    type StoryReportGenerator = StoryGenerator;
    fn report_failure(
        &self,
        story: &story::Story,
        duration: std::time::Duration,
        reason: String,
    ) -> Self::StoryReportGenerator {
        log::error!(
            "{}: failed in {}s: {}",
            story.name.to_owned(),
            duration.as_secs(),
            reason
        );
        StoryGenerator::new(story.name.to_owned())
    }
    fn report_success(
        &self,
        story: &story::Story,
        duration: std::time::Duration,
    ) -> Self::StoryReportGenerator {
        log::info!(
            "{}: succeeded in {}s",
            story.name.to_owned(),
            duration.as_secs()
        );
        StoryGenerator::new(story.name.to_owned())
    }
}

impl Generator {
    pub(super) fn new() -> Generator {
        Generator {}
    }
}
