local ActTaskData = class("ActTaskData", BaseData)

function ActTaskData:setDirty()
	self.data.isDirty = not self.data.isDirty
end

function ActTaskData:ctor()
end

function ActTaskData:clear()
	self.taskData = nil
	self.taskDataScore = nil
end

-- 任务初始化
function ActTaskData:initTaskData(scoreData, normalData)
	scoreData = scoreData or {}
	normalData = normalData or {}
	if EDITOR_MODE then
		Logger.logHighlight("活动任务数据")
		Logger.printTable(scoreData)
		Logger.printTable(normalData)
	end

	self.taskData = self.taskData or {}
	self.taskDataScore = self.taskDataScore or {}

	for i, info in ipairs(normalData.task_data) do
		self.taskData[info.activity_id] = self.taskData[info.activity_id] or {}
		self.taskData[info.activity_id][info.data.id] = info.data
	end

	for i, info in ipairs(scoreData.task_data) do
		self.taskDataScore[info.activity_id] = self.taskDataScore[info.activity_id] or {}
		self.taskDataScore[info.activity_id] = info
		self.taskDataScore[info.activity_id].claimedMap = {}
		for _, taskId in ipairs(info.claimed) do
			self.taskDataScore[info.activity_id].claimedMap[taskId] = true
		end
	end

	if not self.isInit then
		self.isInit = true
		-- 监听任务
		for id, info in pairs(self:getTaskConfig()) do
			ModuleManager.TaskManager:registerTask("ActTaskData", info.type, function(count)
				self:addTaskProgressCallback(info.type, count)
				self:addTaskProgressCallbackScore(info.type, count)
			end)
		end
		-- 跨天
		DataManager:registerCrossDayFunc("ActTaskData", function()
			self:setDirty()
		end)
	end
end

--region 普通任务
function ActTaskData:getTaskIdList(actId)
	local cfg = self:getTaskConfig()
	local actTaskList = {}
	for taskId, info in pairs(cfg) do
		if info.activity == actId and info.display == 2 then
			table.insert(actTaskList, taskId)
		end
	end
	return actTaskList
end

function ActTaskData:addTaskProgressCallback(taskType, count)
	for actId, data in pairs(self.taskData) do
		for i, taskId in ipairs(self:getAllTaskIdsByActId(actId)) do
			if self:getTaskType(taskId) == taskType and self:getTaskDisplay(taskId) == 2 then
				self:addTaskProgress(actId, taskId, count)
			end
		end
	end
end

-- 添加任务进度
function ActTaskData:addTaskProgress(actId, taskId, count)
	if not self:isTaskFinished(actId, taskId) then
		local taskType = self:getTaskType(taskId)
		local data = self:getTaskData(actId, taskId)
		if table.containValue(GConst.TaskConst.RELPACE_TASK_TYPE, taskType) then
			data.progress = math.max(data.progress or 0, count)
		else
			data.progress = (data.progress or 0) + count
		end
	end
	self:setDirty()
end

function ActTaskData:getTaskData(actId, taskId)
	if taskId then
		self.taskData[actId] = self.taskData[actId] or {}
		if self.taskData[actId][taskId] == nil then
			self.taskData[actId][taskId] = {id = taskId, progress = 0, claimed = 0}
		end
		return self.taskData[actId][taskId]
	else
		return self.taskData[actId]
	end
end

-- 获取活动全部任务id
function ActTaskData:getAllTaskIdsByActId(actId)
	self.taskIds = self.taskIds or {}
	if self.taskIds[actId] == nil then
		self.taskIds[actId] = {}
		for id, info in pairs(self:getTaskConfig()) do
			if self:getActId(id) == actId then
				table.insert(self.taskIds[actId], id)
			end
		end
		table.sort(self.taskIds, function(a, b) return a.id < b.id end)
	end

	return self.taskIds[actId]
end

-- 获取任务当前进度
function ActTaskData:getTaskProg(actId, taskId)
	taskId = tonumber(taskId)
	local data = self:getTaskData(actId, taskId)
	return data and data.progress or 0
end

-- 任务是否已领取
function ActTaskData:isTaskReceived(actId, taskId)
	taskId = tonumber(taskId)
	local data = self:getTaskData(actId, taskId)
	return data and data.claimed and data.claimed > 0
end

-- 任务是否已完成
function ActTaskData:isTaskFinished(actId, taskId)
	return self:getTaskProg(actId, taskId) >= self:getTaskTarget(taskId)
