class 'GameTasksTracker'

local ACTIVATED_GAME_TASKS_STATE_NAME = "ACTIVATED_GAME_TASKS"
local FINISHED_GAME_TASKS_STATE_NAME = "FINISHED_GAME_TASKS"

GameTasksTracker.ACTIVATED_GAME_TASKS_STATE_NAME = ACTIVATED_GAME_TASKS_STATE_NAME

function GameTasksTracker:__init(game_scene)
    self.game_scene = game_scene

    self.taskDescriptions = {}
    local taskDescriptions = self.taskDescriptions
    local function addTaskDescription(id, percentOfGame)
        local desc = {
                        id = id,                        
                        percentOfGame = percentOfGame and percentOfGame or 0,
                        activated = false,
                        finished = false,
                     }
        --taskDescriptions[id] = desc
        table.insert(taskDescriptions, desc)
    end        
    
    --if not IsExtraSandboxMode or not IsExtraSandboxMode() then
        addTaskDescription('01', 2)
        addTaskDescription('02', 2)
        addTaskDescription('03', 2)
        addTaskDescription('04', 2)
        addTaskDescription('05', 2)
        addTaskDescription('06', 2)
        addTaskDescription('07', 2)

        addTaskDescription('08', 2)
        addTaskDescription('09', 2)
        addTaskDescription('10', 2)
        addTaskDescription('11', 2)
        addTaskDescription('12', 2)
        addTaskDescription('13', 2)
        addTaskDescription('14', 2)
        addTaskDescription('15', 2)

        if isCollectorsEdition() then
        end
    --[[else
        addTaskDescription('BONUS_MINIGAME', 100)
        addTaskDescription('BONUS_HO', 100)
    end]]

    self:initGameTasks()
end

function GameTasksTracker:getTaskById(id)
    for i=1,#self.taskDescriptions do
        local task = self.taskDescriptions[i]
        if task and task.id == id then
            return task
        end
    end    
    return nil
end

function GameTasksTracker:addGameTask(task_id)
    local task = self:getTaskById(task_id)
    
    --[[if not task and IsExtraSandboxMode and IsExtraSandboxMode() then
        return
    end]]
    
    assert(task and 'No description for task with this Id')

    if not HasGameState(ACTIVATED_GAME_TASKS_STATE_NAME, task_id) then
        AddUniqueGameState(HAS_NOT_SHOWN_TASKS, 'true')

        playCommonSound('AddTask')
    
        AddGameState(ACTIVATED_GAME_TASKS_STATE_NAME, task_id)
        --Console:Log('Task '..task_id..' added')
        
        current_scene:getPanel():getControlsPanel():updatePhoneUpdatesState()
    end
    task.activated = true
end

function GameTasksTracker:completeGameTask(task_id)
    local task = self:getTaskById(task_id)
    
    --[[if not task and IsExtraSandboxMode and IsExtraSandboxMode() then
        return
    end]]
    
    assert(task and 'No description for task with this Id')
    
    if not HasGameState(FINISHED_GAME_TASKS_STATE_NAME, task_id) then
        AddUniqueGameState(HAS_NOT_SHOWN_TASKS, 'true')
    
        AddGameState(FINISHED_GAME_TASKS_STATE_NAME, task_id)
        --Console:Log('Task '..task_id..' finished')
        
        current_scene:getPanel():getControlsPanel():updatePhoneUpdatesState()
    end
    task.finished = true
end

function GameTasksTracker:initGameTasks()
	if HasGameState(ACTIVATED_GAME_TASKS_STATE_NAME) then
		local tasks = GetGameState(ACTIVATED_GAME_TASKS_STATE_NAME)
		local num_tasks = tasks:getSize()
		
		for i = 0, num_tasks - 1 do
			local task_id = tasks:get(i)			
			self:addGameTask(task_id)
			if HasGameState(FINISHED_GAME_TASKS_STATE_NAME, task_id) then
			    self:completeGameTask(task_id)
			end
		end
	end
end

function GameTasksTracker:addAllTasks()
     for i = 1, #self.taskDescriptions do
        local task = self.taskDescriptions[i]   
    
        if not HasGameState(ACTIVATED_GAME_TASKS_STATE_NAME, task.id) then
            AddUniqueGameState(HAS_NOT_SHOWN_TASKS, 'true')           
            AddGameState(ACTIVATED_GAME_TASKS_STATE_NAME, task.id)       
        end
        task.activated = true
    end
    current_scene:getPanel():getControlsPanel():updatePhoneUpdatesState()
end

function GameTasksTracker:finishAllTasks()
     for i = 1, #self.taskDescriptions do
        local task = self.taskDescriptions[i]   
    
        if not HasGameState(FINISHED_GAME_TASKS_STATE_NAME, task.id) then
            AddUniqueGameState(HAS_NOT_SHOWN_TASKS, 'true')    
            AddGameState(FINISHED_GAME_TASKS_STATE_NAME, task.id)
        end
        task.finished = true
    end
    current_scene:getPanel():getControlsPanel():updatePhoneUpdatesState()
end

function addGameTask(task_id)
    local game_tasks_tracker = current_scene:getGameTasksTracker()
    
    game_tasks_tracker:addGameTask(task_id)
end

function completeGameTask(task_id)
    local game_tasks_tracker = current_scene:getGameTasksTracker()
    
    game_tasks_tracker:completeGameTask(task_id)
end

function getActivatedGameTasksDescriptions()
    local game_tasks_tracker = current_scene:getGameTasksTracker()
    
    local percentCompleted = 0
    local activatedTasks = {}
    for i,task in ipairs(game_tasks_tracker.taskDescriptions) do
        if task.activated then
            table.insert(activatedTasks, task)            
        end
        if task.finished then
            percentCompleted = percentCompleted + task.percentOfGame
        end
    end
    return activatedTasks, percentCompleted 
end

function getActiveGameTasksDescriptions()
    local game_tasks_tracker = current_scene:getGameTasksTracker()
    
    local percentCompleted = 0
    local activeTasks = {}
    for i,task in ipairs(game_tasks_tracker.taskDescriptions) do
        if task.activated and not task.finished then
            table.insert(activeTasks, task)            
        end
        if task.finished then
            percentCompleted = percentCompleted + task.percentOfGame
        end
    end
    return activeTasks, percentCompleted
end