// Modified by Zunath

#include "rhs_include"
#include "zzdlg_main_inc"

const string RESPONSE_PAGE = "rhs_furn_menu_responses";

const string PAGE_MAIN = "main_page";

const string PAGE_BUY_FURNITURE_CATEGORIES = "buy_furniture_categories_page";
const string PAGE_BUY_FURNITURE_LIST = "buy_furniture_list_page";
const string PAGE_BUY_FURNITURE_DETAILS = "buy_furniture_details_page";

const string PAGE_SELL_FURNITURE_LIST = "sell_furniture_list_page";
const string PAGE_SELL_FURNITURE_DETAILS = "sell_furniture_details_page";

const string PAGE_ROTATE_FURNITURE = "rotate_furniture_page";
const string PAGE_ROTATE_FURNITURE_DETAILS = "rotate_furniture_details_page";

const string PAGE_MOVE_FURNITURE = "move_furniture_page";

/////////////////////////
// TEMPORARY VARIABLES //
/////////////////////////

// Tracks the category selected by oPC in the buy/sell furniture categories
const string RHS_MENU_TEMP_CATEGORY_SELECTED = "RHS_MENU_TEMP_CATEGORY_SELECTED";
// Tracks the furniture selected by oPC in the buy furniture types
const string RHS_MENU_TEMP_FURNITURE_SELECTED = "RHS_MENU_TEMP_FURNITURE_SELECTED";
// Tracks whether or not oPC is currently previewing a furniture object
const string RHS_MENU_TEMP_PREVIEWING_FURNITURE = "RHS_MENU_TEMP_PREVIEWING_FURNITURE";
// Tracks the price of a particular piece of furniture
const string RHS_MENU_TEMP_FURNITURE_COST = "RHS_MENU_TEMP_FURNITURE_COST";
// Tracks the specific furniture object that was selected by oPC
const string RHS_MENU_TEMP_FURNITURE_OBJECT = "RHS_MENU_TEMP_FURNITURE_OBJECT";


void ClearTemporaryVariables()
{
    object oPC = dlgGetSpeakingPC();

    DeleteLocalString(oPC, RHS_MENU_TEMP_CATEGORY_SELECTED);
    DeleteLocalString(oPC, RHS_MENU_TEMP_FURNITURE_SELECTED);
    DeleteLocalInt(oPC, RHS_MENU_TEMP_PREVIEWING_FURNITURE);
    DeleteLocalInt(oPC, RHS_MENU_TEMP_FURNITURE_COST);
    DeleteLocalObject(oPC, RHS_MENU_TEMP_FURNITURE_OBJECT);
}

void DisplayFurnitureCategories()
{
    // Display the categories
    string sSQL = "SELECT Name FROM " + RHS_SCHEMA + "." + RHS_TABLE_FURNITURE_CATEGORIES + " ORDER BY Name;";
    SQLExecDirect(sSQL);

    while(SQLFetch() == SQL_SUCCESS)
    {
        dlgAddResponseAction(RESPONSE_PAGE, SQLDecodeSpecialChars(SQLGetData(1)));
    }
}

void DisplayFurnitureList(int iCategoryID)
{
    // Show all furniture types for the chosen category
    string sSQL = "SELECT Name FROM " + RHS_SCHEMA + "." + RHS_TABLE_FURNITURE_TYPES + " WHERE Category=" + IntToString(iCategoryID) + ";";
    SQLExecDirect(sSQL);

    while(SQLFetch() == SQL_SUCCESS)
    {
        dlgAddResponseAction(RESPONSE_PAGE, SQLGetData(1));
    }
}

