/**
 *  $Id: dlg_well.nss,v 1.2 2005/08/07 04:38:30 pspeed Exp $
 *
 *  This is the dialog handler script for the well.  It
 *  has one "page" that allows the user to select one of
 *  their inventory items to throw away.  I say "page"
 *  because for most players it will automatically be split
 *  into multiple pages with 10 items per page.
 *
 *  Copyright (c) 2004 Paul Speed - BSD licensed.
 *  NWN Tools - http://nwntools.sf.net/
 */
#include "zdlg_include_i"
#include "x3_inc_string"
#include "fastbuff_inc"

const string LIST_NAME = "spell list";

void Init()
{
    // We'll create a new inventory list each
    // time the player clicks on the well.
    // Note: we have to be very careful to clean it
    // up if the user ends or aborts the conversation.
    //
    // Since the inventory selections will be different
    // for every player and we don't want two simultaneous
    // conversations sharing each others' list, we'll stick
    // the list on the player.
    object oPlayer = GetPcDlgSpeaker();
    object oItem = GetItemPossessedBy(oPlayer, "fastbuff");

    if(GetLocalInt(oItem, "first_time") != 1)
    {
        AddStringElement("Understood.", LIST_NAME, oPlayer);
    }
    else
    {
        int index = 0;

        int i;
        int iSpellID;

        string sSpellName;

        for(i = 0; i < GetIntArraySize("SpellBuff", GetModule()); i++)
        {
            iSpellID = GetIntArrayElement("SpellBuff", i, GetModule());

            if(iSpellID != 0 && GetHasSpell(iSpellID, oPlayer) >= 1)
            {
                sSpellName = GetSpellName(iSpellID);

                if(GetLocalInt(oItem, IntToString(iSpellID)) != 1)
                {
                    sSpellName = "<c�>ON</c>" + " " + GetSpellName(iSpellID);
                    AddStringElement(sSpellName, LIST_NAME, oPlayer);
                    SetLocalString(oItem, "i" + IntToString(index), "s" + IntToString(iSpellID));
                    index++;
                }
                else if(GetLocalInt(oItem, IntToString(iSpellID)) == 1)
                {
                    sSpellName = "<c�>OFF</c>" + " " + GetSpellName(iSpellID);
                    AddStringElement(sSpellName, LIST_NAME, oPlayer);
                    SetLocalString(oItem, "i" + IntToString(index), "s" + IntToString(iSpellID));
                    index++;
                }
            }
        }
    }
}

void CleanUp()
{
    // Delete the list we create in Init()
    object oPlayer = GetPcDlgSpeaker();
    DeleteList( LIST_NAME, oPlayer );
}

void HandleSelection()
{
    int selection = GetDlgSelection();
    int iSpellID;
    object oPlayer = GetPcDlgSpeaker();
    object oItem = GetItemPossessedBy(oPlayer, "fastbuff");

    string sSpell;

    // Use the user selection to get the item from the
    // list.
    string sElement = GetStringElement ( selection, LIST_NAME, oPlayer);
    //FloatingTextStringOnCreature(sElement, oPlayer, FALSE);
    //FloatingTextStringOnCreature(GetSubString(sElement, 0, 13), oPlayer, FALSE);
    //object oItem = GetObjectElement( selection, LIST_NAME, oPlayer );
    if( GetLocalInt(oItem, "first_time") != 1 )
    {
        SetLocalInt(oItem, "first_time", 1);
        EndDlg();
        DelayCommand(0.1f, StartDlg( oPlayer, oItem, "script_conv", TRUE, FALSE ));
    }
    else if(GetSubString(sElement, 0, 12) == "<c�>ON</c>")
    {
        sElement = StringReplace(sElement, "<c�>ON</c>", "<c�>OFF</c>");
        sSpell = GetLocalString(oItem, "i" + IntToString(selection));
        iSpellID = StringToInt(GetSubString(sSpell, 1, 3));
        SetLocalInt(oItem, IntToString(iSpellID), 1);
        ReplaceStringElement(selection, sElement, LIST_NAME, oPlayer);
    }
    else if(GetSubString(sElement, 0, 13) == "<c�>OFF</c>")
    {
        sElement = StringReplace(sElement, "<c�>OFF</c>", "<c�>ON</c>");
        sSpell = GetLocalString(oItem, "i" + IntToString(selection));
        iSpellID = StringToInt(GetSubString(sSpell, 1, 3));
        SetLocalInt(oItem, IntToString(iSpellID), 0);
        ReplaceStringElement(selection, sElement, LIST_NAME, oPlayer);
    }
    // And end the conversation
}

void main()
{
    object oPC = GetPcDlgSpeaker();
    object oItem = GetItemPossessedBy(oPC, "fastbuff");

    string sMeta;

    int iEvent = GetDlgEventType();

    switch( iEvent )
    {
        case DLG_INIT:
            Init();
            break;
        case DLG_PAGE_INIT:
            // Only one page.
            if(GetLocalInt(oItem, "first_time") != 1)
            {
                SetDlgPrompt("Hello " + "<c�>" + GetName(oPC) + "</c>" + ", I am your fast buffer. \n \n" +
                             "I can cast all your buff spells automatically and instantly. You can target either yourself or a party member (familiars, summons, etc included). \n \n" +
                             "I will only cast spells you have learned/memorized/prepared. Don't worry I won't spend their charges/day. \n \n" +
                             "You can check which spells are going to be automatically cast or not by their prefix ON or OFF." );
            }
            else
            {

                SetDlgPrompt("Hello " + "<c�>"+ GetName(oPC) + "</c>" + ". \n \n" +
                            "These are the spells that are going to be cast automatically. \n \n" +
                            "Just click on them to disable them from being automatically cast." );
            }

            // We turn on the end prompt so that it will show up at the
            // bottom of every page.
            if(GetLocalInt(oItem, "first_time") != 1)
            {
                SetShowEndSelection( FALSE );
            }
            else
            {
                SetShowEndSelection( TRUE );
            }
            // And give it the list it should use for the page.
            SetDlgResponseList( LIST_NAME, GetPcDlgSpeaker() );
            break;
        case DLG_SELECTION:
            HandleSelection();
            break;
        case DLG_ABORT:
        case DLG_END:
            // We do the same thing on abort or end
            CleanUp();
            break;
    }
}