dofile('data/Scripts/Constants.lua')
dofile('data/Scripts/DebugInfo.lua')

function RandomBool()
    return math.random(0, 1)
end

function RandomTableSort(t)
    table_size = table.getn(t)
    for i = 1, table_size, 1 do
        ind1 = math.random(1, table_size)
        ind2 = math.random(1, table_size)
        tmp = t[ind1]
        t[ind1] = t[ind2]
        t[ind2] = tmp
    end
end

function Entity:Move(dx, dy, time)
    -- TODO: Need using adopt policy
    local af = CreateAnimationEffector()--AnimationEffector()
    af:AttachTo(self)
    af:SetKey(_TRACK_X, 0.0, self:GetX())
    af:SetKey(_TRACK_Y, 0.0, self:GetY())
    af:SetKey(_TRACK_X, time, self:GetX() + dx)
    af:SetKey(_TRACK_Y, time, self:GetY() + dy)
    af:Play()
end

function Entity:Rotate(dangle, time)
    local af = CreateAnimationEffector()--AnimationEffector()
    af:AttachTo(self)
    af:SetKey(_TRACK_A, 0.0, self:GetAngle())
    af:SetKey(_TRACK_A, time, self:GetAngle() + dangle)
    af:Play()
end

function Entity:ChangeOpacity(start, finish, time)
    local af = CreateAnimationEffector()--AnimationEffector()
    af:AttachTo(self)
    af:SetKey(_TRACK_O, 0.0, start)
    af:SetKey(_TRACK_O, time, finish)
    af:Play()
end

function CreateCloseButton(pos, message, listener)
---[[
	local button = CreateButton('ButtonZoomZoneCloseUp',
								'ButtonZoomZoneCloseOver',
								'ButtonZoomZoneCloseDown',
								message,
								listener)
	button:AttachTo(playField)
	button:SetX(pos.x)
	button:SetY(pos.y)
	button:SetZ(500)
button:SetScale(0.8);
--	button:SetRect(-17, -14, 34, 28)
--]]
--	local button = CreateAnimButton('AnimatedButtonZoomZoneClose', message, listener)
--	button:AttachTo(playField);
--	button:SetX(pos.x);
--	button:SetY(pos.y);
--	button:SetZ(500);
    return button
end

function CreateBackButton(pos, message, listener)
	local button = CreateButton('ButtonBigZoomZoneCloseUp',
								'ButtonBigZoomZoneCloseOver',
								'ButtonBigZoomZoneCloseDown',
								message,
								listener)
	button:AttachTo(playField)
	button:SetX(pos.x)
	button:SetY(pos.y)
	button:SetZ(500)
button:SetScale(1.0);
--	button:SetRect(-17, -14, 34, 28)
    return button
end

function CreateNextButton(pos, message, listener)
	local button = CreateButton('ButtonNextUp',
								'ButtonNextOver',
								'ButtonNextDown',
								message,
								listener)
	button:AttachTo(playField)
	button:SetX(pos.x)
	button:SetY(pos.y)
	button:SetZ(500)
	--button:SetRect(-17, -14, 34, 28)
    return button
end

function GetAnimationLength(anim)
    assert(anim:GetSpeed() ~= 0.)
    return anim:GetFrames() / anim:GetSpeed()
end

function CreateCloseAppButton(parent)
	assert(playField ~= nil);

	local button = CreateButton('InGameMenuButtonUp',
								'InGameMenuButtonOver',
								'InGameMenuButtonDown', 
								'MSG_CLOSE_APPLICATION',
								playField:GetLevelScreen());
	button:AttachTo(parent ~= nil and parent or playField);
	button:SetXY(-10.0, SCREEN_HEIGHT/2.0 - 50.0);
	button:SetZ(1.0);
	button:SetVisible(true);

	local captionColor = '0xFF703030';
	local captionScale = 1.2;
	local captionDY	   = -3.0;
	local captionDX	   = -6.0;

	local caption = CreateTextEntity('GUIFont', 'OK');
	caption:SetName('CloseApplicationAndOpenSurveyURL');
    caption:AttachTo(button);
	caption:SetColor(captionColor);
	caption:SetScale(captionScale);
	caption:SetXY(captionDX, captionDY);
	caption:SetZ(0.05);
end
