// Modified by Zunath

#include "zzdlg_main_inc"
#include "bank_include"
#include "colors_inc"

const string RESPONSE_PAGE = "bank_responses";

const string PAGE_MAIN = "main_page";
const string PAGE_GOLD = "gold_page";
const string PAGE_DEPOSIT = "deposit_page";
const string PAGE_WITHDRAW = "withdraw_page";

// Prototypes
void MainPageInit();
void MainPageSelect();
void GoldPageInit();
void GoldPageSelect();
void DepositPageInit();
void DepositPageSelect();
void WithdrawPageInit();
void WithdrawPageSelect();

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();
    else if(sPage == PAGE_GOLD) GoldPageInit();
    else if(sPage == PAGE_DEPOSIT) DepositPageInit();
    else if(sPage == PAGE_WITHDRAW) WithdrawPageInit();

    dlgSetActiveResponseList( RESPONSE_PAGE );
}

// Handles any selection.
void OnSelection( string sPage )
{
    if ( sPage == PAGE_MAIN ) MainPageSelect();
    else if(sPage == PAGE_GOLD) GoldPageSelect();
    else if(sPage == PAGE_DEPOSIT) DepositPageSelect();
    else if(sPage == PAGE_WITHDRAW) WithdrawPageSelect();
}

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

void OnAbort( string sPage )
{
    DeleteLocalInt(dlgGetSpeakingPC(), "BANK_UPGRADE_CONFIRMATION");
    DeleteList( RESPONSE_PAGE, dlgGetSpeakingPC() );
}
void OnEnd( string sPage )
{
    DeleteLocalInt(dlgGetSpeakingPC(), "BANK_UPGRADE_CONFIRMATION");
    DeleteList( RESPONSE_PAGE, dlgGetSpeakingPC() );
}

void OnContinue( string sPage, int iContinuePage )
{
}

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

// Specific scripting starts here

// MAIN PAGE START
void MainPageInit( )
{
    // Remove temporary gold storage information
    dlgClearPlayerDataInt("BANK_TEMP_GOLD_STORAGE");
    object oPC = dlgGetSpeakingPC();
    object oNPC = OBJECT_SELF;
    string sBank = GetLocalString(oNPC, "BANK_NAME");
    int iSize = BANK_GetPCMaxSize(oPC, GetLocalString(oNPC, "BANK_TAG"));
    string sMsg = "Hello there. The following is what I have on file for you:\n\n" +
                  ColorTokenGreen() + "Location: " + ColorTokenEnd() + sBank + "\n" +
                  ColorTokenGreen() + "Storage Amount: " + ColorTokenEnd() + IntToString(iSize) + "\n\n" +
                  ColorTokenGreen() + "Next upgrade (+5 Items): " + ColorTokenEnd();
    int iPrice;
    int iGold = GetGold(oPC);

    // PC is at the max already.
    if(iSize >= BANK_HARD_ITEM_CAP){sMsg = sMsg + ColorTokenRed() + "MAX" + ColorTokenEnd();}
    // PC can still upgrade
    else
    {
        iPrice = (iSize + 5) * 10;
        sMsg = sMsg + IntToString(iPrice) + " Money";
    }

    dlgSetPrompt(sMsg);

    // Gold management
    dlgAddResponseAction(RESPONSE_PAGE, "Deposit/Withdraw Money");

    // PC doesn't have money, or they're at the max already.
    if(iSize < BANK_HARD_ITEM_CAP && iGold >= iPrice)
    {
        dlgAddResponseAction(RESPONSE_PAGE, "Upgrade Storage Amount: +5 Items (" + IntToString(iPrice) + " Money)");
    }


    dlgDeactivateResetResponse();
    dlgActivateEndResponse( "End" );
}

