/*
 * This file is part of the Aaru Remote Server.
 * Copyright (c) 2019-2026 Natalia Portillo.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <debug.h>
#include <errno.h>
#include <gccore.h>
#include <wiiuse/wpad.h>

#include "../aaruremote.h"

void Initialize()
{
    void              *framebuffer;
    static GXRModeObj *rmode = NULL;

    VIDEO_Init();
    WPAD_Init();

    rmode       = VIDEO_GetPreferredMode(NULL);
    framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
    console_init(framebuffer, 20, 20, rmode->fbWidth, rmode->xfbHeight, rmode->fbWidth * VI_DISPLAY_PIX_SZ);

    VIDEO_Configure(rmode);
    VIDEO_SetNextFramebuffer(framebuffer);
    VIDEO_SetBlack(FALSE);
    VIDEO_Flush();
    VIDEO_WaitVSync();
    if(rmode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync();
}

void PlatformLoop(AaruPacketHello *pkt_server_hello) { WorkingLoop(pkt_server_hello); }

void PlatformResetIdleInput() {}

uint8_t PlatformGetIdleAction()
{
    int buttonsDown;

    WPAD_ScanPads();
    buttonsDown = WPAD_ButtonsDown(0);

    if(buttonsDown & WPAD_BUTTON_HOME) return AARUREMOTE_IDLE_ACTION_EXIT;
    if(buttonsDown & WPAD_BUTTON_A) return AARUREMOTE_IDLE_ACTION_LIST_DEVICES;

    return AARUREMOTE_IDLE_ACTION_NONE;
}

uint8_t AmIRoot() { return 1; }