use std::cell::RefCell;
use std::time::Duration;
use std::{fmt::Display, string};
use std::error::Error;

use crate::GameState;
use crate::window::ui::GameWindowUiState;

pub type Second = u64;

pub struct TimeMan
{
    time: Second,
    auto_tick: Option<Second>
}

impl TimeMan
{
    pub fn new(time: Second)
    -> Self {
        Self {
            time,
            auto_tick: None
        }
    }

    pub fn seconds(&self)
    -> Second {
        self.time
    }

    pub fn advance(
        &mut self,
        by: Second)
    {
        self.time += by;
    }

    pub fn update(
        &mut self)
    {
        match self.auto_tick {
            Some(advance) => { self.time += advance; },
            None => {}
        }
    }

    pub fn format_duration(time: Second) -> String
    {
        let seconds = time % 60;
        let minutes = (time / 60) % 60;
        let hours = (time / (60 * 60)) % 24;
        let days = (time / (60 * 60 * 24)) % 365;
        let years = time / (60 * 60 * 24 * 365);
        
        if time == 0 {
            return "0s".to_string();
        }

        format!("{}{}{}{}{}",
            if seconds > 0 { format!("{}s", seconds) } else { format!("") },
            if minutes > 0 { format!("{}m", minutes) } else { format!("") },
            if hours > 0 { format!("{}h", hours) } else { format!("") },
            if days > 0 { format!("{}d", days) } else { format!("") },
            if years > 0 { format!("{}y", years) } else { format!("") })
    }


} //impl TimeMan