void MainPageSelect( )
{
    object oPC = dlgGetSpeakingPC();
    int iSelection = dlgGetSelectionIndex() + 1;
    object oNPC = OBJECT_SELF;
    string sBank = GetLocalString(oNPC, "BANK_TAG");
    int iSize = BANK_GetPCMaxSize(oPC, sBank);
    int iPrice = (iSize + 5) * 10;
    int iGold = GetGold(oPC);

    // PC selected to upgrade their account
    switch(iSelection)
    {
        // Deposit/Withdraw Credits (Gold)
        case 1:
        {
            dlgChangePage(PAGE_GOLD);
            DeleteLocalInt(oPC, "BANK_UPGRADE_CONFIRMATION");
            break;
        }
        // Upgrade account size
        case 2:
        {
            // Safety check to ensure the PC didn't drop their gold to the ground (which would result in free upgrades)
            if(iGold < iPrice)
            {
                SendMessageToPC(oPC, ColorTokenRed() + "You do not have enough money to increase your storage amount.");
                DeleteLocalInt(oPC, "BANK_UPGRADE_CONFIRMATION");
                return;
            }
            // Force player to confirm their choice before making it permanent
            else if(GetLocalInt(oPC, "BANK_UPGRADE_CONFIRMATION") == FALSE)
            {
                FloatingTextStringOnCreature("Storage amount increase option has been selected. Please click again to confirm your purchase.", oPC, FALSE);
                SetLocalInt(oPC, "BANK_UPGRADE_CONFIRMATION", TRUE);
            }
            // Player confirmed their choice - make the upgrade
            else if(GetLocalInt(oPC, "BANK_UPGRADE_CONFIRMATION") == TRUE)
            {
                TakeGoldFromCreature(iPrice, oPC, TRUE);
                iSize = iSize + 5;
                BANK_SetPCBankLimit(oPC, sBank, iSize);
                SendMessageToPC(oPC, ColorTokenGreen() + "You may now store " + IntToString(iSize) + " items at this location.");
                DeleteLocalInt(oPC, "BANK_UPGRADE_CONFIRMATION");
            }
            break;
        }
    }
}

void GoldPageInit()
{
    object oPC = dlgGetSpeakingPC();
    object oDatabase = GetItemPossessedBy(oPC, PC_DATABASE);
    object oNPC = OBJECT_SELF;
    string sBank = GetLocalString(oNPC, "BANK_TAG");
    int iStoredGold = GetLocalInt(oDatabase, "BANK_STORED_GOLD");
    string sMsg = "Currently stored credits: " + IntToString(iStoredGold) + "\n\n" +
                  "What would you like to do?";

    dlgSetPrompt(sMsg);

    dlgAddResponseAction(RESPONSE_PAGE, "Deposit Money");
    dlgAddResponseAction(RESPONSE_PAGE, "Withdraw Money");

    dlgActivateResetResponse("Main Menu");
    dlgActivateEndResponse("End");
}

void GoldPageSelect()
{
    int iIndex = dlgGetSelectionIndex() + 1;

    switch(iIndex)
    {
        case 1: dlgChangePage(PAGE_DEPOSIT); break;
        case 2: dlgChangePage(PAGE_WITHDRAW); break;
    }
}

void DepositPageInit()
{
    object oPC = dlgGetSpeakingPC();
    object oDatabase = GetItemPossessedBy(oPC, PC_DATABASE);
    int iStoredGold = GetLocalInt(oDatabase, "BANK_STORED_GOLD");
    int iCurrentAmount = dlgGetPlayerDataInt("BANK_TEMP_GOLD_STORAGE");
    string sMsg = "How much money would you like to deposit?\n\n" +
                  "Money in account: " + IntToString(iStoredGold) + "\n" +
                  "Currently depositing: " + IntToString(iCurrentAmount) + " money";

    dlgSetPrompt(sMsg);

    dlgAddResponseAction(RESPONSE_PAGE, "Store All Money");
    dlgAddResponseAction(RESPONSE_PAGE, "Increase By 10,000");
    dlgAddResponseAction(RESPONSE_PAGE, "Increase By 1,000");
    dlgAddResponseAction(RESPONSE_PAGE, "Increase By 100");
    dlgAddResponseAction(RESPONSE_PAGE, "Increase By 10");
    dlgAddResponseAction(RESPONSE_PAGE, "Increase By 1");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease By 10,000");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease By 1,000");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease By 100");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease By 10");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease By 1");
    dlgAddResponseAction(RESPONSE_PAGE, "Store " + IntToString(iCurrentAmount) + " money");

    dlgActivateResetResponse("Main Menu");
    dlgActivateEndResponse("End");
}

