dofile('Data/Scripts/Common/Screens/HintAnimationLogicBase.lua')

-- local OFFSET_X = (SCR_W - WORK_W) / 2 

class 'HintAnimationLogic' (HintAnimationLogicBase)

---------------------------------------------------------------------------------------------------
function HintAnimationLogic:__init(controls_panel)	
	HintAnimationLogicBase.__init(self, controls_panel)	
	
	self.is_skip_maze_fill = false
	self.is_help_fill = false
	
	Helpers.attachInPlace(self:getMainEntity(), self.controls_panel:getEntityNamed('anchor_bottom_panel'))
	--self:getMainEntity():setOrder(10)
	--[[self.hint_filling = self:getEntityNamed('hint_filling')
	self.hint_filling:setFrame(tonumber(GetGameState('HintFillPercentage'):get(0)) / (self.hint_filling:getFramesCount() + 1))
	self:getEntityNamed('ballerina'):play()]]
	
	self:getEntityNamed('area_hint_timer').onFocusIn:add(function()
	        self:getEntityNamed('text_hint_timer'):show()
        end)
    self:getEntityNamed('area_hint_timer').onFocusOut:add(function()
            self:getEntityNamed('text_hint_timer'):hide()
        end)
end

-------------------------------------------------------------------------------
function HintAnimationLogic:setupFillDuration()
	HintAnimationLogicBase.setupFillDuration(self)

	self.fill_duration[EPanelMode.MATCH3] = self.quest_hint_fill_duration
end
-------------------------------------------------------------------------------
function HintAnimationLogic:setupModeHandlers()
	self.change_mode_handlers =
	{
		[EPanelMode.NONE] 			= function() self:activateHint() end,
		[EPanelMode.QUEST] 			= function() self:activateHint() end,
		[EPanelMode.MINIGAME] 		= function() self:activateSkip() end,
		[EPanelMode.HIDDENOBJECT] 	= function() self:activateHint() end,
		[EPanelMode.COMICS] 		= function() self:activateHint() end,
		[EPanelMode.CLEAN] 			= function() self:activateHint() end,
		[EPanelMode.HIDDENPICTURE] 	= function() self:activateHint() end,
		[EPanelMode.CUTSCENE] 		= function() self:activateHint() end,
		[EPanelMode.MAZE] 		    = function() self:activateMazeSkip() end,
		[EPanelMode.MATCH3] 		= function() self:activateHelp() end,
	}
end

-------------------------------------------------------------------------------
function HintAnimationLogic:setupButtons()
	HintAnimationLogicBase.setupButtons(self)
	self.helpButton = self:getEntityNamed('bttn_help')
	
	self.helpButton.onPress:add( bind(self.onHelpButtonClick, self) )
	self.helpButton:setSendEventsToParent(true)
	self.helpButton:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
end
-------------------------------------------------------------------------------
function HintAnimationLogic:activateHint()	
	if getPlayModeParameters().hintEnabled then
  		self.hintButton:show()
  		self:getEntityNamed('area_hint_timer'):show()
    else
		self.hintButton:hide()
		self:getEntityNamed('area_hint_timer'):hide()
	end 

	self.skipButton:hide()
	self.helpButton:hide()

	local hint_fill_percentage = tonumber(GetGameState('HintFillPercentage'):get(0))

	self.fill_time	  = hint_fill_percentage / 100 * self.fill_duration[self.mode][getPlayMode()]
	self.is_hint_fill = (hint_fill_percentage < 100)
	self.is_skip_fill = false
	self.is_skip_maze_fill = false
	self.is_help_fill = false

	self:showHintAnimation(true, hint_fill_percentage)

	if self.is_hint_fill then
		self.hintButton:setRespondable(false)
	end

	if self.onActivateHint then
		self:onActivateHint(hint_fill_percentage)
	end
end

