require( "scripts/style.lua" );

local pnl_water_barrel_rollover_name = "pnl_water_barrel_rollover";

function Spawn_WaterBarrelRollOverPanel()
    return Group
    {
        WindowExt
        {
            name = pnl_water_barrel_rollover_name,
            x=880,y=440,
            w=234,h=266,
            
            state_handlers = {
                justcreated = function()
                    EnableWindow(pnl_water_barrel_rollover_name, false);
                end;
                showed = function()
                    EnableWindow(pnl_water_barrel_rollover_name, true);
                    local wb_lvl = GetCount("lvl_water_barrel");
                    EnableWindow("wb_upgrade_cost_1", wb_lvl == 1);
                    EnableWindow("wb_upgrade_cost_2", wb_lvl == 2);
                    EnableWindow("wb_upgrade_money", wb_lvl ~= 3);
                    EnableWindow("wb_rollover_upgrade", wb_lvl ~= 3);
                    
                    ActivateControl("wb_rollover_buy_one");
                    if GetCount("hint_group_water") == 2 then
                        ActivateControl("wb_rollover_fill");
                        ActivateControl("wb_rollover_upgrade");
                    else
                        DeactivateControl("wb_rollover_fill");
                        DeactivateControl("wb_rollover_upgrade");
                    end;
                end,
                hidden = function()
                    EnableWindow(pnl_water_barrel_rollover_name, false);
                end,
            },
            
            BitmapVisual
            {
                x=117,y=133,
                name = "picture",
                image="interface/game/pop_up_object/rollover_water",
                rect = "109,-128;-107,-129;-107,125;-46,126;3,245;108,226;",
                
                -- title
                Text
                {
                    x=kCenter,y=20,
                    h=20,w=100,
                    flags=kHAlignCenter+kVAlignCenter,
                    font=InfoTextFont,
                    label = "water_barrel_rollover_title",
                },
                -- body
                Text
                {
                    name="body",
                    x=kCenter,y=40,
                    h=60,w=220,
                    flags=kHAlignCenter+kVAlignCenter,
                    font=InfoTextFont,
                    label = "water_barrel_rollover_body",
                },
                
                -- one unit
                Text
                {
                    name="wb_unit_cost",
                    x=50,y=110,
                    h=20,w=40,
                    flags=kHAlignCenter+kVAlignCenter,
                    font=InfoTextFont,
                    label = "#"..tostring(costs.water_unit),
                },
                Bitmap
                {
                    name="wb_unit_money",
                    image = "interface/game/pop_up_object/gold_small",
                    x=25,y=105,
                },
                
                -- fill
                
                TextVisual
                {
                    name="wb_fill_cost",
                    x=50,y=160,
                    h=20,w=40,
                    flags=kHAlignCenter+kVAlignCenter,
                    font=InfoTextFont,
                    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;
                        wb_fill_cost_SetText(tostring(will_buy * costs.water_unit));
                    end;
                },
                Bitmap
                {
                    name="wb_fill_money",
                    image = "interface/game/pop_up_object/gold_small",
                    x=25,y=155,
                },
                
                -- upgrade
                Text
                {
                    name="wb_upgrade_cost_1",
                    x=50,y=210,
                    h=20,w=40,
                    flags=kHAlignCenter+kVAlignCenter,
                    font=InfoTextFont,
                    label = "#"..tostring(costs.water_barrel_1),
                },
                Text
                {
                    name="wb_upgrade_cost_2",
                    x=50,y=210,
                    h=20,w=40,
                    flags=kHAlignCenter+kVAlignCenter,
                    font=InfoTextFont,
                    label = "#"..tostring(costs.water_barrel_2),
                },
                Bitmap
                {
                    name="wb_upgrade_money",
                    image = "interface/game/pop_up_object/gold_small",
                    x=25,y=205,
                },
                
                -- buttons
                SetStyle(UpgradeButtonStyle),
                ButtonExt
                {
                    name = "wb_rollover_buy_one",
                    x = 152,
                    y = 122,
                    label = "buy_single_water",
                    graphics = RolloverButtonGraphics,
                    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);
                        end;
                    end,
                },
                SetStyle(UpgradeButtonStyle),
                ButtonExt
                {
                    name = "wb_rollover_fill",
                    x = 152,
                    y = 172,
                    label = "buy_all_water",
                    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;
                        end;
                    end,
                },
                
                ButtonExt
                {
                    name = "wb_rollover_upgrade",
                    x = 152,
                    y = 222,
                    label = "upgrade",
                    font = CreateNamedFont(MinionSbItSymbolSet, 24, ButtonBrown),
                    command = 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
                                DisplayDialog {
                                    "scripts/ok.lua",
                                    body = "upgrade_not_enough_money",
                                    title = "",
                                };
                            else
                                RemoveItem("money", cost);
                                SupplyItem("lvl_water_barrel", 1);
                            end;
                            GM_StartPersistentAction("UIClick");
                            GM_StartPersistentAction("ShopClick");
                        end;
                    end,
                },
            },
        },
    };
end;