end

-- 任务是否可领取
function ActTaskData:canClaimTask(actId, taskId)
	return self:isTaskFinished(actId, taskId) and not self:isTaskReceived(actId, taskId)
end

-- 重置任务数据
function ActTaskData:resetTaskData(actId)
	for taskId, data in pairs(self:getTaskData(actId)) do
		data.claimed = 0
		data.progress = 0
	end
	self:setDirty()
end

-- 领奖成功
function ActTaskData:onTaskClaimed(actId, taskId)
	local data = self:getTaskData(actId, taskId)
	data.claimed = 1
	self:setDirty()
end

-- 普通任务列表
function ActTaskData:getTaskSortList(actId)
	local list = {}
	local cfg = self:getTaskConfig()
	for taskId, info in pairs(cfg) do
		if info.activity == actId and info.display == 2 then
			local info = {}
			info.id = taskId
			info._sort = 1000000 - taskId
			if self:canClaimTask(actId, taskId) then
				info._sort = info._sort + 100000000
			end
			if not self:isTaskFinished(actId, taskId) then
				info._sort = info._sort + 10000000
			end
			table.insert(list, info)
		end
	end
	table.sort(list, function(a, b)
		-- 已完成(可领奖) > 进行中 > 已完成(已领奖) > id
		return a._sort > b._sort
	end)

	return list
end
--endregion

--region 积分任务
function ActTaskData:getTaskIdListScore(actId)
	local cfg = self:getTaskConfig()
	local actTaskList = {}
	for taskId, info in pairs(cfg) do
		if info.activity == actId and info.display == 1 then
			table.insert(actTaskList, taskId)
		end
	end
	return actTaskList
end

function ActTaskData:addTaskProgressCallbackScore(taskType, count)
	for actId, data in pairs(self.taskDataScore) do
		local actTaskList = self:getTaskIdListScore(actId)
		if actTaskList and actTaskList[1] then
			if self:getTaskType(actTaskList[1]) == taskType then
				data.score = (data.score or 0) + count
			end
		end
	end
end

-- 添加任务进度
function ActTaskData:addTaskProgressScore(actId, taskId, count)
	if not self:isTaskFinishedScore(actId, taskId) then
		self.taskDataScore[actId].score = (self.taskDataScore[actId].score or 0) + count
	end
	self:setDirty()
end

function ActTaskData:getTaskNumScore(actId, actTaskId)
	if self:getTaskDisplay(actTaskId) == 1 then
		return self.taskDataScore[actId].score or 0
	else
		return self.taskData[actTaskId] or 0
	end
end

function ActTaskData:getMaxTurnScore(actId)
	return DataManager.ActivityData:getActRoundLimit(actId)
end

function ActTaskData:getRewardsgetMaxTurnScore(actId)
	return DataManager.ActivityData:getActRoundReward(actId)
end

function ActTaskData:getHasGotTaskRewardScore(actId, actTaskId)
	return self.taskDataScore[actId].round >= self:getMaxTurnScore(actId) or self.taskDataScore[actId].claimedMap[actTaskId]
end

function ActTaskData:getTaskDataScore(actId)
	return self.taskDataScore[actId] or {activity_id = actId, score = 0, total_score = 0, round = 0, round_claimed = 0, claimed = {}, claimedMap = {}}
end

-- 获取任务当前进度
function ActTaskData:getTaskProgScore(actId)
	local data = self:getTaskDataScore(actId)
	return data and data.score or 0
end

-- 任务是否已完成
function ActTaskData:isTaskFinishedScore(actId, taskId)
	return self:getTaskProgScore(actId) >= self:getTaskTarget(taskId)
end

function ActTaskData:getTurnLimitScore(actId)
	local scoreLimit = 0
	local list = self:getTaskIdListScore(actId)
	for _, id in ipairs(list) do
		local cfg = self:getTaskConfig(id)
		if cfg then
			if self:getTaskTarget(id) > scoreLimit then
				scoreLimit = self:getTaskTarget(id)
			end
		end
	end
	return scoreLimit
end

-- 特定任务是否可领奖
function ActTaskData:getCanGetTaskRewardScore(actId, actTaskId)
	if not self:getHasGotTaskRewardScore(actId, actTaskId) and self:getTaskNumScore(actId, actTaskId) >= self:getTaskTarget(actTaskId) then
		return true
	else
		return false
	end
