use std::collections::HashMap;
use serde_derive::{Deserialize, Serialize};
use crate::write_error;

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

impl SeriesHistory {
    pub(crate) fn load_history() -> Self {
        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) {
        if let Err(e) = confy::store(env!("CARGO_PKG_NAME"), "history", self) {
            let err_msg = format!("Unexpected error saving to history.toml: {e}");
            write_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,
}