
use chrono;
use chrono::Timelike;
use crate::IResult;

// unstable
#[derive(serde::Deserialize, serde::Serialize, Clone, Copy)]
pub struct TimeMicros {
    pub year: u8,
    pub month: u8,
    pub day: u8,
    pub hour: u8,
    pub minute: u8,
    pub second: u8,
    pub millisecond: u16,
    pub microsecond: u16,
}

impl TimeMicros {

    /// 输入17位数字 2023_09_14_1755_59_999
    /// 年月日时分秒毫秒
    pub fn new(mut ticks: i64) -> Self {
        let mut ticks2 =  100000000000000000;
        let mut get_v = |s|{
            ticks = ticks % ticks2;
            let r = ticks / s;
            r
        };
        let year = get_v(10000);
        let month = get_v(100);
        let day = get_v(100);
        let hour = get_v(100);
        let minute = get_v(100);
        let second = get_v(100);
        let millisecond = get_v(1000);
        Self {
            year: year as u8,
            month: month as u8,
            day: day as u8,
            hour: hour as u8,
            minute: minute as u8,
            second: second as u8,
            millisecond: millisecond as u16,
            microsecond: 0,
        }
    }
    /* pub fn now() -> Self {
        let now = chrono::prelude::Local::now();
        let millisec = now.nanosecond() as i32 / 1000_000;
        let ticks = now.hour() as i32 * TimeI32Const::TicksPerHour
            + now.minute() as i32 * TimeI32Const::TicksPerMinute
            + now.second() as i32 * TimeI32Const::TicksPerSecond
            + millisec;

        Self { ticks }
    }

    pub fn fromHmsS(hour: i32, minute: i32, second: i32, millisec: i32) -> IResult<Self> {
        if hour > 23 {
            return Err(format!("TimeInt::Ctor: Hour {} is not in [0 ~ 23]", hour))?;
        }
        if minute > 59 {
            return Err(format!(
                "TimeInt::Ctor: Minute {} is not in [0 ~ 59]",
                minute
            ))?;
        }
        if second > 59 {
            return Err(format!(
                "TimeInt::Ctor: Second {} is not in [0 ~ 59]",
                second
            ))?;
        }
        if millisec > 999 {
            return Err(format!(
                "TimeInt::Ctor: Millisec {} is not in [0 ~ 999]",
                millisec
            ))?;
        }
        let ticks = hour * TimeI32Const::TicksPerHour
            + minute * TimeI32Const::TicksPerMinute
            + second * TimeI32Const::TicksPerSecond
            + millisec;
        Ok(TimeInt { ticks })
    } */
    /* pub fn fromHms(hour: i32, minute: i32, second: i32) -> IResult<Self> {
        if hour > 23 {
            return Err(format!("TimeInt::Ctor: Hour {} is not in [0 ~ 23]", hour))?;
        }
        if minute > 59 {
            return Err(format!(
                "TimeInt::Ctor: Minute {} is not in [0 ~ 59]",
                minute
            ))?;
        }
        if second > 59 {
            return Err(format!(
                "TimeInt::Ctor: Second {} is not in [0 ~ 59]",
                second
            ))?;
        }
        let ticks = hour * TimeI32Const::TicksPerHour
            + minute * TimeI32Const::TicksPerMinute
            + second * TimeI32Const::TicksPerSecond;
        Ok(TimeInt { ticks })
    } */
    /* pub fn fromHm(hour: i32, minute: i32) -> IResult<Self> {
        if hour > 23 {
            return Err(format!("TimeInt::Ctor: Hour {} is not in [0 ~ 23]", hour))?;
        }
        if minute > 59 {
            return Err(format!(
                "TimeInt::Ctor: Minute {} is not in [0 ~ 59]",
                minute
            ))?;
        }
        let ticks = hour * TimeI32Const::TicksPerHour + minute * TimeI32Const::TicksPerMinute;
        Ok(TimeInt { ticks })
    } */

