package controller

import (
	"blazing/common/socket/errorcode"

	"blazing/logic/service"
	"blazing/logic/service/fight"
	"blazing/logic/service/fight/info"
	"blazing/modules/blazing/model"

	"github.com/jinzhu/copier"
)

func (h Controller) OnPlayerFightNpcMonster(data *fight.FightNpcMonsterInboundInfo, c *service.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {

	ttt := info.NoteReadyToFightInfo{

		FightId: 3,
	}

	copier.Copy(&ttt.OurInfo, &c.Info)
	len := len(c.Info.PetList)
	ttt.OurPetList = make([]info.ReadyFightPetInfo, len)
	for i := 0; i < len; i++ {

		err := copier.CopyWithOption(&ttt.OurPetList[i], &c.Info.PetList[i], copier.Option{IgnoreEmpty: true, DeepCopy: true})
		if err != nil {
			panic(err)
		}
	}

	ttt.OpponentInfo = info.FightUserInfo{UserID: 0}
	refpet := c.OgreInfo.Data[data.Number]
	if refpet.Id == 0 {

		return nil, errorcode.ErrorCodes.ErrPokemonNotExists
	}
	mo := model.GenPetInfo(
		int(refpet.Id), []int{0, 31},
		[]int{0, 24},
		[]int{0}, //野怪没特性
		[]int{int(refpet.Shiny)},
		[]int{int(refpet.Lv)})
	ttt.OpponentPetList = make([]info.ReadyFightPetInfo, 1)

	err1 := copier.CopyWithOption(&ttt.OpponentPetList[0], &mo, copier.Option{IgnoreEmpty: true, DeepCopy: true})
	if err1 != nil {
		panic(err)
	}

	if c.FightC != nil {
		return nil, errorcode.ErrorCodes.ErrOnlineOver6HoursCannotFight
	}
	ai := service.NewAI_player(*mo)
	service.NewFight(ttt, c, ai)

	c.FightC.OwnerID = c.Info.UserID

	return nil, -1
}

// 准备战斗
func (h Controller) OnReadyToFight(data *fight.ReadyToFightInboundInfo, c *service.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {

	c.FightC.ReadyFight(c)
	return nil, -1
}

// 接收战斗或者取消战斗的包
func (h Controller) OnPlayerHandleFightInvite(data *fight.HandleFightInviteInboundInfo, c *service.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {

	return nil, -1
}

// 使用技能包
func (h Controller) UseSkill(data *fight.UseSkillInboundInfo, c *service.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {
	c.FightC.UseSkill(c, int32(data.SkillId))
	return nil, 0
}

// 战斗逃跑
func (h Controller) Escape(data *fight.EscapeFightInboundInfo, c *service.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {

	c.FightC.Escape(c)
	return nil, 0
}

// 切换精灵
func (h Controller) ChangePet(data *fight.ChangePetInboundInfo, c *service.Player) (result *fight.NullOutboundInfo, err errorcode.ErrorCode) {

	c.FightC.ChangePet(c, int32(data.CatchTime))
	return nil, 0
}
