require( "scripts/style.lua" );

local dlg_water_timer = {
    name = "panels",
    category = "UI",
};

local dlg_water_name = "refill_water";
local buttonXLeft = 180;
local kButtonSpace = 120;

MakeDialog
{
    WindowExt
    {
        name=dlg_water_name,
        x=512, y=384,
        w = 1024, h = 768,
        
        timer_settings = dlg_water_timer,
        state_handlers = {
            justcreated = function()
                SuspendFXPool("");
                ResumeFXPool("refill_water");
                EnableWindow("pnl_water_barrel_rollover", false);
                EnableWindow("pnl_fountain_rollover", false);
                EnableWindow("pnl_gates_rollover", false);
                EnableWindow("pnl_magic_object_rollover", false);
                EnableWindow("pnl_storage_rollover", false);
                ShowControl(dlg_water_name);
                PauseTimer("ui", "UI");
                PauseTimer("game", "game");
                SetCursorAllowed(true);
            end;
            showing = function()
                water_progress_Fill(GetCount("water") / GetCount("waterbarrel_capacity"));
                SetItemCount("lvl_water_barrel", GetCount("lvl_water_barrel"));
                SetItemCount("water", GetCount("water"));
                SetItemCount("money", GetCount("money"));
                SetItemCount("waterbarrel_capacity", GetCount("waterbarrel_capacity"));
                EnableWindow("pnl_water_upg", false);
                
                GM_ProcessGOActions();
                HM_SetMouseProcessing(true);
                GM_StartPersistentAction("RefillWaterClick");
                SetItemCount("pnl_refill_state", 1);
            end;
            hiding = function()
                HM_SetMouseProcessing(false);
            end;
            hidden = function()
                SetItemCount("pnl_refill_state", 0);
                GM_StartPersistentAction("ShopClick");
                
                if not EffectRunning() then
                    StartTimer("ui", "UI");
                    StartTimer("game", "game");
                end;
                
                ResumeFXPool("");
                SuspendFXPool("refill_water");
                CloseWindow();
            end;
        },
        BitmapVisual
        {
            name="bmp_fade_wp",
            image    = "cutscenes/black",
            timer_settings = {
                name = "fade",
                category = "UI",
            },
            alpha_val = 0.8,
            x = 512, y = 384,
            w = 1024, h = 768,

            Bitmap
            {
                image="backgrounds/water_panel",
                x=kCenter,
                y=kCenter,
                
                SetStyle(TextStyle),
                CreateMinionSbItText(24, CaptionBrown, kCenter+1, 41, "#"..GetString("caption_outline", GetString("refill_title")), 250, 24),
                CreateMinionSbItText(24, CaptionYellow, kCenter, 40, "#"..GetString("caption_outline", GetString("refill_title")), 250, 24),
                
                CreateMinionSbItText(20, ShadowColor, kCenter+1, 56, "#"..GetString("caption_outline", GetString("refill_description")), 300, 48),
                CreateMinionSbItText(20, WhiteColor, kCenter, 55, "#"..GetString("caption_outline", GetString("refill_description")), 300, 48),
                
                -- WATER
                TextVisual
                {
                    name = "wp_txt_water_shadow",
                    flags = kHAlignCenter + kVAlignCenter,
                    font = CreateNamedFont(MinionSbItSymbolSet, 22, BlueOutline),
                    x=346,y=261,
                    h = 22, w = 30,
                    context = {"water",},
                    context_change = function(item, old_val, new_val)
                        wp_txt_water_shadow_SetText(GetString("blue_outline", tostring(new_val)));
                    end;
                },
                TextVisual
                {
                    name = "wp_txt_water",
                    flags = kHAlignCenter + kVAlignCenter,
                    font = CreateNamedFont(MinionSbItSymbolSet, 22, BudGold),
                    x=345,y=260,
                    h = 22, w = 30,
                    context = {"water",},
                    context_change = function(item, old_val, new_val)
                        wp_txt_water_SetText(GetString("blue_outline", tostring(new_val)));
                    end;
                },
                
                TextVisual
                {
                    name = "wp_txt_water_cap_shadow",
                    flags = kHAlignLeft + kVAlignCenter,
                    font = CreateNamedFont(MinionSbItSymbolSet, 18, BlueOutline),
                    x = 381,y = 261,
                    h = 20, w = 40,
                    context = {"waterbarrel_capacity",},
                    context_change = function(item, old_val, new_val)
                        wp_txt_water_cap_shadow_SetText(GetString("blue_outline", "/"..tostring(new_val)));
                    end;
                },
                TextVisual
                {
                    name = "wp_txt_water_cap",
                    flags = kHAlignLeft + kVAlignCenter,
                    font = CreateNamedFont(MinionSbItSymbolSet, 18, LightBlueText),
                    x = 380,y = 260,
                    h = 20, w = 40,
                    context = {"waterbarrel_capacity",},
                    context_change = function(item, old_val, new_val)
                        wp_txt_water_cap_SetText(GetString("blue_outline", "/"..tostring(new_val)));
                        water_progress_Fill(GetCount("water") / GetCount("waterbarrel_capacity"));
                    end;
                },
                -- MONEY
                CreateMinionSbItText(20, ShadowColor, 46, 228, "#"..GetString("caption_outline", GetString("wp_coins")), 300, 48),
                CreateMinionSbItText(20, WhiteColor, 45, 227, "#"..GetString("caption_outline", GetString("wp_coins")), 300, 48),
                
                CreateMinionSbItText(22, CaptionBrown, 151, 127, "#"..GetString("caption_outline", tostring(costs.water_unit)), 40, 22),
                CreateMinionSbItText(22, CaptionYellow, 150, 126, "#"..GetString("caption_outline", tostring(costs.water_unit)), 40, 22),
                
                TextVisual
                {
                    name="wp_fill_cost_sh",
                    x=150,y=191,
                    h=22,w=40,
                    flags=kHAlignCenter+kVAlignCenter,
                    font = CreateNamedFont(MinionSymbolSet, 22, CaptionBrown),
                    label = "#"..tostring(costs.water_unit),
                    context = {"water", "waterbarrel_capacity", "money"},
                    context_change = function(item, old_val, new_val)
                        local required = GetCount("waterbarrel_capacity") - GetCount("water");
                        local can_afford = math.floor(GetCount("money") / costs.water_unit);
                        local will_buy = required;
                        if (can_afford < required) then
                            will_buy = can_afford;
                        end;
                        wp_fill_cost_sh_SetText(GetString("caption_outline", tostring(will_buy * costs.water_unit)));
                    end;
                },
                TextVisual
                {
                    name="wp_fill_cost",
                    x=151,y=190,
                    h=22,w=40,
                    flags=kHAlignCenter+kVAlignCenter,
                    font = CreateNamedFont(MinionSymbolSet, 22, CaptionYellow),
                    label = "#"..tostring(costs.water_unit),
                    context = {"water", "waterbarrel_capacity", "money"},
                    context_change = function(item, old_val, new_val)
                        local required = GetCount("waterbarrel_capacity") - GetCount("water");
                        local can_afford = math.floor(GetCount("money") / costs.water_unit);
                        local will_buy = required;
                        if (can_afford < required) then
                            will_buy = can_afford;
                        end;
                        wp_fill_cost_SetText(GetString("caption_outline", tostring(will_buy * costs.water_unit)));
                    end;
                },
                
                TextVisual
                {
                    name = "wb_txt_money_shadow",
                    x=226,y=241,
                    h=22,w=90,
                    flags=kHAlignCenter+kVAlignCenter,
                    font = CreateNamedFont(MinionSymbolSet, 22, CaptionBrown),
                    context = {"money"},
                    context_change = function(item, old_val, new_val)
                        wb_txt_money_shadow_SetText(GetString("caption_outline", tostring(new_val)));
                    end;
                },
                TextVisual
                {
                    name = "wb_txt_money",
                    x=225,y=240,
                    h=22,w=90,
                    flags=kHAlignCenter+kVAlignCenter,
                    font = CreateNamedFont(MinionSymbolSet, 22, CaptionYellow),
                    context = {"money"},
                    context_change = function(item, old_val, new_val)
                        wb_txt_money_SetText(GetString("caption_outline", tostring(new_val)));
                    end;
                },
                
                WndProgressVertical
                {
                    name = "water_progress",
                    x = kCenter + 93,
                    y = kCenter-30,
                    h = 214,
                    w = 180,
                    
                    progress_x = 32,
                    progress_y = 80,
                    
                    picture_back = "game/common/water_barrel/water_barrel",
                    picture_progress = "game/common/water_barrel/progress_water",
                    fill_coef = 0,
                },

                SetStyle(ButtonStyle),
                -- CLOSE
                ButtonExt
                {
                    name = "close",
                    state_handlers = {
                        justcreated = function()
                            ActivateControl("close")
                        end;
                    },
                    flags = kHAlignCenter + kVAlignCenter,
                    label = "close",
                    x = buttonXLeft + 64,
                    y = 315,
                    font = CreateNamedFont(MinionSbItSymbolSet, 24, ButtonBrown),
                    command = function()
                        HideControl(dlg_water_name);
                    end;
                },
                
                -- 1 UNIT
                ButtonExt
                {
                    name = "buy_1_water",
                    state_handlers = {
                        justcreated = function()
                            ActivateControl("buy_1_water")
                        end;
                    },
                    flags = kHAlignCenter + kVAlignCenter,
                    label = "buy_single_water",
                    x = buttonXLeft+64,
                    y = 140,
                    font = CreateNamedFont(MinionSbItSymbolSet, 24, ButtonBrown),
                    command = function()
                        if (GetCount("money") >= costs.water_unit) and (GetCount("water") < GetCount("waterbarrel_capacity")) then
                            RemoveItem("money", costs.water_unit);
                            SupplyItem("water", 1);
                            SetItemCount("days_no_buy_water", 0);
                            water_progress_Fill(GetCount("water") / GetCount("waterbarrel_capacity"));
                        end;
                    end;
                },
                
                -- FILL
                ButtonExt
                {
                    name = "buy_all_water",
                    state_handlers = {
                        justcreated = function()
                            ActivateControl("buy_all_water")
                        end;
                    },
                    flags = kHAlignCenter + kVAlignCenter,
                    label = "buy_all_water",
                    x = buttonXLeft + 64,
                    y = 200,
                    font = CreateNamedFont(MinionSbItSymbolSet, 24, ButtonBrown),
                    command = function()
                        if GetCount("hint_group_water") == 2 then
                            local required = GetCount("waterbarrel_capacity") - GetCount("water");
                            local can_afford = math.floor(GetCount("money") / costs.water_unit);
                            local will_buy = required;
                            if (can_afford < required) then
                                will_buy = can_afford;
                            end;
                            RemoveItem("money", will_buy * costs.water_unit);
                            SupplyItem("water", will_buy);
                            if (will_buy ~= 0) then
                                SetItemCount("days_no_buy_water", 0);
                            end;
                            water_progress_Fill(GetCount("water") / GetCount("waterbarrel_capacity"));
                        end;
                    end;
                },
                
                -- UPGRADE
                ButtonVisual
                {
                    name = "wp_upg",
                    x = buttonXLeft + kButtonSpace + 64,
                    y = 315,
                    layer_click = 10000,
                    tex_normal = "buttons/buttonup",
                    tex_over   = "buttons/buttonrollover",
                    tex_push   = "buttons/buttondown",
                    snd_click    = kMenuButtonSound,
                    snd_over     = kMenuRolloverSound,
                    rect      = "-13,33;16,33;58,20;58,-20;14,-33;-17,-33;-62,-18;-62,24;",
                    actions   = {"RefillWaterClick",},
                    context = {"lvl_water_barrel",},
                    context_change = function(item, old_val, new_val)
                        if new_val == 3 then
                            wp_upg_SetLocked(true);
                            EnableWindow("wp_upg", false);
                            EnableWindow("pnl_water_upg", false);
                            CR_SwitchToDefault();
                        end;
                    end;
                    on_click = function()
                        if GetCount("lvl_water_barrel") < 3 then
                            local is_lvl_1 = (GetCount("lvl_water_barrel") == 1);
                            local is_lvl_2 = (GetCount("lvl_water_barrel") == 2);
                            local cost = 0;
                            if is_lvl_1 then
                                cost = costs.water_barrel_1;
                            end;
                            if is_lvl_2 then
                                cost = costs.water_barrel_2;
                            end;
                            if cost > GetCount("money") then
                                EnableWindow("pnl_water_upg", false);
                                CR_SwitchToDefault();
                                GM_StartPersistentAction("NullClick");
                                SetItemCount("water_cant_upgrade", 1);
                                DisplayDialog {
                                    "scripts/ok.lua",
                                    body = "upgrade_not_enough_money",
                                    title = "",
                                };
                                SetItemCount("water_cant_upgrade", 0);
                                GM_StartPersistentAction("RefillWaterClick");
                            else
                                RemoveItem("money", cost);
                                SupplyItem("lvl_water_barrel", 1);
                            end;
                        end;
                    end;
                    on_enter = function()
                        EnableWindow("pnl_water_upg", true);
                    end;
                    on_exit  = function()
                        EnableWindow("pnl_water_upg", false);
                    end;
                        
                    SetStyle(TextStyle),
                    CreateMinionText(22, ButtonYellow, kCenter+1, kCenter+1, "#"..GetString("button_outline", GetString("upgrade")), 140, 22),
                    CreateMinionText(22, ButtonBrown, kCenter, kCenter, "#"..GetString("button_outline", GetString("upgrade")), 140, 22),
                },

                -- LAMP FX

                WndFxSprite
                {
                    name = "left_lamp_firebugs_fx",
                    x = 0,
                    y = 0,
                    w = 580,
                    h = 310,
                    effect_position = "57,96",
                    file_name = "fx/lamp_firebugs.lua",
                    start_activate = true,
                    anim_pool = "refill_water",
                },

                WndFxSprite
                {
                    name = "right_lamp_firebugs_fx",
                    x = 0,
                    y = 0,
                    w = 580,
                    h = 310,
                    effect_position = "508,96",
                    file_name = "fx/lamp_firebugs.lua",
                    start_activate = true,
                    anim_pool = "refill_water",
                },

                WndFxSprite
                {
                    name = "left_lamp_light_fx",
                    x = 0,
                    y = 0,
                    w = 580,
                    h = 310,
                    effect_position = "57,96",
                    file_name = "fx/lamp_light.lua",
                    start_activate = true,
                    anim_pool = "refill_water",
                },

                WndFxSprite
                {
                    name = "right_lamp_light_fx",
                    x = 0,
                    y = 0,
                    w = 580,
                    h = 310,
                    effect_position = "508,96",
                    file_name = "fx/lamp_light.lua",
                    start_activate = true,
                    anim_pool = "refill_water",
                },
                
                Button
                {
                    x = 2000,
                    y = 1000,
                    cancel=true,
                    command = function()
                        PostMessage(CreateNamedMessage(1013, "pause"));
                    end;
                },
            },
            
            Window
            {
                name = "pnl_water_upg",
                x = 650,
                y = 435,
                w=254,h=168,
                
                BitmapVisual
                {
                    x=127,y=84,
                    name = "picture",
                    image="interface/game/pop_up_object/rollover_obj",
                    
                    Bitmap
                    {
                        image = "interface/game/pop_up_object/gold_small",
                        x=kCenter-20,y=115,
                    },
                    
                    SetStyle(TextStyle),
                    CreateMinionText(24, CaptionYellow, kCenter, 22, "water_barrel_upgrade_title", 200, 24),
                    
                    TextVisual
                    {
                        name = "wp_upg_price",
                        flags = kHAlignCenter + kVAlignCenter,
                        font = CreateNamedFont(MinionSymbolSet, 20, CaptionYellow),
                        x = kCenter+20, y = 120,
                        w = 180, h = 20,
                        context = {"lvl_water_barrel",},
                        context_change = function(item, old_val, new_val)
                            if new_val == 1 then
                                wp_upg_price_SetText(tostring(costs.water_barrel_1));
                            end;
                            if new_val == 2 then
                                wp_upg_price_SetText(tostring(costs.water_barrel_2));
                            end;
                        end;
                    },
                    
                    TextVisual
                    {
                        name = "wp_upg_body",
                        flags = kHAlignCenter + kVAlignCenter,
                        font = CreateNamedFont(MinionSymbolSet, 18, WhiteColor),
                        x = kCenter, y = kCenter,
                        w = 250, h = 50,
                        context = {"lvl_water_barrel",},
                        context_change = function(item, old_val, new_val)
                            if new_val == 1 then
                                wp_upg_body_SetText(GetString("water_barrel_upgrade_body",
                                    tostring(mechanics.water_barrel.capacity_lvl_1), 
                                    tostring(mechanics.water_barrel.capacity_lvl_2)));
                            end;
                            if new_val == 2 then
                                wp_upg_body_SetText(GetString("water_barrel_upgrade_body",
                                    tostring(mechanics.water_barrel.capacity_lvl_2), 
                                    tostring(mechanics.water_barrel.capacity_lvl_3)));
                            end;
                        end;
                    },
                },
            },
        },
    },
    
};