    // 9_01_02_030 -> 9:01:02:
    /* pub fn fromInt9(time9: i32) -> IResult<Self> {
        let hour = time9 / 1_00_00_000;
        let mm_ss_SSS = time9 % 1_00_00_000;
        let minute = mm_ss_SSS / 100_000;
        let ss_SSS = mm_ss_SSS % 100_000;
        return TimeInt::fromHmsS(hour, minute, ss_SSS / 1000, ss_SSS % 1000);

        // let mut ti = 10_00_00_00;
        // let mut com_time = |t| {
        //     let r = time9 / ti;
        //     time9 %= ti;
        //     ti /= t;
        //     // println!("r: {r}: time9: {time9}, ti: {ti}");
        //     r
        // };

        // let hour = com_time(100);

        // let minute = com_time(100);
        // let sec = com_time(100);
        // let millisec = time9;

        // return TimeI32::fromHmsS(hour, minute, sec, millisec);
    } */
    /* pub fn from_string9_safe(time9: &str) -> Self {
        if time9.len() != 9 {
            error!("Error time_int string: {}", time9);
            return TimeInt::min();
        }

        let i_time9 = match time9.parse::<i32>() {
            Ok(v) => v,
            Err(_) => {
                error!("Error time_int string: {}", time9);
                return TimeInt::min();
            },
        };

        Self::fromInt9(i_time9).unwrap_or_else(|_| Default::default())


    } */

