require("scripts/cpscript.lua");
require("scripts/styles/style.lua");

local function CreateEventFunction(event)
	return function()
		PopState();
		StartEvent(event);
	end
end

local function CreateEventButtons()
	local groupTable = {};

	for i,value in ipairs(gDialogTable.events) do
		local data = loadstring(GetEventData(value))();

		table.insert(groupTable, SetStyle(_G["Button"..i.."Style"]));

		table.insert(groupTable,
			BlgButton
			{
				label = "wnd_chooseevent_ok",
				transformMatrix = {x = -90 + (i * 160), y = 120},
				--font = {
					--standardFont,
					--16,
					--Color(255, 255, 255),
				--},
				font = {
					standardFont,
					23,
					Color(182, 40, 154),
				},
				upState = {
					bitmap = "common/buttons/btn_default",
				},
				overState = {
					bitmap = "common/buttons/btn_rollover",
				},
				downState = {
					bitmap = "common/buttons/btn_pushed",
					sound = kDefaultButtonSound,
				},
				command = CreateEventFunction(data.service);
			}

		);
	end
	
	local attracts = gDialogTable.counts;
	
	table.insert(groupTable,
		BlgText {
			label = "wnd_chooseevent_title",
			flags = kHAlignCenter + kVAlignTop,
			w = 460,
			h = 150,
			font = {
				standardFont,
				32,
				Color(0, 0, 0),
			},

			transformMatrix = {x = 20, y = 30},
		}
	);
	
	if attracts > 1 then
	
		table.insert(groupTable,
			BlgText {
				label = "#"..GetString("wnd_chooseevent_attract_multi", tostring(attracts)),
				flags = kHAlignCenter + kVAlignTop,
				w = 460,
				h = 150,
				font = {
					standardFont,
					42,
					Color(0, 0, 0),
				},

				transformMatrix = {x = 22, y = 68},
			}
		);
		
		table.insert(groupTable,
			BlgText {
				label = "#"..GetString("wnd_chooseevent_attract_multi", tostring(attracts)),
				flags = kHAlignCenter + kVAlignTop,
				w = 460,
				h = 150,
				font = {
					standardFont,
					42,
					Color(255, 255, 255),
				},

				transformMatrix = {x = 20, y = 66},
			}
		);
	
	else
	
		table.insert(groupTable,
			BlgText {
				label = "#"..GetString("wnd_chooseevent_attract_single", tostring(attracts)),
				flags = kHAlignCenter + kVAlignTop,
				w = 460,
				h = 150,
				font = {
					standardFont,
					42,
					Color(0, 0, 0),
				},

				transformMatrix = {x = 22, y = 68},
			}
		);
		
		table.insert(groupTable,
			BlgText {
				label = "#"..GetString("wnd_chooseevent_attract_single", tostring(attracts)),
				flags = kHAlignCenter + kVAlignTop,
				w = 460,
				h = 150,
				font = {
					standardFont,
					42,
					Color(255, 255, 255),
				},

				transformMatrix = {x = 20, y = 66},
			}
		);
	
	end

	return Group(groupTable);
end

MakeState{
	transition = { type = "TransitionNone"; },
	BlgContainer{
	
		BlgMovieClip{
			transformMatrix = {x = 254, y = 200},

			-- Graphic content
			SetStyle(WindowBGStyle),
			BlgBitmap{
				image = "common/backgrounds/bg_dialog_1",
				name = "accountsDialog",
				center = {0, 0},
				transformMatrix = {x = 5, scaleX = 1.0, scaleY = 0.6},
			},

			CreateEventButtons();
			
			SetStyle(Button2Style),
			
			BlgButton{
				transformMatrix = {x = 250, y = 120},
				label = "wnd_chooseevent_cancel",
				--font = {
					--standardFont,
					--16,
					--Color(255, 255, 255),
				--},
				font = {
					standardFont,
					23,
					Color(182, 40, 154),
				},
				upState = {
					bitmap = "common/buttons/btn_default",
				},
				overState = {
					bitmap = "common/buttons/btn_rollover",
				},
				downState = {
					bitmap = "common/buttons/btn_pushed",
					sound = kDefaultButtonSound,
				},
				command = function()
					PopState();
				end
			},
		},
	}
};