void DisplayNearestFurnitureList(location lTargetLocation)
{
    // Display all furniture objects a a response selection
    int iCurrent = 1;
    object oNearest = GetNearestObjectToLocation(OBJECT_TYPE_PLACEABLE, lTargetLocation, iCurrent);
    location lNearestLocation = GetLocation(oNearest);
    float fDistance = GetDistanceBetweenLocations(lTargetLocation, lNearestLocation);
    while(GetIsObjectValid(oNearest) && fDistance <= RHS_FURNITURE_DISTANCE)
    {
        if(GetLocalInt(oNearest, RHS_FURNITURE_ID_NUMBER) > 0)
        {
            dlgAddResponseAction(RESPONSE_PAGE, GetName(oNearest));
        }

        iCurrent++;
        oNearest = GetNearestObjectToLocation(OBJECT_TYPE_PLACEABLE, lTargetLocation, iCurrent);
        lNearestLocation = GetLocation(oNearest);
        fDistance = GetDistanceBetweenLocations(lTargetLocation, lNearestLocation);
    }
}

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

void BuyFurnitureCategoriesPageInit();
void BuyFurnitureCategoriesPageSelect();
void BuyFurnitureListPageInit();
void BuyFurnitureListPageSelect();
void BuyFurnitureDetailsPageInit();
void BuyFurnitureDetailsPageSelect();

void SellFurniturePageInit();
void SellFurniturePageSelect();
void SellFurnitureDetailsPageInit();
void SellFurnitureDetailsPageSelect();

void RotateFurniturePageInit();
void RotateFurniturePageSelect();
void RotateFurnitureDetailsPageInit();
void RotateFurnitureDetailsPageSelect();

void MoveFurniturePageInit();
void MoveFurniturePageSelect();

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_BUY_FURNITURE_CATEGORIES) BuyFurnitureCategoriesPageInit();
    else if(sPage == PAGE_BUY_FURNITURE_LIST) BuyFurnitureListPageInit();
    else if(sPage == PAGE_BUY_FURNITURE_DETAILS) BuyFurnitureDetailsPageInit();

    else if(sPage == PAGE_SELL_FURNITURE_LIST) SellFurniturePageInit();
    else if(sPage == PAGE_SELL_FURNITURE_DETAILS) SellFurnitureDetailsPageInit();

    else if(sPage == PAGE_ROTATE_FURNITURE) RotateFurniturePageInit();
    else if(sPage == PAGE_ROTATE_FURNITURE_DETAILS) RotateFurnitureDetailsPageInit();

    else if(sPage == PAGE_MOVE_FURNITURE) MoveFurniturePageInit();

    dlgSetActiveResponseList( RESPONSE_PAGE );
}

// Handles any selection.
void OnSelection( string sPage )
{
    if ( sPage == PAGE_MAIN ) MainPageSelect( );
    else if(sPage == PAGE_BUY_FURNITURE_CATEGORIES) BuyFurnitureCategoriesPageSelect();
    else if(sPage == PAGE_BUY_FURNITURE_LIST) BuyFurnitureListPageSelect();
    else if(sPage == PAGE_BUY_FURNITURE_DETAILS) BuyFurnitureDetailsPageSelect();

    else if(sPage == PAGE_SELL_FURNITURE_LIST) SellFurniturePageSelect();
    else if(sPage == PAGE_SELL_FURNITURE_DETAILS) SellFurnitureDetailsPageSelect();

    else if(sPage == PAGE_ROTATE_FURNITURE) RotateFurniturePageSelect();
    else if(sPage == PAGE_ROTATE_FURNITURE_DETAILS) RotateFurnitureDetailsPageSelect();

    else if(sPage == PAGE_MOVE_FURNITURE) MoveFurniturePageSelect();
}

void OnReset( string sPage )
{
    ClearTemporaryVariables();

    dlgChangePage( PAGE_MAIN );
    dlgResetPageNumber( );
}

void OnAbort( string sPage )
{
    ClearTemporaryVariables();

    DeleteList( RESPONSE_PAGE, dlgGetSpeakingPC() );
}
void OnEnd( string sPage )
{
    ClearTemporaryVariables();

    DeleteList( RESPONSE_PAGE, dlgGetSpeakingPC() );
}

void OnContinue( string sPage, int iContinuePage )
{
}

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

// Specific scripting starts here

