#include "fcb_inc_vars"
#include "fcb_inc_cvgen"
#include "fcb_inc_cvdisp"
#include "fcb_inc_general"
#include "fcb_inc_typespec"

//==========================================/
void main()
//==========================================/
    {
    int script_param = GetLocalInt(FCB_HOST, SCRIPT_PARAM);
    int stack_index_sel = FILTER_F1 & script_param;
    int stack_param = GetLocalInt(FCB_HOST, STACK_PARAM_ + IntToHexString(stack_index_sel));
    object user = GetLocalObject(FCB_HOST, USER);

    //--------------------------------------/
    switch(FILTER_F100 & script_param)
        {
        //--------------------------------------/
        case SCRIPT_INDEX_IPRP_SEL_0:
            {
            int folder_index_iprp = MAIN_FOLDER_INDEX_IPRP;

            int param0 = 0; //main
            int param1 = 0; //subtype
            int param2 = 0; //cost
            int param3 = 0; //param1

            string iprp_txt = "";

            int file_dir_0 = FILTER_F11 & GetLocalInt(FCB_HOST, STACK_PARAM_ + IntToHexString(stack_index_sel));

            //--------------------------------------/
            if((FILTER_F10 & file_dir_0) == folder_index_iprp)
                {
                param0 = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir_0));

                //--------------------------------------/
                iprp_txt = GetStringByStrRef(StringToInt(Get2DAString("itempropdef", "GameStrRef", param0)));
                //--------------------------------------/
                }
            else
                {
                int file_dir_1 = FILTER_F11 & GetLocalInt(FCB_HOST, STACK_PARAM_ + IntToHexString(stack_index_sel-1));

                if((FILTER_F10 & file_dir_1) == folder_index_iprp)
                    {
                    param0 = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir_1));
                    param1 = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir_0));

                    //--------------------------------------/
                    iprp_txt = GetStringByStrRef(StringToInt(Get2DAString("itempropdef", "GameStrRef", param0)));

                    if(file_dir_0 > 0)
                        {
                        iprp_txt += UI_TXT_SPACE + GetLocalString(FCB_HOST, MAIN_FILE_NAME_ + IntToHexString(file_dir_0));
                        }
                    //--------------------------------------/
                    }
                else
                    {
                    int file_dir_2 = FILTER_F11 & GetLocalInt(FCB_HOST, STACK_PARAM_ + IntToHexString(stack_index_sel-2));

                    if((FILTER_F10 & file_dir_2) == folder_index_iprp)
                        {
                        param0 = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir_2));
                        param1 = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir_1));
                        param2 = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir_0));

                        //--------------------------------------/
                        iprp_txt = GetStringByStrRef(StringToInt(Get2DAString("itempropdef", "GameStrRef", param0)));

                        if(file_dir_1 > 0)
                            {
                            iprp_txt += UI_TXT_SPACE + GetLocalString(FCB_HOST, MAIN_FILE_NAME_ + IntToHexString(file_dir_1));
                            }
                        if(file_dir_0 > 0)
                            {
                            iprp_txt += UI_TXT_SPACE + GetLocalString(FCB_HOST, MAIN_FILE_NAME_ + IntToHexString(file_dir_0));
                            }
                        //--------------------------------------/
                        }
                    else
                        {
                        int file_dir_3 = FILTER_F11 & GetLocalInt(FCB_HOST, STACK_PARAM_ + IntToHexString(stack_index_sel-3));

                        if((FILTER_F10 & file_dir_3) == folder_index_iprp)
                            {
                            param0 = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir_3));
                            param1 = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir_2));
                            param2 = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir_1));
                            param3 = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir_0));

                            //--------------------------------------/
                            iprp_txt = GetStringByStrRef(StringToInt(Get2DAString("itempropdef", "GameStrRef", param0)));

                            if(file_dir_2 > 0)
                                {
                                iprp_txt += UI_TXT_SPACE + GetLocalString(FCB_HOST, MAIN_FILE_NAME_ + IntToHexString(file_dir_2));
                                }
                            if(file_dir_1 > 0)
                                {
                                iprp_txt += UI_TXT_SPACE + GetLocalString(FCB_HOST, MAIN_FILE_NAME_ + IntToHexString(file_dir_1));
                                }
                            if(file_dir_0 > 0)
                                {
                                iprp_txt += UI_TXT_SPACE + GetLocalString(FCB_HOST, MAIN_FILE_NAME_ + IntToHexString(file_dir_0));
                                }
                            //--------------------------------------/
                            }
                        }
                    }
                }

            SetLocalString(FCB_HOST, IPRP_SEL_TXT, iprp_txt);

            object item = get_working_item();
            object item_copy = copy_item(item, FCB_INV);

            itemproperty iprp = convert_iprp(param0, param1, param2, param3);
            AddItemProperty(DURATION_TYPE_PERMANENT, iprp, item_copy);

            //--------------------------------------/
            //check cost and ILR restriction
            int mod_enable = 1;

            //--------------------------------------/
            int cost = get_item_cost(item_copy) - get_item_cost(item);
            int user_gold = GetGold(user);

            string cost_disp = IntToString(cost) + UI_TXT_UNIT_GP;
            if(cost > user_gold)
                {
                cost_disp = c_crs(cost_disp + UI_TXT_INDENT + UI_TXT_COST_OUTOFBOUND);

                mod_enable = 0;
                }
            else
                {
                if(cost <= 0)
                    {
                    cost_disp = c_hlt2(cost_disp);
                    }
                }

            //--------------------------------------/
            int item_level = get_item_level(item);
            int item_copy_level = get_item_level(item_copy);
            int user_level = get_character_level(user);

            string item_copy_level_disp = IntToString(item_copy_level);
            if(GetLocalInt(FCB_HOST, SW_ILR_ENABLE)
                && item_copy_level > user_level
                && cost > 0)
                {
                item_copy_level_disp = c_crs(item_copy_level_disp + UI_TXT_INDENT + UI_TXT_LEVEL_OUTOFBOUND);

                mod_enable = 0;
                }

            //--------------------------------------/
            if(mod_enable)
                {
                SetLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST, cost);

                SetLocalInt(FCB_HOST, IPRP_PARAM_SEL_0_1, (param0 << BITLEN_FILE_1) | param1);
                SetLocalInt(FCB_HOST, IPRP_PARAM_SEL_2_3, (param2 << BITLEN_FILE_1) | param3);

                ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_IPRP_MENU | 1, 0);
                }
            else
                {
                ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_IPRP_MENU | 1, 1);
                }

            //--------------------------------------/
            DestroyObject(item_copy);

            //--------------------------------------/
            iprp_txt += UI_TXT_INDENT + c_hdr1(UI_TXT_COST) + UI_TXT_SPACE + cost_disp + UI_TXT_INDENT + c_hdr1(UI_TXT_LEVEL) + UI_TXT_SPACE + item_copy_level_disp;
            /////

            //--------------------------------------/
            ui_disp_set_txt(DISP_INDEX_LOCAL1, "", c_hdr1(UI_TXT_IPRP_SEL) + UI_TXT_SPACE + iprp_txt);
            ui_disp_state_new(DISP_INDEX_LOCAL1, 1);
            //--------------------------------------/

            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_IPRP_SEL_1:
            {
            //charge player
            //--------------------------------------/
            int cost = GetLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST);
            DeleteLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST);

            if(give_gold(-cost, user))
                {
                //--------------------------------------/
                int param0_1 = GetLocalInt(FCB_HOST, IPRP_PARAM_SEL_0_1);
                int param2_3 = GetLocalInt(FCB_HOST, IPRP_PARAM_SEL_2_3);
                itemproperty iprp = convert_iprp(param0_1 >>> BITLEN_FILE_1, FILTER_F1 & param0_1, param2_3 >>> BITLEN_FILE_1, FILTER_F1 & param2_3);

                if(GetIsItemPropertyValid(iprp))
                    {
                    object item = get_working_item();

                    iprp_addto(item, iprp);

                    //--------------------------------------/
                    ui_disp_clear_local();
                    }
                }

            ui_stack_index(1);
            visual_effect();

            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_IPRPR_SEL_0:
            {
            int stack_index_sel = FILTER_F1 & GetLocalInt(FCB_HOST, STACK_STATE);
            int file_dir_sel = FILTER_F11 & stack_param;
            string file_name = GetLocalString(FCB_HOST, MAIN_FILE_NAME_ + IntToHexString(file_dir_sel));

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

            int file_dir_max = GetLocalInt(FCB_HOST, MAIN_FILE_INDEX_ + IntToHexString(FILTER_F10 & file_dir_sel));

            int file_dir = (FILTER_F10 & file_dir_sel) | UI_MAIN_DEFAULT_OFFSET;
            while(file_dir <= file_dir_max)
                {
                if(file_dir != file_dir_sel)
                    {
                    int file_param = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(file_dir));
                    AddItemProperty(DURATION_TYPE_PERMANENT, param_to_iprp(file_param), item_copy);
                    }

                file_dir++;
                }

            //--------------------------------------/
            //check cost and ILR restriction
            int mod_enable = 1;

            //--------------------------------------/
            int cost = get_item_cost(item_copy) - get_item_cost(item);
            int user_gold = GetGold(user);

            string cost_disp = IntToString(cost) + UI_TXT_UNIT_GP;
            if(cost > user_gold)
                {
                cost_disp = c_crs(cost_disp + UI_TXT_INDENT + UI_TXT_COST_OUTOFBOUND);

                mod_enable = 0;
                }
            else
                {
                if(cost <= 0)
                    {
                    cost_disp = c_hlt2(cost_disp);
                    }
                }

            //--------------------------------------/
            int item_level = get_item_level(item);
            int item_copy_level = get_item_level(item_copy);
            int user_level = get_character_level(user);

            string item_copy_level_disp = IntToString(item_copy_level);
            if(GetLocalInt(FCB_HOST, SW_ILR_ENABLE)
                && item_copy_level > user_level
                && cost > 0)
                {
                item_copy_level_disp = c_crs(item_copy_level_disp + UI_TXT_INDENT + UI_TXT_LEVEL_OUTOFBOUND);

                mod_enable = 0;
                }

            //--------------------------------------/
            if(mod_enable)
                {
                SetLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST, cost);

                ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_IPRPR_MENU | 1, 0);
                }
            else
                {
                ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_IPRPR_MENU | 1, 1);
                }

            //--------------------------------------/
            file_name += UI_TXT_INDENT + c_hdr1(UI_TXT_COST) + UI_TXT_SPACE + cost_disp + UI_TXT_INDENT + c_hdr1(UI_TXT_LEVEL) + UI_TXT_SPACE + item_copy_level_disp;
            /////

            //----------------------------------------//
            ui_disp_set_txt(DISP_INDEX_LOCAL1, "", c_hdr1(UI_TXT_IPRPR_SEL) + UI_TXT_SPACE + file_name);
            ui_disp_state_new(DISP_INDEX_LOCAL1, 1);

            //--------------------------------------/

            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_IPRPR_SEL_1:
            {
            //charge player
            //--------------------------------------/
            int cost = GetLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST);

            //stack: [...][offset: 10][folder_index: 10][selected: 10]
            int stack_param_1 = GetLocalInt(FCB_HOST, STACK_PARAM_ + IntToHexString(stack_index_sel-1));

            if(give_gold(-cost, user))
                {
                //--------------------------------------/
                object item = get_working_item();

                int item_value = cost + get_item_cost(item);

                //--------------------------------------/
                //set currently selected to 0
                SetLocalInt(FCB_HOST, STACK_PARAM_ + IntToHexString(stack_index_sel-1), (FILTER_F110 & stack_param_1));

                //--------------------------------------/
                int file_dir_sel = FILTER_F11 & stack_param_1;
                int file_index_sel = FILTER_F1 & stack_param_1;
                int file_index_offset = stack_param_1 >>> BITLEN_FILE_2;

                int folder_index = FILTER_F10 & stack_param_1;

                int file_index_start = FILTER_F1 & stack_param_1;
                int file_type_menu = create_main_file_type_link(SCRIPT_INDEX_IPRPR_SEL_0 | MAIN_FOLDER_INDEX_IPRPR_MENU);

                //clear for new entry
                //--------------------------------------/
                clear_main_folder(file_dir_sel - 1);

                int n = 1;
                int m = 1;

                string iprp_list = "";

                itemproperty iprp = GetFirstItemProperty(item);
                while(GetIsItemPropertyValid(iprp))
                    {
                    if(n != file_index_sel)
                        {
                        string iprp_txt = ui_iprp_display(iprp);

                        if(m > 1)
                            {
                            iprp_list += UI_TXT_INDENT + iprp_txt;
                            }
                        else
                            {
                            iprp_list = iprp_txt;
                            }

                        if(file_index_start <= m)
                            {
                            create_main_file_int(folder_index, iprp_txt, file_type_menu, iprp_to_param(iprp));
                            }
                        m++;
                        }
                    else
                        {
                        RemoveItemProperty(item, iprp);
                        }

                    n++;
                    iprp = GetNextItemProperty(item);
                    }

                //--------------------------------------/
                SetLocalString(FCB_HOST, ITEM_SEL_TXT, iprp_list);

                //set disp text
                //--------------------------------------/
                string item_header = ui_item_header(item, item_value);

                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);

                ui_disp_clear_local();
                }

            ui_stack_index(1);
            visual_effect();

            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_SETNAME_SEL_1:
            {
            string name = GetLocalString(FCB_HOST, SETNAME_SEL);

            object item = set_working_item_name(name);

            string item_header = ui_item_header(item);
            ui_disp_set_txt(DISP_INDEX_HEADER, UI_TXT_ITEM_WORKING_CURRENT, item_header);
            ui_disp_clear_local();

            ui_stack_index(0);
            visual_effect();

            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_SETNAME_DEFAULT:
            {
            string name = GetName(get_working_item(), 1);

            SetLocalString(FCB_HOST, SETNAME_SEL, name);

            ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_SETNAME | 1, 0);
            ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_SETNAME | 2, 0);
            ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_SETNAME | 3, 1);

            ui_disp_set_txt(DISP_INDEX_LOCAL1, UI_TXT_SETNAME_ENTERED, name);
            ui_disp_state_new(DISP_INDEX_LOCAL1, 1);

            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_CHARGE_SEL_0:
            {
            object item = get_working_item();
            object item_copy = copy_item(item, FCB_INV);

            int chcnt = GetLocalInt(FCB_HOST, MAIN_FILE_PARAM_ + IntToHexString(FILTER_F11 & stack_param));

            SetItemCharges(item_copy, chcnt);

            string charge_txt = IntToString(chcnt);

            //--------------------------------------/
            //check cost and ILR restriction
            int mod_enable = 1;

            //--------------------------------------/
            int cost = get_item_cost(item_copy) - get_item_cost(item);
            int user_gold = GetGold(user);

            string cost_disp = IntToString(cost) + UI_TXT_UNIT_GP;
            if(cost > user_gold)
                {
                cost_disp = c_crs(cost_disp + UI_TXT_INDENT + UI_TXT_COST_OUTOFBOUND);

                mod_enable = 0;
                }
            else
                {
                if(cost <= 0)
                    {
                    cost_disp = c_hlt2(cost_disp);
                    }
                }

            //--------------------------------------/
            int item_level = get_item_level(item);
            int item_copy_level = get_item_level(item_copy);
            int user_level = get_character_level(user);

            string item_copy_level_disp = IntToString(item_copy_level);
            if(GetLocalInt(FCB_HOST, SW_ILR_ENABLE)
                && item_copy_level > user_level
                && cost > 0)
                {
                item_copy_level_disp = c_crs(item_copy_level_disp + UI_TXT_INDENT + UI_TXT_LEVEL_OUTOFBOUND);

                mod_enable = 0;
                }

            //--------------------------------------/
            if(mod_enable)
                {
                SetLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST, cost);
                SetLocalInt(FCB_HOST, CHARGE_SEL, FILTER_F11 & chcnt);

                ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_CHARGE_MENU | 1, 0);
                }
            else
                {
                ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_CHARGE_MENU | 1, 1);
                }

            //--------------------------------------/
            charge_txt += UI_TXT_INDENT + c_hdr1(UI_TXT_COST) + UI_TXT_SPACE + cost_disp + UI_TXT_INDENT + c_hdr1(UI_TXT_LEVEL) + UI_TXT_SPACE + item_copy_level_disp;
            /////

            //--------------------------------------/
            DestroyObject(item_copy);

            //--------------------------------------/
            ui_disp_set_txt(DISP_INDEX_LOCAL1, "", c_hdr1(UI_TXT_CHARGE_SEL) + UI_TXT_SPACE + charge_txt);
            ui_disp_state_new(DISP_INDEX_LOCAL1, 1);
            //--------------------------------------/

            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_CHARGE_SEL_1:
            {
            //charge player
            //--------------------------------------/
            int cost = GetLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST);
            DeleteLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST);

            if(give_gold(-cost, user))
                {
                //--------------------------------------/
                int chcnt = GetLocalInt(FCB_HOST, CHARGE_SEL);
                object item = get_working_item();

                SetItemCharges(item, chcnt);
                string item_header = ui_item_header(item);

                //--------------------------------------/
                ui_disp_set_txt(DISP_INDEX_HEADER, UI_TXT_ITEM_WORKING_CURRENT, item_header);
                ui_disp_clear_local();
                }

            ui_stack_index(0);
            visual_effect();

            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_DUPE_SEL_0:
            {
            object item = get_working_item();

            //--------------------------------------/
            int cost = get_item_cost(item);
            string cost_disp = IntToString(cost) + UI_TXT_UNIT_GP;
            if(cost > GetGold(user))
                {
                cost_disp = c_crs(cost_disp + UI_TXT_INDENT + UI_TXT_COST_OUTOFBOUND);

                ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_DUPE | 1, 1);
                }
            else
                {
                if(cost <= 0)
                    {
                    cost_disp = c_hlt2(cost_disp);
                    }
                SetLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST, cost);

                ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_DUPE | 1, 0);
                }

            string disp_txt = c_hdr1(UI_TXT_DUPE_COST) + UI_TXT_SPACE + cost_disp;

            //--------------------------------------/
            ui_disp_set_txt(DISP_INDEX_LOCAL1, "", disp_txt);
            ui_disp_state_new(DISP_INDEX_LOCAL1, 1);

            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_DUPE_SEL_1:
            {
            //charge player
            //--------------------------------------/
            int cost = GetLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST);
            DeleteLocalInt(FCB_HOST, ITEM_WORKING_SEL_MOD_COST);

            if(give_gold(-cost, user))
                {
                copy_item(get_working_item(), user);

                //--------------------------------------/
                ui_disp_clear_local();
                }

            ui_stack_index(0);
            visual_effect();

            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_COLORGUIDE:
            {
            ExecuteScript("fcb__colorguide", FCB_HOST);

            break;
            }

        //--------------------------------------/
        //--------------------------------------/
        //--------------------------------------/
        case SCRIPT_INDEX_DISP_IPRP:
            {
            ui_set_active_disp_index(DISP_INDEX_IPRP);
            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_DISP_HEADER:
            {
            ui_set_active_disp_index(DISP_INDEX_HEADER);
            break;
            }

        //--------------------------------------/
        case SCRIPT_INDEX_DISP_BLANK:
            {
            ui_set_active_disp_index();
            break;
            }

        //--------------------------------------/
        //--------------------------------------/
        //--------------------------------------/
        case SCRIPT_INDEX_LISTEN_SETNAME:
            {
            ui_set_active_disp_index(DISP_INDEX_HEADER);

            ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_SETNAME | 1, 1);
            ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_SETNAME | 2, 1);
            ui_main_set_hidden_refresh(MAIN_FOLDER_INDEX_SETNAME | 3, 0);

            ui_disp_set_txt(DISP_INDEX_LOCAL1, UI_TXT_SETNAME_NONE);
            ui_disp_state_new(DISP_INDEX_LOCAL1, 1);

            break;
            }
        }
    }
