package controller

import (
	"blazing/common/socket/errorcode"

	"blazing/logic/service/player"
	"blazing/logic/service/task"
	"blazing/modules/blazing/model"
)

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

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

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

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

	c.Service.Task(data.TaskId, func(te *model.TaskEX) bool {
		te.Data = data.TaskList
		return true
	})

	return &task.AddTaskBufOutboundInfo{}, 0
}

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

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

		return result, 0

	}
	c.Info.TaskList[data.TaskId-1] = 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})

	}
	if data.TaskId == 86 { //新手注册任务
		var petid int
		switch data.OutState {

		case 1:
			petid = 1

		case 2:
			petid = 7

		case 3:
			petid = 4
		}
		r := model.GenPetInfo(petid, 31, -1, 0, 0, 5)
		result.CaptureTime = r.CatchTime
		result.PetTypeId = r.ID
		c.Service.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})

	}
	var ttt []model.SingleItemInfo
	for _, v := range result.ItemList {
		ttt = append(ttt, model.SingleItemInfo{ItemId: v.ItemId, ItemCnt: v.ItemCount})
	}
	c.ItemAdd(ttt)
	return result, 0
}

/**
 * 获取任务状态
 */
func (h Controller) Get_Task_Buf(data *task.GetTaskBufInboundInfo, c *player.Player) (result *task.GetTaskBufOutboundInfo, err errorcode.ErrorCode) {
	result = &task.GetTaskBufOutboundInfo{}
<<<<<<< HEAD
	c.Service.Task(data.TaskId, func(te *model.TaskEX)bool {
		result.TaskId = te.TaskID
		result.TaskList = te.Data
		return false
	})
=======
	result.TaskId = data.TaskId
	result.TaskList = info.Data
>>>>>>> ea1148039e70e6b1dadf6c892b49e547d0f10ac7

	return result, 0
}

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

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

	// } else {

	// }

	return &task.DeleteTaskOutboundInfo{}, 0
}