    /* pub fn fromInt9_safe(time9: i32) -> Self {
        Self::fromInt9(time9).unwrap_or_else(|_| Default::default())
    }
    pub fn fromLong17_safe(date_time: i64) -> Self {
        let tm9 = date_time % 10_0000_0000;
        let me = Self::fromInt9(tm9 as i32).unwrap_or_else(|_| Default::default());

        me
    } */
}
/* 
impl std::default::Default for TimeInt {
    fn default() -> Self {
        TimeInt::min()
    }
}

impl std::fmt::Display for TimeInt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{:0>2}:{:0>2}:{:0>2}-{:0>3}",
            self.hour(),
            self.minute(),
            self.second(),
            self.millisecond()
        )
    }
}
impl std::fmt::Debug for TimeInt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TimeInt").field("ticks", &self.ticks).field("friendly", &self.to_string9b()).finish()
    }
}



pub mod TimeI32Const {
    /// 1000
    pub const TicksPerSecond: i32 = 1000;
    /// 60_000
    pub const TicksPerMinute: i32 = TicksPerSecond * 60;
    /// 3600_000
    pub const TicksPerHour: i32 = TicksPerMinute * 60;
    /// 86400_000
    pub const TicksPerDay: i32 = TicksPerHour * 24;
    /// 0, 0:0:0-000，
    /// MinTimeTicks is the ticks for the midnight time 00:00:00.000 AM
    pub const MinTicks: i32 = 0;
    /// 86_399_999, 23:59:59-999，
    /// MaxTimeTicks is the max tick value for the time in the day. It is calculated using DateTime.Today.AddTicks(-1).TimeOfDay._ticks.
    pub const MaxTicks: i32 = 86_399_999;
}

// constant ===============================================
impl TimeInt {
    pub fn min() -> Self {
        TimeInt {
            ticks: TimeI32Const::MinTicks,
        }
    }
    pub fn max() -> Self {
        TimeInt {
            ticks: TimeI32Const::MaxTicks,
        }
    }
}

impl TimeInt {
    pub fn ticks(&self) -> i32 {
        self.ticks
    }
    pub fn hour(&self) -> i32 {
        self.ticks / TimeI32Const::TicksPerHour
    }
    pub fn minute(&self) -> i32 {
        (self.ticks / TimeI32Const::TicksPerMinute) % 60
    }
    pub fn second(&self) -> i32 {
        (self.ticks / TimeI32Const::TicksPerSecond) % 60
    }
    pub fn millisecond(&self) -> i32 {
        self.ticks % TimeI32Const::TicksPerSecond
    }
    /// 测试环境的数据回放，会有差值是负的情况，所以返回i32, 而非I32
    pub fn milliseconds_from(&self, tm_before: &Self) -> i32 {
        self.ticks as i32 - tm_before.ticks as i32
    }
    
    pub fn seconds_from(&self, tm_before: &Self) -> f64 {
        (self.ticks - tm_before.ticks) as f64 / 1000_f64
    }
    // pub fn minutes_from(&self, tm_before: &Self) -> f64 {
    //     (self.ticks - tm_before.ticks) as f64 / 60_000_f64
    // }

    pub fn to_int4(&self) -> i32 {
        let hour = self.ticks / TimeI32Const::TicksPerHour;
        let minute = (self.ticks / TimeI32Const::TicksPerMinute) % 60;
        hour * 100 + minute
    }

    pub fn to_int6(&self) -> i32 {
        let hour = self.ticks / TimeI32Const::TicksPerHour;
        let minute = (self.ticks / TimeI32Const::TicksPerMinute) % 60;
        let second = (self.ticks / TimeI32Const::TicksPerSecond) % 60;

        hour * 100_00 + minute * 100 + second
    }
    pub fn to_int9(&self) -> i32 {
        let hour = self.ticks / TimeI32Const::TicksPerHour;
        let minute = (self.ticks / TimeI32Const::TicksPerMinute) % 60;
        let second = (self.ticks / TimeI32Const::TicksPerSecond) % 60;
        let millisec = self.ticks % TimeI32Const::TicksPerSecond;
        hour * 100_00_000 + minute * 100_000 + second * 1000 + millisec
    }

    /* 
    pub fn toString4(&self) -> String {
        let hour = self.ticks / TimeI32Const::TicksPerHour;
        let minute = (self.ticks / TimeI32Const::TicksPerMinute) % 60;
        format!("{:02}{:02}", hour, minute)
    }
     */

    
    pub fn to_string6(&self) -> String {
        let hour = self.ticks / TimeI32Const::TicksPerHour;
        let minute = (self.ticks / TimeI32Const::TicksPerMinute) % 60;
        let second = (self.ticks / TimeI32Const::TicksPerSecond) % 60;

        format!("{:02}{:02}{:02}", hour, minute, second)
    }
    pub fn to_string6b(&self) -> String {
        let hour = self.ticks / TimeI32Const::TicksPerHour;
        let minute = (self.ticks / TimeI32Const::TicksPerMinute) % 60;
        let second = (self.ticks / TimeI32Const::TicksPerSecond) % 60;

        format!("{:02}:{:02}:{:02}", hour, minute, second)
    }
    pub fn to_string9(&self) -> String {
        let hour = self.ticks / TimeI32Const::TicksPerHour;
        let minute = (self.ticks / TimeI32Const::TicksPerMinute) % 60;
        let second = (self.ticks / TimeI32Const::TicksPerSecond) % 60;
        let millisec = self.ticks % TimeI32Const::TicksPerSecond;

        format!("{:02}{:02}{:02}{:03}", hour, minute, second, millisec)
    }
    
    pub fn to_string9b(&self) -> String {
        let hour = self.ticks / TimeI32Const::TicksPerHour;
        let minute = (self.ticks / TimeI32Const::TicksPerMinute) % 60;
        let second = (self.ticks / TimeI32Const::TicksPerSecond) % 60;
        let millisec = self.ticks % TimeI32Const::TicksPerSecond;

        format!("{:02}:{:02}:{:02}-{:03}", hour, minute, second, millisec)
    }
} */
/* 
#[test]
fn test_to_int9() {
    let now = TimeInt::now();
    let now = now.to_int9();
    println!("now: {now}");

    // let now2 = crate::utils::time::get_now_time_9();
    // println!("now2: {now2}");
}


#[test]
fn test_from_int9() {
    let r = TimeInt::fromInt9(1_23_45_678);
    println!("{:?}", r);

    let r = TimeInt::fromInt9(11_23_45_678);
    println!("{:?}", r);

    let r = TimeInt::fromInt9(1_02_03_004);
    println!("{:?}", r);

    let r = TimeInt::fromInt9(11_02_03_004);
    println!("{:?}", r);

    let r = TimeInt::fromInt9(12_02_03_004);
    println!("{:?}", r);

    let r = TimeInt::fromInt9(00_01_02_003);
    println!("{:?}", r);

    let r = TimeInt::fromInt9(00_11_22_333);
    println!("{:?}", r);
} */

#[test]
fn test_new(){
    let t = TimeMicros::new(2023_09_14_1755_59_999);
    println!("{:#?}", serde_json::json!(t))
}