//::///////////////////////////////////////////////
//:: Scarface's Persistent Banking
//:: sfpb_open
//:://////////////////////////////////////////////
/*
    Written By Scarface
    Modified By AmanShadar
*/
//////////////////////////////////////////////////

#include "sfpb_config"
#include "x4_inc_functions"
void main()
{
    // Vars
    object oPC = GetLastOpenedBy();
    object oChest = OBJECT_SELF;
    location lLoc = GetLocation(oPC);
    string sID = SF_GetPlayerID(oPC);
    string sUserID = GetLocalString(oChest, "USER_ID");
    string sModName = CharacterDB(oPC);
    //variables added for container support
    int iBagID;
    object oBagItem;
    object oNewItem; //used for bag copied back into chest
    object oNewBagItem;//used for items copied back into bag

    // End script if any of these conditions are met
    if (!GetIsPC(oPC) ||
         GetIsDM(oPC) ||
         GetIsDMPossessed(oPC) ||
         GetIsPossessedFamiliar(oPC)) return;

    // If the chest is already in use then this must be a thief
    if (sUserID != "" && sUserID != sID) return;

    // Set the players ID as a local string onto the chest
    // for anti theft purposes
    SetLocalString(oChest, "USER_ID", sID);

    // Get the player's storer NPC from the database
    object oStorer = RetrieveCampaignObject(sModName, DATABASE_ITEM + sID, lLoc);
    DeleteCampaignVariable(sModName, DATABASE_ITEM + sID);


    int iGold = GetCampaignInt(sModName, "GOLD_IN_LOCKER");
    CreateItemOnObject("NW_IT_GOLD001", oChest, iGold);
    // loop through the NPC storers inventory and copy the items
    // into the chest.
    object oItem = GetFirstItemInInventory(oStorer);

    //First find the bags, copy them and their contents into the chest
    while (GetIsObjectValid(oItem))
    {
        if (GetHasInventory(oItem))
        {
            //Get the ID for it's items
            iBagID = GetLocalInt(oItem, "BagID");

            oBagItem = GetFirstItemInInventory(oItem);
            while(GetIsObjectValid(oBagItem))
            {
                //Set up items with matching bag ID to not be copied, as they will be
                //when the bag itself gets copied
                if(GetLocalInt(oBagItem, "BagID")==iBagID && !GetHasInventory(oBagItem))
                {
                    // item in the bag got copied with the bag
                    SetLocalInt(oBagItem, "copied", 1);//set to copied
                    DeleteLocalInt(oItem, "BagID");  //don't need this anymore
                }

                // Next item
                oBagItem = GetNextItemInInventory(oItem);
            }
            //copy the bag itself (items inside come with)
            CopyObject(oItem, GetLocation(oChest), oChest);
        }
        oItem = GetNextItemInInventory(oStorer);
    }

    oItem = GetFirstItemInInventory(oStorer);
    //Copy the items into the chest
    int iCopy;
    while (GetIsObjectValid(oItem))
    {
        // Copy the item into the chest if not already copied
        // containers and their inventories have already been copied,
        // so ignore them
        if(!GetHasInventory(oItem))
        {
            iCopy = GetLocalInt(oItem, "copied");
            if(iCopy != 1)
            {
                oNewItem = CopyItem(oItem, oChest, TRUE);
            }
        }
        DeleteLocalInt(oNewItem, "copied");  //don't need these anymore
        DeleteLocalInt(oNewItem, "BagID");

        // Destroy the original
        DestroyObject(oItem);

        // Next item
        oItem = GetNextItemInInventory(oStorer);
    }

    // Destroy the NPC storer
    DestroyObject(oStorer);
}