#pragma once
#include "IOValues/timer_io_values.hpp"

namespace JabyEngine {
    namespace Timer_IO {
        using CounterMode_IO   = IOPort<Timer_IO_Values::CounterMode>;
        using CounterTarget_IO = IOPort<Timer_IO_Values::CounterTarget>;
        using CounterValue_IO  = IOPort<Timer_IO_Values::CounterValue>;

        #pragma pack(push, 1)
        struct Counter {
            CounterValue_IO  value;
            CounterMode_IO   mode;
            CounterTarget_IO target;
            uint32_t         unused;

            inline uint16_t get_current_value() const {
                return this->value.read().get(Timer_IO_Values::CounterValue::Value);
            }

            inline void set_target_value(uint16_t value) {
                this->target.write(Timer_IO_Values::CounterTarget{0}.set_range(Timer_IO_Values::CounterTarget::CounterTargetValue, value));
            }

            inline void set_mode(Timer_IO_Values::CounterMode mode) {
                this->mode.write(mode);
            }
        };
        #pragma pack(pop)

        #pragma pack(push, 1)
        struct Counter0 : public Counter {
            struct SyncMode {
                static constexpr auto Zero_At_Hblank                      = Timer_IO_Values::CounterMode::SyncMode.with(1u);
                static constexpr auto Pause_During_Hblank                 = Timer_IO_Values::CounterMode::SyncMode.with(0u);
                static constexpr auto Zero_At_Hblank_Pause_Outside_Hblank = Timer_IO_Values::CounterMode::SyncMode.with(2u);
                static constexpr auto Pause_Until_Hblank_Then_Freerun     = Timer_IO_Values::CounterMode::SyncMode.with(3u);
            };

            struct Source {
                static constexpr auto System_Clock     = Timer_IO_Values::CounterMode::ClockSource.with(0u);
                static constexpr auto Dot_Clock        = Timer_IO_Values::CounterMode::ClockSource.with(1u);
                static constexpr auto System_Clock_Too = Timer_IO_Values::CounterMode::ClockSource.with(2u);
                static constexpr auto Dot_Clock_Too    = Timer_IO_Values::CounterMode::ClockSource.with(3u);
            };
        };

        struct Counter1 : public Counter {
            struct SyncMode {
                static constexpr auto Pause_During_Vblank                 = Timer_IO_Values::CounterMode::SyncMode.with(0u);
                static constexpr auto Zero_At_Vblank                      = Timer_IO_Values::CounterMode::SyncMode.with(1u);
                static constexpr auto Zero_At_Vblank_Pause_Outside_Vblank = Timer_IO_Values::CounterMode::SyncMode.with(2u);
                static constexpr auto Pause_Until_Vblank_Then_FreeRun     = Timer_IO_Values::CounterMode::SyncMode.with(3u);
            };

            struct Source {
                static constexpr auto System_Clock     = Timer_IO_Values::CounterMode::ClockSource.with(0u);
                static constexpr auto Hblank           = Timer_IO_Values::CounterMode::ClockSource.with(1u);
                static constexpr auto System_Clock_Too = Timer_IO_Values::CounterMode::ClockSource.with(2u);
                static constexpr auto Hblank_Too       = Timer_IO_Values::CounterMode::ClockSource.with(3u);   
            };
        };

        struct Counter2 : public Counter {
            struct SyncMode {
                static constexpr auto Stop_Counter     = Timer_IO_Values::CounterMode::SyncMode.with(0u);
                static constexpr auto FreeRun          = Timer_IO_Values::CounterMode::SyncMode.with(1u);
                static constexpr auto FreeRun_Too      = Timer_IO_Values::CounterMode::SyncMode.with(2u);
                static constexpr auto Stop_Counter_Too = Timer_IO_Values::CounterMode::SyncMode.with(3u);
            };

            struct Source {
                static constexpr auto System_Clock           = Timer_IO_Values::CounterMode::ClockSource.with(0u);
                static constexpr auto System_Clock_Too       = Timer_IO_Values::CounterMode::ClockSource.with(1u);
                static constexpr auto System_Clock_Div_8     = Timer_IO_Values::CounterMode::ClockSource.with(2u);
                static constexpr auto System_Clock_Div_8_Too = Timer_IO_Values::CounterMode::ClockSource.with(3u);
            };
        };
        #pragma pack(pop)

        static constexpr uintptr_t counter_base_adr(size_t ID) {
            return (0x1F801100 + (ID*0x10));
        }

        static auto& Counter0 = __declare_io_value(struct Counter0, counter_base_adr(0));
        static auto& Counter1 = __declare_io_value(struct Counter1, counter_base_adr(1));
        static auto& Counter2 = __declare_io_value(struct Counter2, counter_base_adr(2));
    }
}