end

function ActTaskData:getCanGetTurnRewardScore(actId)
	actId = actId or self.actId
	-- 当前任务全部领取 未领取当前波次奖励
	if (self:getClaimedTurnScore(actId) < self:getTurnScore(actId)) then
		return true
	else
		return false
	end
end

-- 是否已经领完了全部的轮次奖励
function ActTaskData:getGotAllTurnRewardScore(actId)
	local claimed = self:getClaimedTurnScore(actId) or 0
	local maxTurn = self:getMaxTurnScore(actId)
	return claimed >= maxTurn
end

function ActTaskData:getTurnScore(actId)
	return self.taskDataScore[actId].round or 0
end

function ActTaskData:getClaimedTurnScore(actId)
	return self.taskDataScore[actId].round_claimed or 0
end

-- 成功领取轮次奖励
function ActTaskData:onGetTurnRewardScore(actId)
	self.taskDataScore[actId].round_claimed = (self.taskDataScore[actId].round_claimed or 0) + 1
	self:setDirty()
end

function ActTaskData:getCanAnyTaskCanGetRewardScore(actId)
	local taskList = self:getActTaskListScore(actId)
	for _, actTaskId in ipairs(taskList) do
		if self:getCanGetTaskRewardScore(actId, actTaskId) then
			return true
		end
	end
	return false
end

-- 领取任务成功
function ActTaskData:onTaskSuccessScore(actId, actTaskId)
	self.taskDataScore[actId] = self.taskDataScore[actId] or {}
	if actTaskId and actTaskId > 0 then
		self.taskDataScore[actId].claimedMap[actTaskId] = true
	else
		local list = self:getTaskIdListScore(actId)
		for _, id in ipairs(list) do
			if self:getCanGetTaskRewardScore(actId, id) then
				self.taskDataScore[actId].claimedMap[id] = true
			end
		end
	end
	-- 如果所有任务都领取了 增加轮次
	local list = self:getTaskIdListScore(actId)
	local isAllGot = true
	for _, id in ipairs(list) do
		if not self:getHasGotTaskRewardScore(actId, id) then
			isAllGot = false
			break
		end
	end
	if isAllGot then
		self.taskDataScore[actId].round = (self.taskDataScore[actId].round or 0) + 1
		self.taskDataScore[actId].claimedMap = {}
		self.taskDataScore[actId].score = self.taskDataScore[actId].score - self:getTurnLimitScore(actId)
	end
	self:setDirty()
end

-- 达标任务列表
function ActTaskData:getActTaskListScore(actId)
	local list = {}
	local cfg = self:getTaskConfig()
	for taskId, info in pairs(cfg) do
		if info.activity == actId and info.display == 1 then
			table.insert(list, taskId)
		end
	end
	table.sort(list, function(a, b)
		-- 已完成(可领奖) > 进行中 > 已完成(已领奖) > id
		local canGetA = self:getCanGetTaskRewardScore(actId, a)
		local canGetB = self:getCanGetTaskRewardScore(actId, b)
		if canGetA == canGetB then
			local gotA = self:getHasGotTaskRewardScore(actId, a)
			local gotB = self:getHasGotTaskRewardScore(actId, b)
			if gotA == gotB then
				return a < b
			else
				return gotB
			end
		else
			return canGetA
		end
	end)

	return list
end
--endregion

--region 任务配置
function ActTaskData:getTaskConfig(taskId)
	if taskId then
		return ConfigManager:getConfig("act_task")[taskId]
	else
		return ConfigManager:getConfig("act_task")
	end
end

-- 获取任务活动id
function ActTaskData:getActId(taskId)
	return self:getTaskConfig(taskId).activity
end

-- 获取任务类型
function ActTaskData:getTaskType(taskId)
	return self:getTaskConfig(taskId).type
end

-- 获取任务目标
function ActTaskData:getTaskTarget(taskId)
	return self:getTaskConfig(taskId).number_1
end

-- 获取任务奖励
function ActTaskData:getTaskReward(taskId)
	return self:getTaskConfig(taskId).reward
end

-- 获取任务展示要求
function ActTaskData:getTaskDisplay(taskId)
	return self:getTaskConfig(taskId).display
end

function ActTaskData:getTaskI18NName(taskId)
	return ConfigManager:getConfig("act_task")[taskId].desc
end
--endregion

return ActTaskData