package controller

import (
	"blazing/common/socket/errorcode"
	"blazing/logic/service"
	"blazing/logic/service/task"
	"blazing/modules/blazing/model"
	blservice "blazing/modules/blazing/service"
	"math/rand"
	"time"
)

/**
 * 接受任务
 */
func (h Controller) AcceptTask(data *task.AcceptTaskInboundInfo, c *service.Player) (result *task.AcceptTaskOutboundInfo, err errorcode.ErrorCode) {
	//isdaliy := false
	// if data.Head.CMD != 2201 { //判断是每日任务
	// 	//isdaliy = true
	// }

	if c.Info.TaskList[data.TaskId] == 0 {
		c.Info.TaskList[data.TaskId] = 1
	}

	result = &task.AcceptTaskOutboundInfo{}
	result.TaskId = data.TaskId
	return result, 0
}

/**
 * 更新任务步骤
 */
func (h Controller) AddTaskBuf(data *task.AddTaskBufInboundInfo, c *service.Player) (result *task.AddTaskBufOutboundInfo, err errorcode.ErrorCode) {
	// isdaliy := false
	// if data.Head.CMD != 2204 { //判断是每日任务
	// 	isdaliy = true
	// }
	result = &task.AddTaskBufOutboundInfo{}

	_, ok := blservice.NewUserService(c.Info.UserID).TaskInfo(data.TaskId)
	if ok {
		blservice.NewUserService(c.Info.UserID).TaskSet(data.TaskId, model.TaskInfo{
			Info: data.TaskList,
		})
	} else {
		blservice.NewUserService(c.Info.UserID).TaskADD(data.TaskId, model.TaskInfo{

			Info: data.TaskList,
		})
	}

	return &task.AddTaskBufOutboundInfo{}, 0
}

// 生成0-24的随机整数
func randInt0To24() int {
	// 初始化随机种子（仅需初始化一次）
	rand.Seed(time.Now().UnixNano())

	// 生成0-24的随机数（Intn(n)返回[0, n)的整数）
	return rand.Intn(25)
}

/**
 * 完成任务
 */
func (h Controller) Complete_Task(data *task.CompleteTaskInboundInfo, c *service.Player) (result *task.CompleteTaskOutboundInfo, err errorcode.ErrorCode) {

	if c.Info.TaskList[data.TaskId] != 1 { //如果任务没有接受或者已经完成Complete_Task

		return result, 0

	}
	c.Info.TaskList[data.TaskId] = 3
	result = &task.CompleteTaskOutboundInfo{}
	result.ItemList = make([]task.ItemInfo, 0)
	result.TaskId = data.TaskId
	if data.TaskId == 85 { //新手注册任务

		result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 100027, ItemCount: 1})
		result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 100028, ItemCount: 1})
		result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 500001, ItemCount: 1})
		result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 500502, ItemCount: 1})
		result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 500503, ItemCount: 1})
		// service.NewUserService(c.Info.UserID).ItemExec(func(tt []model.ItemE) []model.ItemE {

		// 	return tt

		// })
	}
	if data.TaskId == 86 { //新手注册任务

		r := model.GenPetInfo(1, []int{0, 31}, []int{0, 24}, []int{0}, []int{0}, []int{5})
		result.CaptureTime = r.CatchTime
		result.PetTypeId = r.ID
		blservice.NewUserService(c.Info.UserID).PetAdd(*r)
	}
	if data.TaskId == 87 { //新手注册任务

		result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 300001, ItemCount: 5})
		result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 300011, ItemCount: 5})

	}
	if data.TaskId == 88 { //新手注册任务

		result.ItemList = append(result.ItemList, task.ItemInfo{ItemId: 1, ItemCount: 5000})

	}
	return result, 0
}

/**
 * 获取任务状态
 */
func (h Controller) Get_Task_Buf(data *task.GetTaskBufInboundInfo, c *service.Player) (result *task.GetTaskBufOutboundInfo, err errorcode.ErrorCode) {
	info, _ := blservice.NewUserService(c.Info.UserID).TaskInfo(data.TaskId)
	result = &task.GetTaskBufOutboundInfo{}
	result.TaskId = data.TaskId
	result.TaskList = info.Info

	return result, 0
}

/**
 * 删除任务
 */
func (h Controller) Delete_Task(data *task.DeleteTaskInboundInfo, c *service.Player) (result *task.DeleteTaskOutboundInfo, err errorcode.ErrorCode) {

	// if data.Head.CMD == 2205 { //判断不是每日任务

	// } else {

	// }

	return &task.DeleteTaskOutboundInfo{}, 0
}