function HintAnimationLogic:showHintAnimation(show, hint_fill_percentage)
	if (show) then 
		self:getEntityNamed('text_hint_timer'):setY(616)  -- some magic numbers
		self:onHintFillUpdate(hint_fill_percentage)
		self:getEntityNamed('sundial'):setVisible(true)
	else 
		self:getEntityNamed('text_hint_timer'):setY(660)   -- some magic numbers
		self:getEntityNamed('sundial'):setVisible(false)
		self:getEntityNamed('sun_1'):setVisible(false)
		self:getEntityNamed('sun_2'):setVisible(false)
		self:getEntityNamed('sun_3'):setVisible(false)
		self:getEntityNamed('sun_4'):setVisible(false)
		self:getEntityNamed('sun_5'):setVisible(false)
		self:getEntityNamed('full_hint'):setVisible(false)
	end
end	
-------------------------------------------------------------------------------
function HintAnimationLogic:activateSkip()
	self.hintButton:hide()
	self:showHintAnimation(false)

	if getPlayModeParameters().skipEnabled then
  		self.skipButton:show()
  		self:getEntityNamed('area_hint_timer'):show()
    else
		self.skipButton:hide()
		self:getEntityNamed('area_hint_timer'):hide()
	end	
	self.helpButton:hide()
	self.skipButton:setRespondable(false)

	self.fill_time	  = 0	
	self.is_hint_fill = false
	self.is_skip_fill = true
	self.is_skip_maze_fill = false
	self.is_help_fill = false

	if self.onActivateSkip then
		self:onActivateSkip( 0 )
	end
end

-------------------------------------------------------------------------------
function HintAnimationLogic:activateHelp()
	self.hintButton:hide()
	self.skipButton:hide()
	self:showHintAnimation(false)

	if getPlayModeParameters().hintEnabled then
  		self.helpButton:show()
  		self:getEntityNamed('area_hint_timer'):show()
    else
		self.helpButton:hide()
		self:getEntityNamed('area_hint_timer'):hide()
	end 
	
	
	self.helpButton:setRespondable(false)	

	local help_fill_percentage
	if HasGameState('HelpPercentage'..current_scene.name) then
	    help_fill_percentage = tonumber(GetGameState('HelpPercentage'..current_scene.name):get(0))
	else
	    help_fill_percentage = 0
	end

	self.fill_time	  = help_fill_percentage / 100 * self.fill_duration[self.mode][getPlayMode()]

	self.is_hint_fill = false
	self.is_skip_fill = false
	self.is_skip_maze_fill = false
	self.is_help_fill = true	
end
-------------------------------------------------------------------------------
function HintAnimationLogic:activateMazeSkip()
	self.skipButton:show()
	self.hintButton:hide()
	self.helpButton:hide()

    local skip_fill_percentage
    if HasGameState('MazeSkipPercentage'..current_scene.name) then
	    skip_fill_percentage = tonumber(GetGameState('MazeSkipPercentage'..current_scene.name):get(0))
	else
	    skip_fill_percentage = 0
	end

	self.fill_time	  = skip_fill_percentage / 100 * self.fill_duration[self.mode][getPlayMode()]
	self.is_skip_fill = false
	self.is_hint_fill = false
	self.is_help_fill = false
	self.is_skip_maze_fill = (skip_fill_percentage < 100)

	if self.is_skip_maze_fill then
		self.skipButton:setRespondable(false)
	end

	if self.onActivateSkip then
		self:onActivateSkip(skip_fill_percentage)
	end
end

