package task

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log-server/global"
	"log-server/model/common/response"
	"log-server/model/task/request"
	taskResponse "log-server/model/task/response"
	"log-server/utils"
	"net/http"
	"time"
)

type GameTaskApi struct {
}

// @Tags gameTask
// @Summary 创建游戏任务
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GameTaskRequest true
// @Success 200 {object} response.Response{msg=string} "创建游戏任务"
// @Router /gameTask/create [post]
func (s *GameTaskApi) CreateGameTask(c *gin.Context) {
	var api request.GameTaskRequest
	_ = c.ShouldBindJSON(&api)
	if err := utils.Verify(api, utils.GameTaskVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := taskService.CreateGameTask(api); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败 "+err.Error(), c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags gameTask
// @Summary 游戏任务列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetCardListRequest true "游戏任务列表"
// @Success 200 {object} response.Response{data=response.GetGameTaskListReply,msg=string} "分页游戏任务列表,返回包括列表,总数,页码,每页数量"
// @Router /gameTask/getGameTaskList [post]
func (s *GameTaskApi) GetGameTaskList(c *gin.Context) {
	var paramsInfo request.GetGameTaskListRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, option, total, err := taskService.GetGameTaskInfoList(paramsInfo.GameTask, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败"+err.Error(), c)
	} else {
		response.OkWithDetailed(taskResponse.GameTaskPageResult{
			List:       list,
			OptionList: option,
			Total:      total,
			Page:       paramsInfo.Page,
			PageSize:   paramsInfo.PageSize,
		}, "获取成功", c)
	}
}

//获取启用状态的任务
func (s *GameTaskApi) GetUsedGameTaskList(c *gin.Context) {
	var paramsInfo request.GetGameTaskListRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, option, total, err := taskService.GetUsedGameTaskInfoList(paramsInfo.GameTask, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败"+err.Error(), c)
	} else {
		response.OkWithDetailed(taskResponse.GameTaskPageResult{
			List:       list,
			OptionList: option,
			Total:      total,
			Page:       paramsInfo.Page,
			PageSize:   paramsInfo.PageSize,
		}, "获取成功", c)
	}
}


// @Tags gameTask
// @Summary 根据id获取游戏任务信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id获取游戏任务信息"
// @Success 200 {object} response.Response{data=systemRes.GameTaskRequest} "根据id获取游戏任务信息,返回包括游戏任务信息详情"
// @Router /gameTask/getGameTaskById [post]
func (s *GameTaskApi) GetGameTaskById(c *gin.Context) {
	var idInfo request.GetById
	_ = c.ShouldBindJSON(&idInfo)
	if err := utils.Verify(idInfo, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	api, err := taskService.GetGameTaskById(idInfo.ID)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	} else {
		response.OkWithDetailed(api, "获取成功", c)
	}
}

// @Tags GameTask
// @Summary 游戏任务信息状态
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.UpdateGameTaskStatusRequest true "id, 状态"
// @Success 200 {object} response.Response{msg=string} "修改游戏任务信息状态"
// @Router /gameTask/statusOperation [post]
func (s *GameTaskApi) StatusOperation(c *gin.Context) {
	var paramsInfo request.UpdateGameTaskStatusRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if paramsInfo.TaskId == 0 {
		response.FailWithMessage("Id不能为空", c)
		return
	}
	if paramsInfo.Status == 0 {
		response.FailWithMessage("状态值不能为空", c)
		return
	}
	if err := taskService.UpdateGameTaskStatus(paramsInfo); err != nil {
		global.GVA_LOG.Error("修改失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("修改成功", c)
	}
}

// @Tags gameTask
// @Summary 更新游戏任务
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GameTaskRequest true
// @Success 200 {object} response.Response{msg=string} "更新游戏任务"
// @Router /gameTask/update [post]
func (s *GameTaskApi) UpdateGameTask(c *gin.Context) {
	var api request.GameTaskRequest
	_ = c.ShouldBindJSON(&api)
	if err := utils.Verify(api, utils.GameTaskVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := taskService.UpdateGameTask(api); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败 "+err.Error(), c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags gameTask
// @Summary 删除gameTask
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body task.GameTask true "ID"
// @Success 200 {object} response.Response{msg=string} "删除gameTask"
// @Router /gameTask/delete [post]
func (s *GameTaskApi) DeleteGameTask(c *gin.Context) {
	var api request.GameTaskRequest
	_ = c.ShouldBindJSON(&api)
	if err := utils.Verify(api, utils.TaskIdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := taskService.DeleteGameTask(api); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败 "+err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags gameTask
// @Summary 游戏任务列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetCardListRequest true "游戏任务列表"
// @Success 200 {object} response.Response{data=response.GetGameTaskListReply,msg=string} "分页游戏任务列表,返回包括列表,总数,页码,每页数量"
// @Router /gameTask/getGameTaskTargetList [post]
func (s *GameTaskApi) GetGameTaskTargetList(c *gin.Context) {
	var paramsInfo request.GetGameTaskListTargetRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := taskService.GetGameTaskTargetInfoList(paramsInfo.GameTargetCompleteRequest, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc, false); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     paramsInfo.Page,
			PageSize: paramsInfo.PageSize,
		}, "获取成功", c)
	}
}

// @Tags gameTask
// @Summary 腾讯游戏任务列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetCardListRequest true "腾讯游戏任务列表"
// @Success 200 {object} response.Response{data=response.GetGameTaskListReply,msg=string} "分页游戏任务列表,返回包括列表,总数,页码,每页数量"
// @Router /gameTask/getGameTxTaskList [post]
func (s *GameTaskApi) GetGameTxTaskList(c *gin.Context) {
	var paramsInfo request.GetGameTaskListTargetRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := taskService.GetGameTaskTargetInfoList(paramsInfo.GameTargetCompleteRequest, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc, true); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     paramsInfo.Page,
			PageSize: paramsInfo.PageSize,
		}, "获取成功", c)
	}
}

// @Tags gameTask
// @Summary 根据id获取游戏任务信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id获取游戏任务目标信息"
// @Success 200 {object} response.Response{data=response.GetGameTargetComplete} "根据id获取游戏任务信息,返回包括游戏任务信息详情"
// @Router /gameTask/getGameTaskTargetById [post]
func (s *GameTaskApi) GetGameTaskTargetById(c *gin.Context) {
	var idInfo request.GetGameTaskTargetByIdRequest
	_ = c.ShouldBindJSON(&idInfo)
	if err := utils.Verify(idInfo, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	date := time.Now().Format("2006-01-02")
	if idInfo.CreateDate != date {
		response.FailWithMessage("只能修改当天的任务", c)
		return
	}
	api, err := taskService.GetGameTaskTargetById(idInfo.ID, date)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	} else {
		response.OkWithDetailed(api, "获取成功", c)
	}
}

// @Tags gameTask
// @Summary 更新游戏任务目标
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GameTaskRequest true
// @Success 200 {object} response.Response{msg=string} "更新游戏任务目标"
// @Router /gameTask/updateTarget [post]
func (s *GameTaskApi) UpdateGameTaskTarget(c *gin.Context) {
	var api request.UpdateGameTaskTargetRequest
	_ = c.ShouldBindJSON(&api)
	if err := utils.Verify(api, utils.UpdateTargetVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := taskService.UpdateGameTaskTarget(api); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败 "+err.Error(), c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags gameTask
// @Summary 群控任务目标
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data Query new_date true
// @Success 200 {object} []control.TargetStatisticsReply "更新游戏任务目标"
// @Router /gameTask/targetStatistics [get]
func (s *GameTaskApi) TargetStatistics(c *gin.Context) {
	date := c.Query("new_date")
	if date == "" {
		response.FailWithMessage("获取失败!参数错误!", c)
		return
	}
	if list, err := taskService.TargetStatistics(date); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	} else {
		c.JSON(http.StatusOK, response.ControlResult{
			Data:    list,
			Message: "获取成功",
			Code:    1000,
			Date:    date,
		})
		return
	}
}

// @Tags gameTask
// @Summary 数优每日统计
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data Query create_date true
// @Success 200 {object} []response.GameTargetStatistics "数优每日统计数据"
// @Router /gameTask/everyDayStatistics [post]
func (s *GameTaskApi) EveryDayStatistics(c *gin.Context) {
	var paramsInfo request.GameTaskStatisticsRequest
	_ = c.ShouldBindJSON(&paramsInfo)

	if data, err := taskService.DayStatisticsData(paramsInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	} else {

		response.OkWithDetailed(response.PageResult{
			List:     data,
			Total:    0,
			Page:     1,
			PageSize: 10,
		}, "获取成功", c)
	}
}

// @Tags gameTask
// @Summary 数优每月统计
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data Query create_date true
// @Success 200 {object} []response.GameTargetStatistics "数优每月统计数据"
// @Router /gameTask/monthStatistics [post]
func (s *GameTaskApi) MonthStatistics(c *gin.Context) {
	if data, err := taskService.MonthStatisticsData(); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	} else {

		response.OkWithDetailed(response.PageResult{
			List:     data,
			Total:    0,
			Page:     1,
			PageSize: 10,
		}, "获取成功", c)
	}
}

// @Tags gameTask
// @Summary 数优游戏统计
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data Query create_date true
// @Success 200 {object} []response.GameTargetStatistics "数优游戏统计数据"
// @Router /gameTask/gameStatistics [post]
func (s *GameTaskApi) GameStatistics(c *gin.Context) {
	var paramsInfo request.CreateDateReply
	_ = c.ShouldBindJSON(&paramsInfo)
	if data, err := taskService.GameStatisticsData(paramsInfo.CreateDate); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	} else {

		response.OkWithDetailed(response.PageResult{
			List:     data,
			Total:    10,
			Page:     1,
			PageSize: 10,
		}, "获取成功", c)
	}
}

