#include "mainmenu.h"
#include "../gfx/gfx.h"
#include "../gfx/gfxutils.h"
#include "../gfx/menu.h"
#include "tools.h"
#include "../hid/hid.h"
#include "../fs/menus/explorer.h"
#include <utils/btn.h>
#include <storage/sd.h>
#include "tconf.h"
#include "../keys/keys.h"
#include "../storage/mountmanager.h"
#include "../storage/gptmenu.h"
#include "../storage/emummc.h"
#include <utils/util.h>
#include "../fs/fsutils.h"
#include <soc/fuse.h>
#include "../utils/utils.h"
#include "../config.h"

#include "../fs/readers/folderReader.h"
#include "../fs/fstypes.h"
#include "../fs/fscopy.h"
#include <utils/sprintf.h>
#include <stdlib.h>
#include <string.h>

#include "../apl/apl.h"

extern hekate_config h_cfg;

enum {
    MainExplore = 0,
    DeleteBootFlags,
    FixArchiveBitA,
    FixArchiveBitN,
    // FixAll,
    MainAMS,
    DeleteThemes,
    FixAMS_4200,
    FixAMS_6900,
    FixAMS_BD00,
    MainConvenience,
    FixMarikoWarmbootSleep,
    FixMacSpecialFolders,
    MainOther,
    MainViewStillNoBootInfo,
    MainViewCredits,
    MainExit,
    MainPowerOff,
    MainRebootRCM,
    // MainRebootNormal,
    MainRebootHekate,
};

MenuEntry_t mainMenuEntries[] = {
    [MainExplore] = {.optionUnion = COLORTORGB(COLOR_WHITE) | SKIPBIT, .name = "-- Behebe Start Probleme --"},
    [DeleteBootFlags] = {.optionUnion = COLORTORGB(COLOR_GREEN), .name = "Automatisches starten der sysmodule deaktivieren"},
    [FixArchiveBitA] = {.optionUnion = COLORTORGB(COLOR_GREEN), .name = "Behebe Archive Bit (alle Ordner ausser Nintendo)"},
    [FixArchiveBitN] = {.optionUnion = COLORTORGB(COLOR_GREEN), .name = "Behebe Archive Bit (Nintendo Ordner)"},
    // [FixAll] = {.optionUnion = COLORTORGB(COLOR_GREEN), .name = "Try everything"},

    [MainAMS] = {.optionUnion = COLORTORGB(COLOR_WHITE) | SKIPBIT, .name = "\n-- Atmosphere Fehlercodes --"},
    [DeleteThemes] = {.optionUnion = COLORTORGB(COLOR_CYAN), .name = "Fix 0100000000001000 (Installiertes Theme)"},
    [FixAMS_4200] = {.optionUnion = COLORTORGB(COLOR_CYAN), .name = "Fix 4200000000000010 (ldn_mitm)"},
    [FixAMS_6900] = {.optionUnion = COLORTORGB(COLOR_CYAN), .name = "Fix 690000000000000D (sys-con)"},
    [FixAMS_BD00] = {.optionUnion = COLORTORGB(COLOR_CYAN), .name = "Fix 010000000000BD00 (MissionControl)"},

    [MainConvenience] = {.optionUnion = COLORTORGB(COLOR_WHITE) | SKIPBIT, .name = "\n-- Bequemlichkeit --"},
    [FixMarikoWarmbootSleep] = {.optionUnion = COLORTORGB(COLOR_ORANGE), .name = "Mariko Sleep Fix (Warmboot)"},
    [FixMacSpecialFolders] = {.optionUnion = COLORTORGB(COLOR_ORANGE), .name = "Entferne spezielle MacOS Dateien"},

    [MainOther] = {.optionUnion = COLORTORGB(COLOR_WHITE) | SKIPBIT, .name = "\n-- Sonstiges --"},
    [MainViewStillNoBootInfo] = {.optionUnion = COLORTORGB(COLOR_YELLOW), .name = "Meine Switch startet nicht mehr"},
    [MainViewCredits] = {.optionUnion = COLORTORGB(COLOR_YELLOW), .name = "Ueber"},

    [MainExit] = {.optionUnion = COLORTORGB(COLOR_WHITE) | SKIPBIT, .name = "\n-- Beenden --"},
    [MainPowerOff] = {.optionUnion = COLORTORGB(COLOR_PURPLE), .name = "Ausschalten"},
    [MainRebootRCM] = {.optionUnion = COLORTORGB(COLOR_PURPLE), .name = "Neustart in RCM-Modus"},
    // [MainRebootNormal] = {.optionUnion = COLORTORGB(COLOR_VIOLET), .name = "Normaler Neustart"},
    [MainRebootHekate] = {.optionUnion = COLORTORGB(COLOR_PURPLE), .name = "Neustart in hekate"}
    
    
    // [MainBrowseSd] = {.optionUnion = COLORTORGB(COLOR_GREEN), .name = "Browse SD"},
    // [MainMountSd] = {.optionUnion = COLORTORGB(COLOR_YELLOW)}, // To mount/unmount the SD
    // [MainBrowseEmmc] = {.optionUnion = COLORTORGB(COLOR_BLUE), .name = "Browse EMMC"},
    // [MainBrowseEmummc] = {.optionUnion = COLORTORGB(COLOR_BLUE), .name = "Browse EMUMMC"},
    // [MainTools] = {.optionUnion = COLORTORGB(COLOR_WHITE) | SKIPBIT, .name = "\n-- Tools --"},
    // [MainPartitionSd] = {.optionUnion = COLORTORGB(COLOR_ORANGE), .name = "Partition the sd"},
    // [MainDumpFw] = {.optionUnion = COLORTORGB(COLOR_BLUE), .name = "Dump Firmware"},
    // [MainViewKeys] = {.optionUnion = COLORTORGB(COLOR_YELLOW), .name = "View dumped keys"},

};