// MAIN PAGE START
void MainPageInit( )
{
    object oPC = dlgGetSpeakingPC();

    // Remove the "move object" temporary variable
    DeleteLocalObject(oPC, RHS_FURNITURE_MOVING_OBJECT_REFERENCE);
    // Remove the "is moving object" temporary variable
    DeleteLocalInt(oPC, RHS_FURNITURE_MOVING_OBJECT);

    object oArea = GetArea(oPC);
    object oDatabase = GetItemPossessedBy(oPC, PC_DATABASE);
    location lTargetLocation = APSStringToLocation(GetLocalString(oPC, RHS_FURNITURE_TOOL_TARGET_LOCATION));
    int iPCID = GetLocalInt(oDatabase, PC_ID_NUMBER);
    int iAreaPCID = GetLocalInt(oArea, RHS_HOUSE_OWNER_ID_NUMBER);
    string sPrompt = ColorTokenGreen() + RHS_HOUSE_TERM_PROPER + " Menu\n\n" + ColorTokenEnd();

    // Check if oPC is owner or a trustee. Otherwise he or she cannot use this menu
    if(iPCID != iAreaPCID)
    {
        if(RHS_IsPlayerFriend(OBJECT_INVALID, OBJECT_INVALID, iPCID, iAreaPCID) != RHS_FRIEND_TYPE_TRUSTEE)
        {
            sPrompt += "You do not have sufficient privileges to access this menu.\n";
            dlgSetPrompt(sPrompt);
            return;
        }
    }

    // Otherwise, offer menu options
    dlgAddResponseAction(RESPONSE_PAGE, "Buy Furniture");
    dlgAddResponseAction(RESPONSE_PAGE, "Sell Furniture");
    dlgAddResponseAction(RESPONSE_PAGE, "Rotate Furniture");
    dlgAddResponseAction(RESPONSE_PAGE, "Move Furniture");

    dlgSetPrompt(sPrompt);

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

void MainPageSelect( )
{
    object oPC = dlgGetSpeakingPC();
    object oDatabase = GetItemPossessedBy(oPC, PC_DATABASE);

    if(dlgIsSelectionEqualToName("Buy Furniture"))
    {
        dlgChangePage(PAGE_BUY_FURNITURE_CATEGORIES);
    }
    else if(dlgIsSelectionEqualToName("Sell Furniture"))
    {
        dlgChangePage(PAGE_SELL_FURNITURE_LIST);
    }
    else if(dlgIsSelectionEqualToName("Rotate Furniture"))
    {
        dlgChangePage(PAGE_ROTATE_FURNITURE);
    }
    else if(dlgIsSelectionEqualToName("Move Furniture"))
    {
        dlgChangePage(PAGE_MOVE_FURNITURE);
    }
}

///////////////////////////
// BUY FURNITURE SECTION //
///////////////////////////

void BuyFurnitureCategoriesPageInit()
{
    object oPC = dlgGetSpeakingPC();

    object oArea = GetArea(oPC);
    location lTargetLocation = APSStringToLocation(GetLocalString(oPC, RHS_FURNITURE_TOOL_TARGET_LOCATION));

    if(oArea != GetAreaFromLocation(lTargetLocation))
    {
        dlgEndDialog();
        return;
    }

    DisplayFurnitureCategories();

    dlgActivateResetResponse("Back", ColorTokenBlue());
}

void BuyFurnitureCategoriesPageSelect()
{
    object oPC = dlgGetSpeakingPC();
    SetLocalString(oPC, RHS_MENU_TEMP_CATEGORY_SELECTED, dlgGetSelectionName());

    dlgResetPageNumber();
    dlgChangePage(PAGE_BUY_FURNITURE_LIST);
}

void BuyFurnitureListPageInit()
{
    object oPC = dlgGetSpeakingPC();
    string sCategory = GetLocalString(oPC, RHS_MENU_TEMP_CATEGORY_SELECTED);
    int iCategoryID = StringToInt(GetMySQLDataKeyString(RHS_TABLE_FURNITURE_CATEGORIES, "ID", sCategory, "Name", RHS_SCHEMA));

    object oArea = GetArea(oPC);
    location lTargetLocation = APSStringToLocation(GetLocalString(oPC, RHS_FURNITURE_TOOL_TARGET_LOCATION));

    if(oArea != GetAreaFromLocation(lTargetLocation))
    {
        dlgEndDialog();
        return;
    }

    DisplayFurnitureList(iCategoryID);

    dlgActivateResetResponse("Main Menu", ColorTokenYellow());
}

void BuyFurnitureListPageSelect()
{
    object oPC = dlgGetSpeakingPC();
    string sSelection = dlgGetSelectionName();
    SetLocalString(oPC, RHS_MENU_TEMP_FURNITURE_SELECTED, sSelection);

    dlgResetPageNumber();
    dlgChangePage(PAGE_BUY_FURNITURE_DETAILS);
}

void BuyFurnitureDetailsPageInit()
{
    object oPC = dlgGetSpeakingPC();
    string sFurnitureName = GetLocalString(oPC, RHS_MENU_TEMP_FURNITURE_SELECTED);
    int iGoldCost = StringToInt(GetMySQLDataKeyString(RHS_TABLE_FURNITURE_TYPES, "Gold_Cost", sFurnitureName, "Name", RHS_SCHEMA));

    string sPrompt = ColorTokenGreen() + RHS_HOUSE_TERM_PROPER + " Menu\n\n" + ColorTokenEnd();
    sPrompt += ColorTokenGreen() + "Furniture Name: " + ColorTokenEnd() + sFurnitureName + "\n";
    sPrompt += ColorTokenGreen() + "Price: " + ColorTokenEnd() + IntToString(iGoldCost) + "\n";

    dlgSetPrompt(sPrompt);

    // Store the gold cost temporarily
    SetLocalInt(oPC, RHS_MENU_TEMP_FURNITURE_COST, iGoldCost);

    dlgAddResponseAction(RESPONSE_PAGE, "Buy");
    dlgAddResponseAction(RESPONSE_PAGE, "Preview");

    dlgActivateResetResponse("Main Menu", ColorTokenYellow());
}

void BuyFurnitureDetailsPageSelect()
{
    object oPC = dlgGetSpeakingPC();
    location lTargetLocation = APSStringToLocation(GetLocalString(oPC, RHS_FURNITURE_TOOL_TARGET_LOCATION));

    if(dlgIsSelectionEqualToName("Buy"))
    {
        int iGold = GetGold(oPC);
        int iCost = GetLocalInt(oPC, RHS_MENU_TEMP_FURNITURE_COST);

        // Prevents issues from occurring when a player previews and then immediately buys the furniture
        if(GetLocalInt(oPC, RHS_MENU_TEMP_PREVIEWING_FURNITURE))
        {
            FloatingTextStringOnCreature(ColorTokenRed() + "You must wait until the preview has ended to make that purchase." + ColorTokenEnd(), oPC, FALSE);
            return;
        }
        // Not enough money
        if(iGold < iCost)
        {
            FloatingTextStringOnCreature(ColorTokenRed() + "You do not have enough money to make that purchase." + ColorTokenEnd(), oPC, FALSE);
            return;
        }

        string sFurnitureName = GetLocalString(oPC, RHS_MENU_TEMP_FURNITURE_SELECTED);
        string sResref = GetMySQLDataKeyString(RHS_TABLE_FURNITURE_TYPES, "Resref", sFurnitureName, "Name", RHS_SCHEMA);

        // Buy the furniture
        AssignCommand(oPC, TakeGoldFromCreature(iCost, oPC, TRUE));
        object oFurniture = CreateObject(OBJECT_TYPE_PLACEABLE, sResref, lTargetLocation);
        RHS_AddFurniture(oPC, oFurniture);

        FloatingTextStringOnCreature(ColorTokenGreen() + "Furniture purchase complete!" + ColorTokenEnd(), oPC, FALSE);
        dlgEndDialog();
    }
    else if(dlgIsSelectionEqualToName("Preview"))
    {
        // There's already a preview in effect - prevent another from occurring
        if(GetLocalInt(oPC, RHS_MENU_TEMP_PREVIEWING_FURNITURE))
        {
            return;
        }
        string sFurnitureName = GetLocalString(oPC, RHS_MENU_TEMP_FURNITURE_SELECTED);
        string sResref = GetMySQLDataKeyString(RHS_TABLE_FURNITURE_TYPES, "Resref", sFurnitureName, "Name", RHS_SCHEMA);
        object oPreview = CreateObject(OBJECT_TYPE_PLACEABLE, sResref, lTargetLocation);
        SetPlotFlag(oPreview, TRUE);
        SetUseableFlag(oPreview, FALSE);

        // Mark player as currently previewing a furniture and remove the boolean after the preview is over
        SetLocalInt(oPC, RHS_MENU_TEMP_PREVIEWING_FURNITURE, TRUE);
        DelayCommand(RHS_FURNITURE_PREVIEW_LENGTH, DeleteLocalInt(oPC, RHS_MENU_TEMP_PREVIEWING_FURNITURE));
        // Destroy the preview object after time has passed.
        RHS_ClearInventory(oPreview);
        DestroyObject(oPreview, RHS_FURNITURE_PREVIEW_LENGTH);
    }
}

////////////////////////////
// SELL FURNITURE SECTION //
////////////////////////////

void SellFurniturePageInit()
{
    object oPC = dlgGetSpeakingPC();

    object oArea = GetArea(oPC);
    location lTargetLocation = APSStringToLocation(GetLocalString(oPC, RHS_FURNITURE_TOOL_TARGET_LOCATION));

    if(oArea != GetAreaFromLocation(lTargetLocation))
    {
        dlgEndDialog();
        return;
    }

    string sPrompt = ColorTokenGreen() + RHS_HOUSE_TERM_PROPER + " Menu\n\n" + ColorTokenEnd();
    sPrompt += "Please select the object you wish to sell. Objects listed are in order of distance from where you targeted with your furniture tool.\n\n";

    // Show all furniture within range
    DisplayNearestFurnitureList(lTargetLocation);

    dlgActivateResetResponse("Back", ColorTokenBlue());
}

void SellFurniturePageSelect()
{
    object oPC = dlgGetSpeakingPC();
    location lTargetLocation = APSStringToLocation(GetLocalString(oPC, RHS_FURNITURE_TOOL_TARGET_LOCATION));
    int iSelection = dlgGetSelectionIndex() + 1;
    object oFurniture = GetNearestObjectToLocation(OBJECT_TYPE_PLACEABLE, lTargetLocation, iSelection);

    SetLocalObject(oPC, RHS_MENU_TEMP_FURNITURE_OBJECT, oFurniture);

    dlgChangePage(PAGE_SELL_FURNITURE_DETAILS);
}

void SellFurnitureDetailsPageInit()
{
    object oPC = dlgGetSpeakingPC();
    object oFurniture = GetLocalObject(oPC, RHS_MENU_TEMP_FURNITURE_OBJECT);
    string sResref = GetResRef(oFurniture);
    int iFurnitureID = GetLocalInt(oFurniture, RHS_FURNITURE_ID_NUMBER);
    int iSellPrice = StringToInt(GetMySQLDataKeyString(RHS_TABLE_FURNITURE_TYPES, "Gold_Cost", sResref, "Resref", RHS_SCHEMA));

    iSellPrice = FloatToInt(iSellPrice * RHS_FURNITURE_SELL_BACK_PERCENTAGE);

    string sPrompt = ColorTokenGreen() + RHS_HOUSE_TERM_PROPER + " Menu\n\n" + ColorTokenEnd();
    sPrompt += ColorTokenGreen() + "Object: " + ColorTokenEnd() + GetName(oFurniture) + "\n\n";
    sPrompt += "You will receive " + IntToString(iSellPrice) + " GP for selling this object.\n";

    dlgSetPrompt(sPrompt);

    SetLocalInt(oPC, RHS_MENU_TEMP_FURNITURE_COST, iSellPrice);

    dlgAddResponseAction(RESPONSE_PAGE, "Sell Object");

    dlgActivateResetResponse("Back", ColorTokenBlue());
}

void SellFurnitureDetailsPageSelect()
{
    object oPC = dlgGetSpeakingPC();
    object oFurniture = GetLocalObject(oPC, RHS_MENU_TEMP_FURNITURE_OBJECT);
    object oArea = GetArea(oPC);
    int iFurnitureID = GetLocalInt(oFurniture, RHS_FURNITURE_ID_NUMBER);
    int iSellPrice = GetLocalInt(oPC, RHS_MENU_TEMP_FURNITURE_COST);
    int iOwnerID = GetLocalInt(oArea, RHS_HOUSE_OWNER_ID_NUMBER);

    if(dlgIsSelectionEqualToName("Sell Object"))
    {
        // Furniture is no longer valid - return to main page
        if(!GetIsObjectValid(oFurniture))
        {
            dlgChangePage(PAGE_MAIN);
        }
        // Sell the furniture
        else
        {
            string sTable = RHS_SCHEMA + "." + RHS_TABLE_FURNITURE + IntToString(iOwnerID);
            string sSQL = "DELETE FROM " + sTable + " WHERE ID=" + IntToString(iFurnitureID);
            SQLExecDirect(sSQL);
            DestroyObject(oFurniture);

            AssignCommand(oPC, GiveGoldToCreature(oPC, iSellPrice));
            dlgChangePage(PAGE_MAIN);
        }
    }
}

//////////////////////////////
// ROTATE FURNITURE SECTION //
//////////////////////////////

void RotateFurniturePageInit()
{
    object oPC = dlgGetSpeakingPC();
    location lTargetLocation = APSStringToLocation(GetLocalString(oPC, RHS_FURNITURE_TOOL_TARGET_LOCATION));

    object oArea = GetArea(oPC);

    if(oArea != GetAreaFromLocation(lTargetLocation))
    {
        dlgEndDialog();
        return;
    }

    string sMessage = ColorTokenGreen() + RHS_HOUSE_TERM_PROPER + " Menu\n\n" + ColorTokenEnd();
    sMessage += "Please select a furniture object to rotate. Objects listed are in order of distance from where you targeted with your furniture tool.\n";
    dlgSetPrompt(sMessage);

    // Show all furniture within range
    DisplayNearestFurnitureList(lTargetLocation);

    dlgActivateResetResponse("Back", ColorTokenBlue());
}

void RotateFurniturePageSelect()
{
    object oPC = dlgGetSpeakingPC();
    location lTargetLocation = APSStringToLocation(GetLocalString(oPC, RHS_FURNITURE_TOOL_TARGET_LOCATION));
    int iSelection = dlgGetSelectionIndex() + 1;
    object oFurniture = GetNearestObjectToLocation(OBJECT_TYPE_PLACEABLE, lTargetLocation, iSelection);

    SetLocalObject(oPC, RHS_MENU_TEMP_FURNITURE_OBJECT, oFurniture);

    dlgChangePage(PAGE_ROTATE_FURNITURE_DETAILS);
}


void RotateFurnitureDetailsPageInit()
{
    object oPC = dlgGetSpeakingPC();
    object oFurniture = GetLocalObject(oPC, RHS_MENU_TEMP_FURNITURE_OBJECT);
    int iFurnitureID = GetLocalInt(oFurniture, RHS_FURNITURE_ID_NUMBER);

    float fFacing = GetFacing(oFurniture);
    string sPrompt = ColorTokenGreen() + RHS_HOUSE_TERM_PROPER + " Menu\n\n" + ColorTokenEnd();
    sPrompt += ColorTokenGreen() + "Furniture Selected: " + ColorTokenEnd() + GetName(oFurniture) + "\n";
    sPrompt += ColorTokenGreen() + "Current Direction: " + ColorTokenEnd() + FloatToString(fFacing, 3, 2) + "�\n\n";
    sPrompt += "You can adjust the direction a piece of furniture faces here.";

    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: North");
    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: South");
    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: East");
    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: West");
    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: +1�");
    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: +20�");
    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: +30�");
    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: +45�");
    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: +60�");
    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: +90�");
    dlgAddResponseAction(RESPONSE_PAGE, "Set Direction: +180�");
}

void RotateFurnitureDetailsPageSelect()
{
    object oPC = dlgGetSpeakingPC();
    object oArea = GetArea(oPC);
    object oFurniture = GetLocalObject(oPC, RHS_MENU_TEMP_FURNITURE_OBJECT);
    float fFacing = GetFacing(oFurniture);
    int iFurnitureID = GetLocalInt(oFurniture, RHS_FURNITURE_ID_NUMBER);
    int iOwnerID = GetLocalInt(oArea, RHS_HOUSE_OWNER_ID_NUMBER);
    string sTable = RHS_TABLE_FURNITURE + IntToString(iOwnerID);

    if(dlgIsSelectionEqualToName("Set Direction: North"))
    {
        fFacing = 0.0;
    }
    else if(dlgIsSelectionEqualToName("Set Direction: South"))
    {
        fFacing = 180.0;
    }
    else if(dlgIsSelectionEqualToName("Set Direction: East"))
    {
        fFacing = 90.0;
    }
    else if(dlgIsSelectionEqualToName("Set Direction: West"))
    {
        fFacing = 270.0;
    }
    else if(dlgIsSelectionEqualToName("Set Direction: +1�"))
    {
        fFacing += 361;  // Facing won't update unless more than 20 is changed.
    }
    else if(dlgIsSelectionEqualToName("Set Direction: +20�"))
    {
        fFacing += 20;
    }
    else if(dlgIsSelectionEqualToName("Set Direction: +30�"))
    {
        fFacing += 30;
    }
    else if(dlgIsSelectionEqualToName("Set Direction: +45�"))
    {
        fFacing += 45;
    }
    else if(dlgIsSelectionEqualToName("Set Direction: +60�"))
    {
        fFacing += 60;
    }
    else if(dlgIsSelectionEqualToName("Set Direction: +90�"))
    {
        fFacing += 90;
    }
    else if(dlgIsSelectionEqualToName("Set Direction: +180�"))
    {
        fFacing += 180;
    }

    // Update the facing
    AssignCommand(oFurniture, SetFacing(fFacing));
    SetMySQLData(sTable, "Facing", iFurnitureID, FloatToString(fFacing), "ID", RHS_SCHEMA);
}

////////////////////////////
// MOVE FURNITURE SECTION //
////////////////////////////

void MoveFurniturePageInit()
{
    object oPC = dlgGetSpeakingPC();
    location lTargetLocation = APSStringToLocation(GetLocalString(oPC, RHS_FURNITURE_TOOL_TARGET_LOCATION));

    object oArea = GetArea(oPC);

    if(oArea != GetAreaFromLocation(lTargetLocation))
    {
        dlgEndDialog();
        return;
    }

    string sMessage = ColorTokenGreen() + RHS_HOUSE_TERM_PROPER + " Menu\n\n" + ColorTokenEnd();
    sMessage += "Please select a furniture object to move. Objects listed are in order of distance from where you targeted with your furniture tool.\n";
    dlgSetPrompt(sMessage);

    // Show all furniture within range
    DisplayNearestFurnitureList(lTargetLocation);

    dlgActivateResetResponse("Back", ColorTokenBlue());
}

void MoveFurniturePageSelect()
{
    object oPC = dlgGetSpeakingPC();
    location lTargetLocation = APSStringToLocation(GetLocalString(oPC, RHS_FURNITURE_TOOL_TARGET_LOCATION));
    int iSelection = dlgGetSelectionIndex() + 1;
    object oFurniture = GetNearestObjectToLocation(OBJECT_TYPE_PLACEABLE, lTargetLocation, iSelection);

    SetLocalObject(oPC, RHS_FURNITURE_MOVING_OBJECT_REFERENCE, oFurniture);
    SetLocalInt(oPC, RHS_FURNITURE_MOVING_OBJECT, TRUE);

    dlgEndDialog();

    FloatingTextStringOnCreature(ColorTokenGreen() + "Use your furniture tool again to select the new location of the furniture." + ColorTokenEnd(), oPC, FALSE);
}