void DepositPageSelect()
{
    object oPC = dlgGetSpeakingPC();
    object oDatabase = GetItemPossessedBy(oPC, PC_DATABASE);
    int iSelection = dlgGetSelectionIndex() + 1;
    int iCurrentAmount = dlgGetPlayerDataInt("BANK_TEMP_GOLD_STORAGE");
    int iStoredAmount = GetLocalInt(oDatabase, "BANK_STORED_GOLD");
    int iGold = GetGold(oPC);
    switch(iSelection)
    {
        // Store all gold
        case 1:
        {
            TakeGoldFromCreature(iGold, oPC, TRUE);
            iStoredAmount = iStoredAmount + iGold;
            SetLocalInt(oDatabase, "BANK_STORED_GOLD", iStoredAmount);
            DeleteLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE");
            break;
        }
        // Increase by 10,000
        case 2:
        {
            iCurrentAmount = iCurrentAmount + 10000;
            if(iCurrentAmount > iGold){iCurrentAmount = iGold;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Increase by 1,000
        case 3:
        {
            iCurrentAmount = iCurrentAmount + 1000;
            if(iCurrentAmount > iGold){iCurrentAmount = iGold;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Increase by 100
        case 4:
        {
            iCurrentAmount = iCurrentAmount + 100;
            if(iCurrentAmount > iGold){iCurrentAmount = iGold;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Increase by 10
        case 5:
        {
            iCurrentAmount = iCurrentAmount + 10;
            if(iCurrentAmount > iGold){iCurrentAmount = iGold;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Increase by 1
        case 6:
        {
            iCurrentAmount = iCurrentAmount + 1;
            if(iCurrentAmount > iGold){iCurrentAmount = iGold;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Decrease by 10,000
        case 7:
        {
            iCurrentAmount = iCurrentAmount - 10000;
            if(iCurrentAmount < 0){iCurrentAmount = 0;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Decrease by 1,000
        case 8:
        {
            iCurrentAmount = iCurrentAmount - 1000;
            if(iCurrentAmount < 0){iCurrentAmount = 0;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Decrease by 100
        case 9:
        {
            iCurrentAmount = iCurrentAmount - 100;
            if(iCurrentAmount < 0){iCurrentAmount = 0;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Decrease by 10
        case 10:
        {
            iCurrentAmount = iCurrentAmount - 10;
            if(iCurrentAmount < 0){iCurrentAmount = 0;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Decrease by 1
        case 11:
        {
            iCurrentAmount = iCurrentAmount - 1;
            if(iCurrentAmount < 0){iCurrentAmount = 0;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Store credits
        case 12:
        {
            iStoredAmount = iStoredAmount + iCurrentAmount;
            TakeGoldFromCreature(iCurrentAmount, oPC, TRUE);
            SetLocalInt(oDatabase, "BANK_STORED_GOLD", iStoredAmount);
            DeleteLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE");
            break;
        }
    }
}

void WithdrawPageInit()
{
    object oPC = dlgGetSpeakingPC();
    object oDatabase = GetItemPossessedBy(oPC, PC_DATABASE);
    int iStoredGold = GetLocalInt(oDatabase, "BANK_STORED_GOLD");
    int iCurrentAmount = dlgGetPlayerDataInt("BANK_TEMP_GOLD_STORAGE");
    string sMsg = "How much money would you like to withdraw?\n\n" +
                  "Money in account: " + IntToString(iStoredGold) + "\n" +
                  "Currently withdrawing: " + IntToString(iCurrentAmount) + " money";

    dlgSetPrompt(sMsg);

    dlgAddResponseAction(RESPONSE_PAGE, "Withdraw All Money");
    dlgAddResponseAction(RESPONSE_PAGE, "Increase By 10,000");
    dlgAddResponseAction(RESPONSE_PAGE, "Increase By 1,000");
    dlgAddResponseAction(RESPONSE_PAGE, "Increase By 100");
    dlgAddResponseAction(RESPONSE_PAGE, "Increase By 10");
    dlgAddResponseAction(RESPONSE_PAGE, "Increase By 1");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease By 10,000");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease By 1,000");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease By 100");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease By 10");
    dlgAddResponseAction(RESPONSE_PAGE, "Decrease By 1");
    dlgAddResponseAction(RESPONSE_PAGE, "Withdraw " + IntToString(iCurrentAmount) + " money");

    dlgActivateResetResponse("Main Menu");
    dlgActivateEndResponse("End");
}

void WithdrawPageSelect()
{
    object oPC = dlgGetSpeakingPC();
    object oDatabase = GetItemPossessedBy(oPC, PC_DATABASE);
    int iSelection = dlgGetSelectionIndex() + 1;
    int iCurrentAmount = dlgGetPlayerDataInt("BANK_TEMP_GOLD_STORAGE");
    int iStoredAmount = GetLocalInt(oDatabase, "BANK_STORED_GOLD");
    int iGold = GetGold(oPC);
    switch(iSelection)
    {
        // Withdraw all gold
        case 1:
        {
            GiveGoldToCreature(oPC, iStoredAmount);
            SetLocalInt(oDatabase, "BANK_STORED_GOLD", 0);
            DeleteLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE");
            break;
        }
        // Increase by 10,000
        case 2:
        {
            iCurrentAmount = iCurrentAmount + 10000;
            if(iCurrentAmount > iStoredAmount){iCurrentAmount = iStoredAmount;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Increase by 1,000
        case 3:
        {
            iCurrentAmount = iCurrentAmount + 1000;
            if(iCurrentAmount > iStoredAmount){iCurrentAmount = iStoredAmount;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Increase by 100
        case 4:
        {
            iCurrentAmount = iCurrentAmount + 100;
            if(iCurrentAmount > iStoredAmount){iCurrentAmount = iStoredAmount;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Increase by 10
        case 5:
        {
            iCurrentAmount = iCurrentAmount + 10;
            if(iCurrentAmount > iStoredAmount){iCurrentAmount = iStoredAmount;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Increase by 1
        case 6:
        {
            iCurrentAmount = iCurrentAmount + 1;
            if(iCurrentAmount > iStoredAmount){iCurrentAmount = iStoredAmount;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Decrease by 10,000
        case 7:
        {
            iCurrentAmount = iCurrentAmount - 10000;
            if(iCurrentAmount < 0){iCurrentAmount = 0;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Decrease by 1,000
        case 8:
        {
            iCurrentAmount = iCurrentAmount - 1000;
            if(iCurrentAmount < 0){iCurrentAmount = 0;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Decrease by 100
        case 9:
        {
            iCurrentAmount = iCurrentAmount - 100;
            if(iCurrentAmount < 0){iCurrentAmount = 0;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Decrease by 10
        case 10:
        {
            iCurrentAmount = iCurrentAmount - 10;
            if(iCurrentAmount < 0){iCurrentAmount = 0;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Decrease by 1
        case 11:
        {
            iCurrentAmount = iCurrentAmount - 1;
            if(iCurrentAmount < 0){iCurrentAmount = 0;}
            SetLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE", iCurrentAmount);
            break;
        }
        // Withdraw credits
        case 12:
        {
            iStoredAmount = iStoredAmount - iCurrentAmount;
            SetLocalInt(oDatabase, "BANK_STORED_GOLD", iStoredAmount);
            DeleteLocalInt(oPC, "BANK_TEMP_GOLD_STORAGE");
            GiveGoldToCreature(oPC, iCurrentAmount);
            break;
        }
    }
}