/**
 * welcum to the cache zone
 * Notes about data model here
 * Basically none of the ever gets written to disk so its mutability is
 * 
 * Memory Model things
 *  The cache should always own its own data and nothing else
 *  On calls where the cache system needs to take data it should actually 
 *  make its own copy to not disturb the render side of things as it too requires
 *  ownership of its own data. For this reason all parts of this are basically 
 *  going to be really selfish about its own data
 *
 */

use crate::config::{ ServerMeta, UserConfig };
use crate::api_types::{Channel, Message};
use crate::command::Command;

use std::collections::HashMap;

struct ChannelCache {
    meta: Channel,
    messages: Vec<Message>
}

struct ServerCache {
    meta: ServerMeta,
    user: UserConfig,
    channels: Vec<ChannelCache>
}

pub struct Cache {
    // Hostname -> Cache
    servers: HashMap<String, ServerCache>,
    active_server: Option<String>
}

impl Default for Cache {
    fn default() -> Cache {
        Cache {
            servers: HashMap::new(),
            active_server: None
        }
    }
}

impl Cache {
    pub async fn switch_channel(&mut self, id: u64) -> Command {
        if let Some(host) = self.active_server.as_ref() {
            Command::Failure(format!("Not implemented yet!"))
        } else {
            Command::Failure(format!("No server selected yet"))
        }
    }
    pub async fn switch_server(&mut self, host: &str) -> Command {
        if let Some(srv_cache) = self.servers.get(host) {
            Command::Failure(format!("Not implemented yet!"))
        } else {
            Command::Failure(format!("Host {{ {} }} not found", host))
        }
    }

    pub async fn send_message(&mut self, id: &str) -> Command {
        if let Some(host) = self.active_server.as_ref() {
            Command::Failure(format!("Not implemented yet!"))
        } else {
            Command::Failure(format!("No active server+channel to send message to"))
        }
    }
}
