require "scripts/style"

wNum = {3,2,6,7,4,5,1}; -- order of the weapons
posX        = 50;
textOffsetX = 60;
costOffsetX = 35;
textWidthX  = 225;
startPosY   = 70;
offsetIconY = 60;

-- function to set gWUnlockArray for weapon buy menu
function weaponBought(weaponNum)
    gWUnlockArray[weaponNum] = 0; -- unlocked and charged
    SetWindowVisible( "buyWeaponButton"..weaponNum, false );
    SetBitmap("buyWeaponButtonPic"..weaponNum, "pics/weapons/weapon_icons/weaponbtncheck");
-- find the unlocked items what are too expensive now
    for i = 1,7 do 
        if ( (gWUnlockArray[wNum[i]] == 1) and
             (gUserWeaponPoints < gWCostArray[wNum[i]]) ) 
        then
            SetWindowVisible( "buyWeaponButton"..wNum[i], false );
            SetBitmap("buyWeaponButtonPic"..wNum[i], "pics/weapons/weapon_icons/weaponbtnlock");
        end
    end
end

function placeWeaponImages( )
--[[
gWUnlockArray:
0 -> unlocked and charged
1 -> unlocked and empty
2 -> locked
]]
    local imgBtn;   -- image if the button is not available
    local wImg;     -- what image should we shown from the weapon
    local descText; -- weapon description
    local costText; -- cost label text
    
    
	t = { SetStyle(QuickTaskStyle), BeginGroup() };
    
    for i = 1,7 do 
        if ( gWUnlockArray[wNum[i]] == 2 ) then -- locked
            wImg     = "pics/weapons/weapon_icons/w_locked";
            descText = "weaponLockedDescription";
            imgBtn   = "pics/weapons/weapon_icons/weaponbtnlock" 
            costText = "questionMark";
        else
            if ( gWUnlockArray[wNum[i]] == 1 ) then -- unlocked and empty
                if(gUserWeaponPoints < gWCostArray[wNum[i]]) then
                    imgBtn   = "pics/weapons/weapon_icons/weaponbtnlock" 
                else
                    imgBtn = "";
                end
            else -- unlocked and charged
                imgBtn   = "pics/weapons/weapon_icons/weaponbtncheck" 
            end
            wImg = "pics/weapons/weapon_icons/w"..wNum[i].."_icon";
            descText = "weaponBuyMenuDescription0"..wNum[i];   
            costText = gWCostArray[wNum[i]];            
        end
-- weapon pic    
        table.insert(t,
            Bitmap
            {
                name = "weaponPic"..wNum[i],
                image = wImg,
                x = posX,
                y = startPosY + offsetIconY * (i-1),
                scale = 0.60
            }
        );
-- weapon pic golden ring        
        table.insert(t,
            Bitmap
            {
                name = "weaponGRingPic"..wNum[i],
                image = "pics/weapons/weapon_icons/golden_ring",
                x = -2 + posX,
                y = -2 + startPosY + offsetIconY * (i-1),
                scale = 0.59
            }
        ); 
-- weapon description        
    table.insert(t,
        Text
        {
            font = StandardButtonFontSmall,
            name = "weaponDescription"..wNum[i],
            x=posX+textOffsetX,
            y=startPosY + offsetIconY * (i-1), 
            w=textWidthX,
            h=55,
            flags = kHAlignLeft+kVAlignCenter,
            label = descText
        }
        );    
-- weapon cost text        
        table.insert(t,
            Text
            {
                font = StandardButtonFontSmall,--StandardButtonFont,
                name = "weaponCostText"..wNum[i],
                x=posX + textOffsetX + textWidthX,
                y=startPosY + offsetIconY * (i-1), 
                w=costOffsetX,
                h=55,
                flags = kHAlignCenter+kVAlignCenter,
                label = costText
            }
        );         
-- weapon button if needed
        if ( (gWUnlockArray[wNum[i]] == 1) and
             (gUserWeaponPoints >= gWCostArray[wNum[i]]) )
        then
            SetStyle(WorldMapMenuButtonStyle);
            -- weapon button
            table.insert(t,
                Button
                {
                    x = posX+textOffsetX+costOffsetX+textWidthX,
                    y = startPosY + offsetIconY * (i-1),
                    name="buyWeaponButton"..wNum[i],
                    graphics=buyWeaponButtonGraphics,
					sound = kDefaultButtonSound,
                    type = kPush,
                    command=
                        function()
                            buyWeaponPushed(wNum[i])
                        end	    
                }
            ); 
        end
-- weapon button pic
        table.insert(t,
            Bitmap
            {
                name = "buyWeaponButtonPic"..wNum[i],
                image = imgBtn,
                x = posX+textOffsetX+costOffsetX+textWidthX,
                y = startPosY + offsetIconY * (i-1),
            }
        );       
    end
   
    return Group(t);
end


MakeDialog
{
	-- Name the modal dialog
	name="weapon_buymenu",
	Bitmap
	{
		image="backgrounds/largedialog",
		x = kCenter,
		y = kCenter,
        
        weaponBuyMenuWindow{
			x = 0, y = 0, w=kMax, h=kMax,        
-- Title            
            Text
            {
                font = StandardButtonFontHuge,
                name = "powerUpShopTitle",
                x=kCenter,
                y=34,
                w=335,h=30,
                flags = kHAlignCenter + kVAlignCenter,
                label = "WeaponBuyMenuTitle"
            };
-- back button
            SetStyle(SmallEmptyRoundButtonStyle),
            Button
            {
                x = kCenter,
                y = kMax-70,
                name="weaponBuyBack",
                label="back",
                close=true,
                default=true,
                type = kPush,
            },
            placeWeaponImages( );            
        }
    },
}


