------------------------------------------------------------------------------
dofile('data/Scripts/Preload.lua')
------------------------------------------------------------------------------
class 'l_LoadingScreen_cycle' (BaseScreen)
------------------------------------------------------------------------------
local LOADING_SPEED  = 150
------------------------------------------------------------------------------
function l_LoadingScreen_cycle:__init(name, screen)
	BaseScreen.__init(self, name, screen)

    screen:GetObject('fade_out_loading_screen'):GetAnimation():SetColor('0xFFFFFFFF')
    screen:GetObject('fade_out_loading_screen'):SetVisible(true)
    screen:GetObject('fade_out_loading_screen'):ChangeOpacity(1, 0, 0.5)
    
    self.currect_width = 0
    self.max_width = screen:GetObject('load_filling'):GetAnimation():GetWidth()
    self:AddProgress(1)
    self:CreateLoadingCaption()
    
    self:SetLoadingData( GetPreloadResource() )

    self.loading_succefull = false
    self.is_destroy = nil
    
    if not IsCollectorsEdition() then
	    screen:GetObject('CE'):SetVisible(false)
	end
end

function l_LoadingScreen_cycle:SetLoadingData(resource)
    local lastDataPoint = 95
    local percentStep   = lastDataPoint/#resource
    
    self.loading_data = {}
    for i, data in ipairs(resource) do
        self.loading_data[i*percentStep] = data
    end
end

function l_LoadingScreen_cycle:GetPercentProgress()
    if self.currect_width >= self.max_width then
        return 100
    end

    return self.currect_width * 100 / self.max_width
end

function l_LoadingScreen_cycle:AddProgress(dt)
    self.currect_width = self.currect_width + dt
    if self.currect_width >= self.max_width then
        self.currect_width = self.max_width;
    end
    
    local object = self.screen:GetObject('load_filling')
    local animation = object:GetAnimation()
    
    local x = object:GetScrX()
    local y = object:GetScrY()
    local hot_spot_x = animation:GetHotX()
    local hot_spot_y = animation:GetHotY()
    local width = self.currect_width
    local height = animation:GetHeight()
    
    object:SetClippingRect(x - hot_spot_x, y - hot_spot_y, width, height)
end

function l_LoadingScreen_cycle:CreateCaption()
    local text = CreateTextEntity(MENU_FONT, 'PRESS_ANY_KEY')
	text:AttachTo(self.screen)
	text:SetXY(512, 740)
    text:SetZ(300)
    text:SetScale(0.6)
	text:SetTextAlignment(HGETEXT_CENTER + HGETEXT_MIDDLE)
	text:SetOpacity(0)

	local eff = CreateAnimationEffector()
	eff:AttachTo(text)
	eff:SetMode(_ANIM_LOOP)
	eff:SetKey(_TRACK_S,  0,        0.95)
	eff:SetKey(_TRACK_S,  0.25,	    0.95)
	eff:SetKey(_TRACK_S,  0.5,		1)
	eff:SetKey(_TRACK_S,  0.75,	    0.95)
	eff:SetKey(_TRACK_S,  1,		0.95)
	eff:Play()
	
	local time = 0.5
	self.screen:GetObject('load_border' ):ChangeOpacity(1, 0, time)
	self.screen:GetObject('load_filling'):ChangeOpacity(1, 0, time)
	self.loadingText:ChangeOpacity(1, 0, time)
	
	self.timer:Add(function()
	                    text:ChangeOpacity(0, 1, 0.25)
	               end, time)
	               
    -- load music -------------------------------------------------------------
	do
	    -- PlayAmbientMusic("music_main_menu", true)
	end
end

function l_LoadingScreen_cycle:CreateLoadingCaption()
    self.loadingText = CreateTextEntity(MENU_FONT, 'LOADING')
	self.loadingText:AttachTo(self.screen)
	self.loadingText:SetXY(512, 720)
    self.loadingText:SetZ(300)
    self.loadingText:SetScale(0.6)
	self.loadingText:SetTextAlignment(HGETEXT_CENTER + HGETEXT_MIDDLE)

	local eff = CreateAnimationEffector()
	eff:AttachTo(self.loadingText)
	eff:SetMode(_ANIM_LOOP)
	eff:SetKey(_TRACK_S,  0,        0.95)
	eff:SetKey(_TRACK_S,  0.25,	    0.95)
	eff:SetKey(_TRACK_S,  0.5,		1)
	eff:SetKey(_TRACK_S,  0.75,	    0.95)
	eff:SetKey(_TRACK_S,  1,		0.95)
	eff:Play()
	
	self.loadingText:ChangeOpacity(0, 1, 0.5)
end

function l_LoadingScreen_cycle:OnUpdate(dt)
    if self.screen:GetScrOpacity() < 1 then
        return
    end
    
    if not self.loading_succefull then
        self:AddProgress(dt * LOADING_SPEED)
        self:UpdateProgress()
    end

    if self.loading_succefull then
        local key = Input_GetKey()
        if key and key ~= 0 and not self.is_destroy then
            OnLoadingScreenCycleDestroy()
            self.is_destroy = true
        end
    end
end

function l_LoadingScreen_cycle:UpdateProgress()
    local progress = self:GetPercentProgress()
    
    for percent, data in pairs(self.loading_data) do
        if percent < progress and data then
            local dataType = type(data)
            
            if dataType == 'function' then
                data()
            elseif dataType == 'string' then
                PreCacheTexture(data)
            end
            self.loading_data[percent] = nil
        end
    end
    
    if progress == 100 then
        self.loading_succefull = true
        self:CreateCaption()
--[[
        self.movie_cycle:Go()
       
        local af = CreateAnimationEffector()
        af:AttachTo(self.movie_cycle)
        af:SetKey(_TRACK_O, 0.0, 0)
        af:SetKey(_TRACK_O, 0.5, 1)
        af:Play()
]]
    end
end

--------------------------------------------------------------------------------
function CreateLoadingScreenCycle()
    local screen = CreateScriptedScreen('data/Scripts/screens/LoadingScreen_cycle.lua', 'l_LoadingScreen_cycle')
    PopUpScreensMgr:Push(screen, false, nil)
end
--------------------------------------------------------------------------------
function OnLoadingScreenCycleDestroy()
     PopUpScreensMgr:Pop()
     --CreateMainMenu()
     CreateIntroScreen()
end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_LoadingScreen_cycle:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/Common/fade.png',0,0,512,384,1,0)
	anim:SetHotSpot(256,192)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('empty',anim,512,384,10,0,2,2)
	
	anim = CreateAnimation('data/Texture/Common/fade.png',0,0,512,384,1,0)
	anim:SetHotSpot(256,192)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('fade_out_loading_screen',anim,512,384,1000,0,2,2)
	
	anim = CreateAnimation('data/Texture/GUI/Loading/load_border.png',0,0,544,24,1,0)
	anim:SetHotSpot(272,12)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('load_border',anim,533,743,400,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Loading/load_filling.png',0,0,544,24,1,0)
	anim:SetHotSpot(272,12)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('load_filling',anim,533,743,390,0,1,1)
	
	anim = CreateAnimation('data/Texture/Logo/CE.png',0,0,1024,104,1,0)
	anim:SetHotSpot(512,52)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFEEC385')
	screen:CreateObject('CE',anim,552,277,350,0,0.45,0.45)
	
	anim = CreateAnimation('data/Texture/GUI/Loading/loadscreen.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('loadscreen',anim,512,384,155,0,1,1)
	
end
-- CLEAR_ON_SAVE_END
