package info

import (
	element "blazing/common/data/Element"
	"blazing/common/data/xmlres"
	"blazing/modules/blazing/model"
	"fmt"
	"math/rand"
	"sync"
	"unsafe"
)

// 战斗属性类型
type EnumAttrType int

// 精灵
const Pet_O_Ctx = "PET_O"
const Pet_T_Ctx = "PET_T"

// 这里获得攻击,防御,特工,特防,速度
// * battle_lv: atk(0), def(1), sp_atk(2), sp_def(3), spd(4), accuracy(5)
// * battle_attr: 0:hp, 1:atk, 2:def, 3:sp_atk, 4:sp_def, 5:spd
func (a *BattlePetEntity) Value(tt uint32) uint32 {

	offsetAtk := unsafe.Offsetof(a.Info.Attack) // c字段的偏移量（通常为4+16=20）
	// 2. 将结构体指针转换为原始内存地址（uintptr）
	baseAddr := uintptr(unsafe.Pointer(&offsetAtk))
	fmt.Println(*(*uint32)(unsafe.Pointer(&baseAddr)))
	addrA := unsafe.Pointer(baseAddr + 4*uintptr(tt)) //根据0是攻击
	offsetAtkP := unsafe.Offsetof(a.Prop.Attack)      // c字段的偏移量（通常为4+16=20）
	// 2. 将结构体指针转换为原始内存地址（uintptr）
	baseAddrp := uintptr(unsafe.Pointer(&offsetAtkP))
	fmt.Println(*(*uint32)(unsafe.Pointer(&offsetAtkP)))
	addrB := unsafe.Pointer(baseAddrp + 4*uintptr(tt)) //根据0是攻击
	fmt.Println(*(*uint32)(addrA))
	ret := uint32(calculateRealValue(int64(*(*uint32)(addrA)), int(*(*byte)(addrB))))
	return ret
}
func (a *BattlePetEntity) Speed() uint32 {
	return uint32(calculateRealValue(int64(a.Info.Speed), int(a.Prop.Speed)))

}
func (a *BattlePetEntity) Attack() uint32 {
	return uint32(calculateRealValue(int64(a.Info.Attack), int(a.Prop.Attack)))

}
func (a *BattlePetEntity) Defense() uint32 {
	return uint32(calculateRealValue(int64(a.Info.Defence), int(a.Prop.Defence)))

}
func (a *BattlePetEntity) SAttack() uint32 {
	return uint32(calculateRealValue(int64(a.Info.SpecialAttack), int(a.Prop.SpecialAttack)))

}
func (a *BattlePetEntity) SDefense() uint32 {
	return uint32(calculateRealValue(int64(a.Info.SpecialDefence), int(a.Prop.SpecialDefence)))

}
func (a *BattlePetEntity) Accuracy(b int64) uint32 {
	return uint32(calculateRealValue(b, int(a.Prop.Accuracy)))

}

type BattlePetEntity struct {
	xmlres.PetInfo
	Info model.PetInfo //通过偏移赋值

	statusConditions sync.Map              // key: StatusCondition, value: int (剩余回合)
	Skills           [4]*BattleSkillEntity // 技能槽（最多4个技能）
	Status           StatusDict            //精灵的状态
	//能力提升属性
	Prop PropDict
}

// 创建精灵实例
func CreateBattlePetEntity(info model.PetInfo, rand *rand.Rand) *BattlePetEntity {
	ret := &BattlePetEntity{}

	ret.PetInfo = xmlres.PetMAP[int(info.ID)] //注入精灵信息
	ret.Info = info
	for i := 0; i < 4; i++ {
		//todo  技能信息应该每回合进行深拷贝,保证每次的技能效果都是不一样的
		ret.Skills[i] = CreateBattleSkillWithInfinity(&info.SkillList[i], rand, ret)
	}

	return ret

}

// calculateRealValue 计算实际属性值根据状态变化计算实际值
// value 基础属性值
// stat 状态变化值（可正可负）
// 返回// 返回计算后的实际属性值，确保结果至少为1
func calculateRealValue(value int64, stat int) int64 {
	if stat == 0 {
		if value <= 0 {
			return 1
		}
		return value
	} else if stat > 0 {
		r := int64(float64(value) * (float64(stat+2) / 2.0))
		if r <= 0 {
			return 1
		}
		return r
	} else {
		r := int64(float64(value) * (2.0 / float64(2-stat)))
		if r <= 0 {
			return 1
		}
		return r
	}
}

func (u *BattlePetEntity) Type() *element.ElementCombination {
	var ff *element.ElementCombination
	for _, v := range xmlres.PetMAP {
		if v.ID == int(u.Info.ID) {
			ff, _ = element.NewElementCombination(v.Type)
		}

	}

	//todo  待实现获取精灵的类型

	return ff
}