-------------------------------------------------------------------------------
function HintAnimationLogic:onUpdate(dt)
    local fillDuration
	if self.is_hint_fill then
		fillDuration = self:getFillDuration()
		self.fill_time = math.min(self.fill_time + dt, fillDuration)

		local fillPercentage = 100 / fillDuration * self.fill_time
		SetGameState('HintFillPercentage', tostring(fillPercentage))

		if self.onHintFillUpdate then
			self:onHintFillUpdate(fillPercentage)
		end

		if self.fill_time == fillDuration then
			self:onHintFilled()
		end
	elseif self.is_skip_fill then
		fillDuration = self:getFillDuration()
		self.fill_time = math.min(self.fill_time + dt, fillDuration)

		local fillPercentage = 100 / fillDuration * self.fill_time
		if self.onSkipFillUpdate then
			self:onSkipFillUpdate(fillPercentage)
		end

		if self.fill_time == fillDuration then
			self:onSkipFilled()
		end
    elseif self.is_skip_maze_fill then
		fillDuration = self:getFillDuration()
		self.fill_time = math.min(self.fill_time + dt, fillDuration)        

		local fillPercentage = 100 / fillDuration * self.fill_time
		SetGameState('MazeSkipPercentage'..current_scene.name, tostring(fillPercentage))
		
		if self.onSkipFillUpdate then
			self:onSkipFillUpdate(fillPercentage)
		end

		if self.fill_time == fillDuration then
			self:onSkipMazeFilled()
		end
	elseif self.is_help_fill then
		fillDuration = self:getFillDuration()
		self.fill_time = math.min(self.fill_time + dt, fillDuration)        

		local fillPercentage = 100 / fillDuration * self.fill_time	
		SetGameState('HelpPercentage'..current_scene.name, tostring(fillPercentage))	
		
		if self.fill_time == fillDuration then
			self:onHelpFilled()
		end
	end
	
    local hintText = ''
    if self.fill_time and fillDuration and self.fill_time < fillDuration then  
    	local time_to_fill = fillDuration - self.fill_time
        local timerModeName
        local mode = self.controls_panel.panel:getMode()
        if mode == EPanelMode.HIDDENOBJECT or mode == EPanelMode.QUEST or mode == EPanelMode.HIDDENPICTURE then
            timerModeName = StringManager:getString('PANEL_HINT')

        elseif mode == EPanelMode.MINIGAME then
            timerModeName = StringManager:getString('PANEL_SKIP')
        elseif mode == EPanelMode.MAZE then
            timerModeName = StringManager:getString('PANEL_SKIP')
        elseif mode == EPanelMode.MATCH3 then
        	 timerModeName = StringManager:getString('PANEL_HELP')
        end
        if timerModeName then
            local minutesHintLeft = math.floor(time_to_fill/60)
            local secondsHintLeft = math.floor(time_to_fill % 60)
        
            hintText = timerModeName..minutesHintLeft..':'..(secondsHintLeft<10 and '0' or '')..secondsHintLeft
        end
    end  
    self:getEntityNamed('area_hint_timer'):setRespondable(not (hintText == ''))    
    self:getEntityNamed('text_hint_timer'):setText(hintText)
end

-------------------------------------------------------------------------------
function HintAnimationLogic:onSkipMazeFilled()
	self.skipButton:setRespondable(self.modeParameters.skipEnabled)
	self.is_skip_maze_fill = false	
end

-------------------------------------------------------------------------------
function HintAnimationLogic:onHelpFilled()
	self.helpButton:setRespondable(getPlayModeParameters().hintEnabled)
end
-------------------------------------------------------------------------------
function HintAnimationLogic:onHelpButtonClick()
	local level = self.game_scene:getPlayField()
	
	level.match3:getField():hint(true)   

	local scene_name = self.game_scene:getPlayField():getName()
    self.game_scene:getAchievmentsManager():handleEvent('on_help_pressed', {['scene_name'] = scene_name})

	self.fill_time = 0

	self:getEntityNamed('bttn_help'):setRespondable( false )
	SetGameState('HintFillPercentage', '0')
end
-------------------------------------------------------------------------------
function HintAnimationLogic:onHintFillUpdate(hint_fill_percentage)
   	self:getEntityNamed('sun_1'):setVisible((hint_fill_percentage <= 20 and hint_fill_percentage >= 2))
   	self:getEntityNamed('sun_2'):setVisible((hint_fill_percentage > 20 and hint_fill_percentage <= 40))
	self:getEntityNamed('sun_3'):setVisible((hint_fill_percentage > 40 and hint_fill_percentage <= 60))
	self:getEntityNamed('sun_4'):setVisible((hint_fill_percentage > 60 and hint_fill_percentage <= 80))
	self:getEntityNamed('sun_5'):setVisible((hint_fill_percentage > 80 and hint_fill_percentage < 100))
	self:getEntityNamed('full_hint'):setVisible(hint_fill_percentage == 100)
end
-------------------------------------------------------------------------------
function HintAnimationLogic:onHintFilled()
	self:onHintFillUpdate(100)
	self.hintButton:show()
end
-------------------------------------------------------------------------------
function HintAnimationLogic:onSkipFillUpdate(skip_fill_percentage)
	--self.hint_filling:setFrame(CorrectValue(skip_fill_percentage, 0, 100) / (self.hint_filling:getFramesCount() + 1))
