#include "../../internal-include/System/callbacks_internal.hpp"
#include "../../internal-include/CD/cd_internal.hpp"
#include <PSX/System/callbacks.hpp>

namespace JabyEngine {
    namespace Callback {
        // Marked deprecated currently
        /*using Function = VSyncCallback::Function;
        Function VSyncCallback :: callback = nullptr;*/

        namespace internal {
            namespace VSync {
                SysCall::ThreadHandle thread_handle    = 0;
                uint32_t              stack[StackSize] = {0};

                void routine() {
                    while(true) {
                        // Marked deprecated currently
                        /*if(VSyncCallback::callback) {
                            VSyncCallback::callback();
                        }*/
                        SysCall::ChangeThread(MainThread::Handle);
                    }
                }
            }

            namespace CD {
                namespace CD_IRQ = JabyEngine::CD::internal::IRQ;

                SysCall::ThreadHandle thread_handle = 0;
                uint32_t              stack[StackSize];

                void routine(uint32_t irq) {
                    while(true) {
                        const auto old_status = CD_IO::IndexStatus.read();
                        CD_IRQ::process(irq);
                        CD_IO::IndexStatus.write(old_status);
                        irq = Callback::internal::CD::resume();
                    }
                }
            }
        }
    }
}