#include "fcb_inc_prot"

//==========================================/
//general
//==========================================/

//==========================================/
void select_working_item(object item)
//==========================================/
    {
    //--------------------------------------/
    //make blank copy of item
    SetIdentified(item, 1);
    SetLocalObject(FCB_HOST, ITEM_WORKING_SEL, item);

    DestroyObject(GetLocalObject(FCB_HOST, ITEM_WORKING_SEL_BLANK));
    object item_copy = copy_item(item, FCB_INV);
    iprp_remove_all(item_copy);

    SetLocalObject(FCB_HOST, ITEM_WORKING_SEL_BLANK, item_copy);

    //--------------------------------------/
    //filter iprp list
    ui_iprp_list_filter(item);
    //create iprp remove list
    ui_item_header(item);

    initialize_iprp_list(item);
    }

//==========================================/
object get_working_item()
//==========================================/
    {
    return GetLocalObject(FCB_HOST, ITEM_WORKING_SEL);
    }

//==========================================/
object set_working_item_name(string name)
//==========================================/
    {
    object working_item = get_working_item();
    SetName(working_item, name);

    return working_item;
    }

//==========================================/
//iprp related
//==========================================/

//==========================================/
string ui_iprp_display(itemproperty iprp)
//==========================================/
    {
    string iprp_string = "";
    string paramtable_row = "";

    //----------------------------------------//
    int itempropdef_row = GetItemPropertyType(iprp);

    string game_strref = Get2DAString("itempropdef", "GameStrRef", itempropdef_row);
    if(game_strref != "")
        {
        iprp_string += GetStringByStrRef(StringToInt(game_strref));
        }

    //----------------------------------------//
    int subtype_row = GetItemPropertySubType(iprp);
    string subtype_resref = Get2DAString("itempropdef", "SubTypeResRef", itempropdef_row);
    if(subtype_resref != "")
        {
        string subtype_strref = Get2DAString(subtype_resref, "Name", subtype_row);

        if(subtype_strref != "")
            {
            iprp_string += UI_TXT_SPACE + GetStringByStrRef(StringToInt(subtype_strref));
            }

        //param1 override
        //----------------------------------------//
        paramtable_row = Get2DAString(subtype_resref, "Param1ResRef", subtype_row);
        //----------------------------------------//
        }

    //----------------------------------------//
    //string costtable_row = Get2DAString("itempropdef", "CostTableResRef", itempropdef_row);
    int costtable_row = GetItemPropertyCostTable(iprp);
    if(costtable_row > 0)
        {
        string costtable_resref = Get2DAString("IPRP_COSTTABLE", "Name", costtable_row);
        string costtable_strref = Get2DAString(costtable_resref, "Name", GetItemPropertyCostTableValue(iprp));

        if(costtable_strref != "")
            {
            iprp_string += UI_TXT_SPACE + GetStringByStrRef(StringToInt(costtable_strref));
            }
        }

    //----------------------------------------//
    if(paramtable_row == "")
        {
        paramtable_row = Get2DAString("itempropdef", "Param1ResRef", itempropdef_row);
        }

    if(paramtable_row != "")
        {
        string param1_resref = Get2DAString("IPRP_PARAMTABLE", "TableResRef", StringToInt(paramtable_row));
        string param1_strref = Get2DAString(param1_resref, "Name", GetItemPropertyParam1Value(iprp));

        if(param1_strref != "")
            {
            iprp_string += UI_TXT_SPACE + GetStringByStrRef(StringToInt(param1_strref));
            }
        }
    return iprp_string;
    }

//==========================================/
string ui_item_stack_disp(object item)
//==========================================/
    {
    return UI_TXT_BRACKET2_START + UI_TXT_UNIT_STACK + IntToString(GetItemStackSize(item)) + UI_TXT_BRACKET2_END;
    }

//==========================================/
string ui_iprp_list(object item)
//==========================================/
    {
    string iprp_list = ui_item_header(item);

    itemproperty iprp = GetFirstItemProperty(item);
    while(GetIsItemPropertyValid(iprp))
        {
        iprp_list += UI_TXT_INDENT + ui_iprp_display(iprp);

        iprp = GetNextItemProperty(item);
        }

    return iprp_list;
    }

//==========================================/
string ui_item_header(object item, int item_cost = -1)
//==========================================/
    {
    string name = GetName(item);
    string name_original = GetName(item, 1);

    if(name != name_original)
        {
        name += c_hid(UI_TXT_SPACE + UI_TXT_BRACKET2_START + name_original + UI_TXT_BRACKET2_END);
        }

    name += UI_TXT_SPACE + c_hlt1(ui_item_stack_disp(item));

    string baseitem_type = GetStringByStrRef(StringToInt(Get2DAString("baseitems", "Name", GetBaseItemType(item))));

    string item_header = UI_TXT_HEADER_BASEITEM + baseitem_type;
    item_header += UI_TXT_INDENT + UI_TXT_HEADER_CHARGE + IntToString(GetItemCharges(item));

    //--------------------------------------/
    if(item_cost < 0)
        {
        item_cost = get_item_cost(item);
        }
    item_header += UI_TXT_INDENT + UI_TXT_HEADER_COST + IntToString(item_cost) + UI_TXT_UNIT_GP + UI_TXT_SPACE;

    int item_level = get_item_level_by_cost(item_cost);
    if(item_level > get_character_level(GetLocalObject(FCB_HOST, USER)))
        {
        item_header += UI_TXT_BRACKET2_START + c_crs(UI_TXT_UNIT_LV + IntToString(item_level)) + UI_TXT_BRACKET2_END;
        }
    else
        {
        item_header += UI_TXT_BRACKET2_START + UI_TXT_UNIT_LV + IntToString(item_level) + UI_TXT_BRACKET2_END;
        }

    //--------------------------------------/
    item_header = name + UI_TXT_INDENT + c_hlt1(item_header);

    SetLocalString(FCB_HOST, ITEM_HEADER_TXT, item_header);
    ui_disp_set_txt(DISP_INDEX_HEADER, UI_TXT_ITEM_WORKING_CURRENT, item_header);

    return item_header;
    }