// @Tags gameTask
// @Summary 负责人端口游戏列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data Query create_date true
// @Success 200 {object} []response.GameTargetStatistics "负责人端口游戏列表"
// @Router /gameTask/gameList [post]
func (s *GameTaskApi) GameList(c *gin.Context) {
	var paramsInfo request.GameListRequest
	_ = c.ShouldBindJSON(&paramsInfo)
	if data, err := taskService.GameStatisticsList(paramsInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     data,
			Total:    10,
			Page:     1,
			PageSize: 10,
		}, "获取成功", c)
	}
}

// @Tags gameTask
// @Summary 重置付费
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GameTaskRequest true
// @Success 200 {object} response.Response{msg=string} "更新游戏任务目标"
// @Router /gameTask/taskResetFee [post]
func (s *GameTaskApi) TaskResetFee(c *gin.Context) {
	var api request.GameTaskRequest
	_ = c.ShouldBindJSON(&api)
	if err := utils.Verify(api, utils.TaskIdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := taskService.TaskResetFee(api); err != nil {
		global.GVA_LOG.Error("重置付费失败!", zap.Error(err))
		response.FailWithMessage("重置付费失败 "+err.Error(), c)
	} else {
		response.OkWithMessage("重置付费成功", c)
	}
}

// @Tags gameTask
// @Summary 根据id获取游戏可付费账号
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id获取可付费账号列表"
// @Success 200 {object} response.Response{data=response.GetGameTargetComplete} "根据id获取可付费账号列表"
// @Router /gameTask/getFeeAccountList [post]
func (s *GameTaskApi) GetFeeAccountList(c *gin.Context) {
	var idInfo request.GetGameTaskTargetByIdRequest
	_ = c.ShouldBindJSON(&idInfo)
	if err := utils.Verify(idInfo, utils.IdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	date := time.Now().Format("2006-01-02")
	if idInfo.CreateDate != date {
		response.FailWithMessage("只能获取当天的任务", c)
		return
	}
	api, err := taskService.GetFeeAccountList(idInfo.ID)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	} else {
		response.OkWithDetailed(api, "获取成功", c)
	}
}

// @Tags gameTask
// @Summary 导出任务目标Excel
// @Security ApiKeyAuth
// @accept application/json
// @Produce  application/octet-stream
// @Param data body request.GameTargetCompleteRequest true "导出Excel文件信息"
// @Success 200
// @Router /gameTask/taskTargetExport [post]
func (e *GameTaskApi) TaskTargetExport(c *gin.Context) {
	var excelInfo request.ExcelInfo
	_ = c.ShouldBindJSON(&excelInfo)
	paramsInfo := excelInfo.InfoList
	paramsInfo.PageSize = 300
	paramsInfo.Page = 1
	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, _, err := taskService.GetGameTaskTargetInfoList(paramsInfo.GameTargetCompleteRequest, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc, false)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败 "+err.Error(), c)
	}
	filePath := global.GVA_CONFIG.Excel.Dir + excelInfo.FileName
	err = taskService.TaskStatisticsList2Excel(list, filePath)
	if err != nil {
		global.GVA_LOG.Error("转换Excel失败!", zap.Error(err))
		response.FailWithMessage("转换Excel失败", c)
		return
	}
	c.Writer.Header().Add("success", "true")
	c.File(filePath)
}