end

-------------------------------------------------------------------------------
function HintAnimationLogic:onHintButtonClick()
    local hinted = self.game_scene:getPlayField():getHint():hint()
    if hinted == HintResult.RESET then
        local scene_name = self.game_scene:getPlayField():getName()
        self.game_scene:getAchievmentsManager():handleEvent('on_hint_pressed', {['scene_name'] = scene_name})
        self.is_hint_fill = true
        self.fill_time = 0

        self:getEntityNamed('bttn_hint'):setRespondable( false )
        SetGameState('HintFillPercentage', '0')

        playCommonSound('Hint')
    elseif hinted == HintResult.NO_OBJECTS then
        self:showBBT()
    end
end

-------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function HintAnimationLogic:CreateLoaderTable()
	local objects =
	{
		{ name = 'area_hint_timer',  entityType = 'SpriteEntity',  x = 977,  y = 725,  order = 20,  isRespondable = false,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 40, y = 40},  width = 80,  height = 80,  },
		{ name = 'bttn_hint',  entityType = 'ButtonEntity',  x = 979,  y = 710,  order = 20,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/ControlsPanel/Buttons/bttn_hint.png',  text = '',  },
		{ name = 'bttn_skip',  entityType = 'ButtonEntity',  x = 980,  y = 727,  order = 8,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/ControlsPanel/Buttons/bttn_skip.png',  text = '',  },
		{ name = 'text_hint_timer',  entityType = 'TextEntity',  x = 975,  y = 616,  order = 100,  isVisible = false,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  text = '123',  font = 'Data/Fonts/Options.fnt',  height = 20,  },
		{ name = 'bttn_help',  entityType = 'ButtonEntity',  x = 981,  y = 727,  order = 8,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/ControlsPanel/Buttons/bttn_help.png',  text = '',  },
		{ name = 'full_hint',  entityType = 'SpriteEntity',  x = 978,  y = 705,  order = 20,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/full_hint.png',  width = 124,  height = 102,  },
		{ name = 'sundial',  entityType = 'SpriteEntity',  x = 977,  y = 706,  order = 10,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/GUI/Hint/sundial.png',  width = 98,  height = 76,  },
		{ name = 'sun_5',  entityType = 'SpriteEntity',  x = 1015,  y = 689,  order = 16,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/sun_5.png',  width = 50,  height = 44,  },
		{ name = 'time_5',  entityType = 'SpriteEntity',  parentName = 'sun_5',  x = -42,  y = 10,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/time_5.png',  width = 94,  height = 64,  },
		{ name = 'sun_4',  entityType = 'SpriteEntity',  x = 999,  y = 670,  order = 17,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/sun_4.png',  width = 44,  height = 50,  },
		{ name = 'time_4',  entityType = 'SpriteEntity',  parentName = 'sun_4',  x = -23,  y = 36,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/time_4.png',  width = 68,  height = 74,  },
		{ name = 'sun_3',  entityType = 'SpriteEntity',  x = 979,  y = 667,  order = 16,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/sun_3.png',  width = 46,  height = 46,  },
		{ name = 'time_3',  entityType = 'SpriteEntity',  parentName = 'sun_3',  x = -3,  y = 42,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/time_3.png',  width = 50,  height = 76,  },
		{ name = 'sun_2',  entityType = 'SpriteEntity',  x = 945,  y = 667,  order = 14,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/sun_2.png',  width = 46,  height = 46,  },
		{ name = 'time_2',  entityType = 'SpriteEntity',  parentName = 'sun_2',  x = 33,  y = 42,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/time_2.png',  width = 76,  height = 68,  },
		{ name = 'sun_1',  entityType = 'SpriteEntity',  x = 907,  y = 663,  order = 16,  isVisible = false,  isRespondable = false,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/sun_1.png',  width = 50,  height = 48,  },
		{ name = 'time_1',  entityType = 'SpriteEntity',  parentName = 'sun_1',  x = 70,  y = 39,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  texturePath = 'Data/Textures/GUI/Hint/time_1.png',  width = 98,  height = 52,  },
	}
	return objects
end
