#include "nw_i0_plot"

void CheckWinnersAndReward() {
    object oArea = GetArea(OBJECT_SELF);
    object oPC = GetFirstPC();
    int nRedAlive = 0;
    int nBlackAlive = 0;
    object oTarget = GetFirstObjectInArea();

    // Loop through all objects in the area to check the status of creatures
    while (GetIsObjectValid(oTarget)) {
        if (GetObjectType(oTarget) == OBJECT_TYPE_CREATURE) {
            if (GetTag(oTarget) == "CAS_ARENARED") {
                nRedAlive = 1;
            } else if (GetTag(oTarget) == "CAS_ARENABLACK") {
                nBlackAlive = 1;
            }
        }
        oTarget = GetNextObjectInArea();
    }

    // Debug message to check which creature won
    string sWinnerMessage = "";
    if (nRedAlive == 1 && nBlackAlive == 0) {
        sWinnerMessage = "Red creature is alive, it won.";
    } else if (nBlackAlive == 1 && nRedAlive == 0) {
        sWinnerMessage = "Black creature is alive, it won.";
    } else {
        sWinnerMessage = "Unable to determine the winner.";
    }
    SendMessageToPC(GetFirstPC(), sWinnerMessage);

    // Determine the last creature destroyed
    int nRedDestroyedLast = (nRedAlive == 0 && nBlackAlive == 1) ? 1 : 0;
    int nBlackDestroyedLast = (nBlackAlive == 0 && nRedAlive == 1) ? 1 : 0;

    // Check all PCs in the area
    SendMessageToPC(GetFirstPC(), "Starting to check PCs in the area.");
    while (GetIsObjectValid(oPC)) {
        if (GetArea(oPC) == oArea) {
            int nBetRed = GetLocalInt(oPC, "I_BET_ON_RED");
            int nBetBlack = GetLocalInt(oPC, "I_BET_ON_BLACK");

            // Reward the correct bets based on the creature destroyed last
            if ((nRedDestroyedLast == 1 && nBetRed > 0) || 
                (nBlackDestroyedLast == 1 && nBetBlack > 0)) {
                int nTicketsToReward = (nRedDestroyedLast == 1) ? nBetRed : nBetBlack;
                SendMessageToPC(oPC, "Rewarding Tickets: " + IntToString(nTicketsToReward));
                CreateItemOnObject("cas_ticket", oPC, nTicketsToReward);
            }

            // Delete the betting variables for every PC
            DeleteLocalInt(oPC, "I_BET_ON_RED");
            DeleteLocalInt(oPC, "I_BET_ON_BLACK");
            SendMessageToPC(oPC, "Deleted Betting Variables.");
        }
        oPC = GetNextPC();
    }
}



void main() {
    SendMessageToPC(GetFirstPC(), "Black Warrior has died.");

    object oPC = GetFirstPC();
    object oArea = GetArea(OBJECT_SELF);

    while (GetIsObjectValid(oPC)) {
        if (GetArea(oPC) == oArea) {
            int nBetRed = GetLocalInt(oPC, "I_BET_ON_RED");

            if (nBetBlack > 0) {
                SendMessageToPC(oPC, "Rewarding Tickets for Betting on Black: " + IntToString(nBetRed));
                CreateItemOnObject("cas_ticket", oPC, nBetRed);
            }

            DeleteLocalInt(oPC, "I_BET_ON_RED");
            DeleteLocalInt(oPC, "I_BET_ON_BLACK");
        }
        oPC = GetNextPC();
    }

    // Destroy itself after 12 seconds
    DelayCommand(6.0, DestroyObject(OBJECT_SELF));
    
}