use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use systemd_journal_logger::connected_to_journal;

macro_rules! info {
    ($msg:tt) => {
        match connected_to_journal() {
            true => log::info!("[INFO] {}", $msg),
            false => println!("[INFO] {}", $msg),
        }
    };
}

macro_rules! error {
    ($msg:tt) => {
        match connected_to_journal() {
            true => log::error!("[ERROR] {}", $msg),
            false => eprintln!("[ERROR] {}", $msg),
        }
    };
}

#[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub(crate) struct SeriesHistory {
    pub(crate) series: HashMap<String, PostHistory>,
}

impl SeriesHistory {
    pub(crate) fn load_history() -> Self {
        let info_msg = "Loading History";
        info!(info_msg);
        match confy::load(env!("CARGO_PKG_NAME"), "history") {
            Ok(data) => data,
            Err(e) => panic!("history.toml not found: {e}"),
        }
    }

    pub(crate) fn save_history(&self) {
        let info_msg = "Saving History";
        info!(info_msg);
        if let Err(e) = confy::store(env!("CARGO_PKG_NAME"), "history", self) {
            let err_msg = format!("Unexpected error saving to history.toml: {e}");
            error!(err_msg);
        }
    }

    pub(crate) fn check_for_post(&self, series: &str, part: &str, title: &str) -> bool {
        if let Some(series_map) = self.series.get(series) {
            if let Some(part_info) = series_map.parts.get(part) {
                return part_info.volume == title || part_info.chapter == title;
            }
        }
        false
    }

    pub(crate) fn get_series(&self, series: &str) -> PostHistory {
        match self.series.get(series) {
            Some(history) => history.clone(),
            None => PostHistory {
                parts: HashMap::new(),
            },
        }
    }

    pub(crate) fn set_series(&mut self, series: &str, data: PostHistory) {
        self.series
            .entry(series.to_owned())
            .and_modify(|val| *val = data.clone())
            .or_insert(data);
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct PostHistory {
    pub(crate) parts: HashMap<String, PostHistoryInner>,
}

impl PostHistory {
    pub(crate) fn get_part(&self, part: &str) -> PostHistoryInner {
        match self.parts.get(part) {
            Some(history) => history.clone(),
            None => PostHistoryInner {
                volume: "".to_owned(),
                chapter: "".to_owned(),
            },
        }
    }

    pub(crate) fn set_part(&mut self, part: &str, data: PostHistoryInner) {
        self.parts
            .entry(part.to_owned())
            .and_modify(|val| *val = data.clone())
            .or_insert(data);
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct PostHistoryInner {
    pub(crate) volume: String,
    pub(crate) chapter: String,
}