//==========================================/
string initialize_iprp_list(object item)
//==========================================/
    {
    clear_main_folder(MAIN_FOLDER_INDEX_IPRPR);
    int file_type_menu = create_main_file_type_link(SCRIPT_INDEX_IPRPR_SEL_0 | MAIN_FOLDER_INDEX_IPRPR_MENU);

    string iprp_list = "";

    itemproperty iprp = GetFirstItemProperty(item);
    if(GetIsItemPropertyValid(iprp))
        {
        string iprp_txt = ui_iprp_display(iprp);

        create_main_file_int(MAIN_FOLDER_INDEX_IPRPR, iprp_txt, file_type_menu, iprp_to_param(iprp));
        iprp_list = ui_iprp_display(iprp);
        }

    iprp = GetNextItemProperty(item);
    while(GetIsItemPropertyValid(iprp))
        {
        string iprp_txt = ui_iprp_display(iprp);

        create_main_file_int(MAIN_FOLDER_INDEX_IPRPR, iprp_txt, file_type_menu, iprp_to_param(iprp));
        iprp_list += UI_TXT_INDENT + iprp_txt;

        iprp = GetNextItemProperty(item);
        }

    SetLocalString(FCB_HOST, ITEM_SEL_TXT, iprp_list);
    ui_disp_set_txt(DISP_INDEX_IPRP, UI_TXT_IPRP_CURRENT, iprp_list);

    return iprp_list;
    }

//==========================================/
string iprp_addto(object item, itemproperty iprp_add)
//==========================================/
    {
    int file_type_menu = create_main_file_type_link(SCRIPT_INDEX_IPRPR_SEL_0 | MAIN_FOLDER_INDEX_IPRPR_MENU);

    AddItemProperty(DURATION_TYPE_PERMANENT, iprp_add, item);
    string iprp_txt = ui_iprp_display(iprp_add);

    string item_header = ui_item_header(item);
    string iprp_list = GetLocalString(FCB_HOST, ITEM_SEL_TXT);
    if(iprp_list != "")
        {
        iprp_list += UI_TXT_INDENT + iprp_txt;
        }
    else
        {
        iprp_list = iprp_txt;
        }

    SetLocalString(FCB_HOST, ITEM_SEL_TXT, iprp_list);

    create_main_file_int(MAIN_FOLDER_INDEX_IPRPR, iprp_txt, file_type_menu, iprp_to_param(iprp_add));

    ui_disp_set_txt(DISP_INDEX_HEADER, UI_TXT_ITEM_WORKING_CURRENT, item_header);
    ui_disp_set_txt(DISP_INDEX_IPRP, UI_TXT_IPRP_CURRENT, iprp_list);

    return iprp_list;
    }

//==========================================/
void iprp_remove_all(object item)
//==========================================/
    {
    itemproperty iprp = GetFirstItemProperty(item);
    while(GetIsItemPropertyValid(iprp))
        {
        RemoveItemProperty(item, iprp);

        iprp = GetNextItemProperty(item);
        }
    }

//::::::::::::::::::::::::::::::::::::::::::/
string get_item_type_2da_column(object item)
//::::::::::::::::::::::::::::::::::::::::::/
    {
    if(GetIsObjectValid(item))
        {
        int base_item_type = GetBaseItemType(item);
        string column_strref = Get2DAString("baseitems", "PropColumn", base_item_type);

        if(column_strref != "")
            {
            switch(StringToInt(column_strref))
                {
                case 0:  return "0_Melee";
                case 1:  return "1_Ranged";
                case 2:  return "2_Thrown";
                case 3:  return "3_Staves";
                case 4:  return "4_Rods";
                case 5:  return "5_Ammo";
                case 6:  return "6_Arm_Shld";
                case 7:  return "7_Helm";
                case 8:  return "8_Potions";
                case 9:  return "9_Scrolls";
                case 10: return "10_Wands";
                case 11: return "11_Thieves";
                case 12: return "12_TrapKits";
                case 13: return "13_Hide";
                case 14: return "14_Claw";
                case 15: return "15_Misc_Uneq";
                case 16: return "16_Misc";
                case 17: return "17_No_Props";
                case 18: return "18_Containers";
                case 19: return "19_HealerKit";
                case 20: return "20_Torch";
                case 21: return "21_Glove";
                }
            }
        }
    return "";
    }

//==========================================/
void ui_iprp_list_filter(object item)
//==========================================/
    {
    string column = get_item_type_2da_column(item);

    int file_dir = MAIN_FOLDER_INDEX_IPRP + UI_MAIN_DEFAULT_OFFSET;
    int file_dir_max = GetLocalInt(FCB_HOST, MAIN_FILE_INDEX_ + IntToHexString(FILTER_F10 & file_dir));

    while(file_dir <= file_dir_max)
        {
        int file_param = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir));

        if(Get2DAString("itemprops", column, file_param) != "")
            {
            ui_main_set_hidden(file_dir, 0);
            }
        else
            {
            ui_main_set_hidden(file_dir, 1);
            }

        file_dir++;
        }
    }
