from ctypes import Structure, c_uint32, c_bool


class ParsedTimingStruct(Structure):
    _fields_ = [
        ("pixel_clock_khz",c_uint32),
        ("h_active", c_uint32),
        ("v_active", c_uint32),
        ("h_total", c_uint32),
        ("h_start", c_uint32),
        ("h_sync", c_uint32),
        ("v_total", c_uint32),
        ("v_start", c_uint32),
        ("v_sync", c_uint32),
        ("is_hsync_neg", c_bool),
        ("is_vsync_neg", c_bool),
        ("interlaced", c_bool)
    ]

    @property
    def total_pixels(self) -> int:
        return self.v_total * self.h_total

    @property
    def frame_rate_hz(self):
        return self.pixel_clock_khz * 1000 / self.total_pixels

    @property
    def frame_rate_millihz(self):
        return self.pixel_clock_khz * 1000000 / self.total_pixels

    @property
    def interlaced_frame_rate_hz(self):
        return (2 * self.frame_rate_hz) if self.interlaced else self.frame_rate_hz

    @property
    def interlaced_frame_rate_millihz(self):
        return (2 * self.frame_rate_millihz) if self.interlaced else self.frame_rate_millihz

    @property
    def is_rb(self):
        return not self.is_hsync_neg and self.is_vsync_neg and self.h_sync == 32

    @property
    def is_ovt(self):
        return not self.is_hsync_neg and not self.is_vsync_neg and self.h_sync == 32 and self.v_sync == 8

    @property
    def line_duration_ms(self):
        return self.h_total / self.pixel_clock_khz

    @property
    def pixel_clock_hz(self):
        return self.pixel_clock_khz * 1000
