// Modified by Zunath

#include "zzdlg_main_inc"
#include "inc_mysql_tables"
#include "colors_inc"
#include "portrait_include"

const string RESPONSE_PAGE = "portraitmenu_responses";

const string PAGE_MAIN = "main_page";


// Prototypes
void MainPageInit();
void MainPageSelect();

void OnInit()
{
    dlgChangeLabelNext("Next page");
    dlgChangeLabelPrevious("Previous page");
    dlgChangePage(PAGE_MAIN);
    dlgActivatePreservePageNumberOnSelection();
    dlgActivateResetResponse();
}

// Create the page
void OnPageInit( string sPage )
{
    DeleteList( RESPONSE_PAGE, dlgGetSpeakingPC() );

    if( sPage == PAGE_MAIN ) MainPageInit( );

    dlgSetActiveResponseList( RESPONSE_PAGE );
}

// Handles any selection.
void OnSelection( string sPage )
{
    if ( sPage == PAGE_MAIN ) MainPageSelect( );
}

void OnReset( string sPage )
{
    dlgChangePage( PAGE_MAIN );
    dlgResetPageNumber( );
}

void OnAbort( string sPage )
{
    object oPC = dlgGetSpeakingPC();
    DeleteList( RESPONSE_PAGE, oPC );

    // Reset to original portrait
    SetPortraitId(oPC, GetLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT"));

    // Remove temporary variables
    DeleteLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT");
    DeleteLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT_DBID");
    DeleteLocalInt(oPC, "TEMP_CURRENT_PORTRAIT_DBID");
}
void OnEnd( string sPage )
{
    object oPC = dlgGetSpeakingPC();
    DeleteList( RESPONSE_PAGE, oPC );

    // Reset to original portrait
    SetPortraitId(oPC, GetLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT"));

    // Remove temporary variables
    DeleteLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT");
    DeleteLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT_DBID");
    DeleteLocalInt(oPC, "TEMP_CURRENT_PORTRAIT_DBID");
}

void OnContinue( string sPage, int iContinuePage )
{
}

// Message handler
void main()
{
    dlgOnMessage();
}

// Specific scripting starts here

// MAIN PAGE START
void MainPageInit( )
{
    object oPC = dlgGetSpeakingPC();
    int iOriginalPortrait = GetLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT");
    int iOriginalDBID     = GetLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT_DBID");
    int iCurrentPortrait  = GetPortraitId(oPC);
    int iCurrentDBID      = StringToInt(GetMySQLData(PORTRAIT_MYSQL_TABLE, "ID", iCurrentPortrait, "2DAID"));
    SetLocalInt(oPC, "TEMP_CURRENT_PORTRAIT_DBID", iCurrentDBID);

    string sPrompt =  "You may adjust your character's portrait here. Open your character sheet to view your current portrait ('C' by default).\n\n";
           sPrompt += ColorTokenGreen() + "Set Portrait ID: " + ColorTokenEnd() + IntToString(iOriginalDBID) + "\n";
           sPrompt += ColorTokenGreen() + "Viewing Portrait ID: " + ColorTokenEnd() + IntToString(iCurrentDBID);
    dlgSetPrompt(sPrompt);


    // Action commands (Increase/Decrease portrait ID by set value)
    dlgAddResponseAction(RESPONSE_PAGE, "Advance Portrait ID by 1");
    dlgAddResponseAction(RESPONSE_PAGE, "Advance Portrait ID by 10");
    dlgAddResponseAction(RESPONSE_PAGE, "Advance Portrait ID by 100");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease Portrait ID by 1");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease Portrait ID by 10");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease Portrait ID by 100");
    // Action commands (Set current portrait ID permanently)
    dlgAddResponseAction(RESPONSE_PAGE, "Set Portrait ID (" + IntToString(iCurrentDBID) + ")");
    // Action commands (Revert to original portrait ID)
    dlgAddResponseAction(RESPONSE_PAGE, "Reset Portrait");
    // Return to main menu
    dlgAddResponseAction(RESPONSE_PAGE, "Back", ColorTokenBlue());
    dlgDeactivateResetResponse();
}

void MainPageSelect( )
{
    object oPC = dlgGetSpeakingPC();
    object oModule = GetModule();
    int iNumberOfPortraits = GetLocalInt(oModule, PORTRAIT_COUNT);
    int iCurrentPortrait  = GetPortraitId(oPC);
    int iCurrentDBID      = GetLocalInt(oPC, "TEMP_CURRENT_PORTRAIT_DBID");

    if(dlgIsSelectionEqualToName("Back"))
    {
        // Reset to original portrait
        SetPortraitId(oPC, GetLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT"));

        // Remove temporary variables
        DeleteLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT");
        DeleteLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT_DBID");
        DeleteLocalInt(oPC, "TEMP_CURRENT_PORTRAIT_DBID");
        dlgChangeDlgScript(oPC, "rest_menu");
        return;
    }

    else if(dlgIsSelectionEqualToName("Advance Portrait ID by 1"))
    {
        int iNewID = iCurrentDBID + 1;
        if(iNewID > iNumberOfPortraits) iNewID = 1;
        iNewID = StringToInt(GetMySQLData(PORTRAIT_MYSQL_TABLE, "2DAID", iNewID, "ID"));

        SetPortraitId(oPC, iNewID);
    }
    else if(dlgIsSelectionEqualToName("Advance Portrait ID by 10"))
    {
        int iNewID = iCurrentDBID + 10;
        if(iNewID > iNumberOfPortraits) iNewID = iNewID - iNumberOfPortraits;
        iNewID = StringToInt(GetMySQLData(PORTRAIT_MYSQL_TABLE, "2DAID", iNewID, "ID"));

        SetPortraitId(oPC, iNewID);
    }
    else if(dlgIsSelectionEqualToName("Advance Portrait ID by 100"))
    {
        int iNewID = iCurrentDBID + 100;
        if(iNewID > iNumberOfPortraits) iNewID = iNewID - iNumberOfPortraits;
        iNewID = StringToInt(GetMySQLData(PORTRAIT_MYSQL_TABLE, "2DAID", iNewID, "ID"));

        SetPortraitId(oPC, iNewID);
    }

    else if(dlgIsSelectionEqualToName("Decrease Portrait ID by 1"))
    {
        int iNewID = iCurrentDBID - 1;
        if(iNewID < 1) iNewID = iNumberOfPortraits;
        iNewID = StringToInt(GetMySQLData(PORTRAIT_MYSQL_TABLE, "2DAID", iNewID, "ID"));

        SetPortraitId(oPC, iNewID);
    }
    else if(dlgIsSelectionEqualToName("Decrease Portrait ID by 10"))
    {
        int iNewID = iCurrentDBID - 10;
        if(iNewID < 1) iNewID = iNumberOfPortraits - abs(iNewID);
        iNewID = StringToInt(GetMySQLData(PORTRAIT_MYSQL_TABLE, "2DAID", iNewID, "ID"));

        SetPortraitId(oPC, iNewID);
    }
    else if(dlgIsSelectionEqualToName("Decrease Portrait ID by 100"))
    {
        int iNewID = iCurrentDBID - 100;
        if(iNewID < 1) iNewID = iNumberOfPortraits - abs(iNewID);
        iNewID = StringToInt(GetMySQLData(PORTRAIT_MYSQL_TABLE, "2DAID", iNewID, "ID"));

        SetPortraitId(oPC, iNewID);
    }

    else if(dlgIsSelectionEqualToName("Set Portrait ID (" + IntToString(iCurrentDBID) + ")"))
    {
        SetLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT", iCurrentPortrait);
        SetLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT_DBID", iCurrentDBID);
    }

    else if(dlgIsSelectionEqualToName("Reset Portrait"))
    {
        SetPortraitId(oPC, GetLocalInt(oPC, "TEMP_ORIGINAL_PORTRAIT"));
    }
}