use std::fmt::Display;

use chrono::{DateTime, Utc};
use libesedb::systemtime_from_filetime;

use crate::{impl_timestamp, value::{StringOrStringSet, ToStringOrStringSet}, CDatabase};

#[derive(Eq, PartialEq)]
pub struct WindowsFileTime(DateTime<Utc>);

impl_timestamp!(WindowsFileTime);

impl From<u64> for WindowsFileTime {
    fn from(value: u64) -> Self {
        Self(systemtime_from_filetime(value).into())
    }
}
impl From<DateTime<Utc>> for WindowsFileTime {
    fn from(value: DateTime<Utc>) -> Self {
        Self(value)
    }
}
impl From<WindowsFileTime> for DateTime<Utc> {
    fn from(value: WindowsFileTime) -> Self {
        value.0
    }
}

impl Display for WindowsFileTime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.to_rfc3339().fmt(f)
    }
}

impl ToStringOrStringSet for WindowsFileTime
{
    fn to_string_or_stringset(&self, _database: &CDatabase<'_, '_>) -> StringOrStringSet {
        StringOrStringSet::String(self.to_string())
    }
}