void HandleSD(){
    gfx_clearscreen();
    TConf.curExplorerLoc = LOC_SD;
    if (!sd_mount() || sd_get_card_removed()){
        gfx_printf("Sd is not mounted!");
        hidWait();
    }
    else
        FileExplorer("sd:/");
}


extern bool is_sd_inited;
void RebootToHekate(){
    reboot_to_hekate_menu();
}

void MountOrUnmountSD(){
    gfx_clearscreen();
    if (sd_get_card_mounted())
        sd_unmount();
    else if (!sd_mount())
        hidWait();
}


void archBitHelperA(){
    m_entry_fixArchiveBit(0);
}
void archBitHelperN(){
    m_entry_fixArchiveBit(1);
}


menuPaths mainMenuPaths[] = {
    [DeleteBootFlags] = m_entry_deleteBootFlags,
    [DeleteThemes] = m_entry_deleteInstalledThemes,
    [FixArchiveBitA] = archBitHelperA,
    [FixArchiveBitN] = archBitHelperN,
    [FixAMS_4200] = m_entry_fixAMSError_4200,
    [FixAMS_6900] = m_entry_fixAMSError_6900,
    [FixAMS_BD00] = m_entry_fixAMSError_BD00,
    [FixMacSpecialFolders] = m_entry_fixMacSpecialFolders,
    [FixMarikoWarmbootSleep] = m_entry_fixMarikoWarmbootSleep,
    // [FixAll] = m_entry_fixAll,
    [MainViewStillNoBootInfo] = m_entry_stillNoBootInfo,
    [MainRebootHekate] = RebootToHekate,
    [MainRebootRCM] = reboot_rcm,
    [MainPowerOff] = power_off,
    [MainViewCredits] = m_entry_ViewCredits,
};

void EnterMainMenu(){
    int res = 0;
    while (1){
        if (sd_get_card_removed())
            sd_unmount();

        // // -- Exit --
        mainMenuEntries[MainRebootHekate].hide = !h_cfg.t210b01 && (!sd_get_card_mounted() || !hekate_reboot_available());
        mainMenuEntries[MainRebootRCM].hide = h_cfg.t210b01;
        mainMenuEntries[FixMarikoWarmbootSleep].hide = !h_cfg.t210b01;

        gfx_clearscreen();
        gfx_putc('\n');
        
        Vector_t ent = vecFromArray(mainMenuEntries, ARR_LEN(mainMenuEntries), sizeof(MenuEntry_t));
        res = newMenu(&ent, res, 79, 30, ALWAYSREDRAW, 0);
        if (mainMenuPaths[res] != NULL)
            mainMenuPaths[res]();
    }
}
