package effect

import (
	"blazing/common/utils"
	"blazing/logic/service/fight/info"
	"blazing/logic/service/fight/input"
	"blazing/logic/service/fight/node"
)

// 施加一个基类effect
type Effect0 struct {
	node.EffectNode
}

// 技能命中计算
func (this *Effect0) IsHit(opp *input.Input, skill *info.SkillEntity) {
	skill.AttackTimeC(int(opp.GetProp(5, true))) //计算命中
}

// 比如xx技能无效
func (this *Effect0) UseSkill(opp *input.Input) bool {
	return true
}

func (e *Effect0) TurnEnd(opp *input.Input) {
	e.Input.AttackValue.RemainHp = int32(e.Input.CurrentPet.Info.Hp)

	//	ret.FAttack.LostHp = uint32(f.First.GetDamageEffect(0).Stack()) //先手方造成血量
	//opp.AttackValue.RemainHp = int32(opp.CurrentPet.Info.Hp)
	//ret.SAttack.LostHp = uint32(f.Second.GetDamageEffect(0).Stack()) //后手方造成血量

}

// 使用技能时
func (e *Effect0) OnSkill(opp *input.Input, skill *info.SkillEntity) {

	e.Input.AttackValue.LostHp = uint32(e.Input.CalculatePower(opp, skill).IntPart())

	e.Input.Exec(func(t input.Effect) bool { //计算暴击率加成

		t.IsCrit(opp, skill)
		e.Input.AttackValue.IsCritical = skill.Crit
		return e.Input.AttackValue.IsCritical == 0
	})
	if e.Input.AttackValue.IsCritical == 1 {
		e.Input.AttackValue.LostHp = e.Input.AttackValue.LostHp * 2

	}
	//这里注释掉，扣血在对方回合
	if uint32(e.Input.AttackValue.LostHp) > opp.CurrentPet.Info.Hp {
		opp.CurrentPet.Info.Hp = 0
	} else {
		opp.CurrentPet.Info.Hp = opp.CurrentPet.Info.Hp - e.Input.AttackValue.LostHp
	}

}
func (this *Effect0) IsCrit(opp *input.Input, skill *info.SkillEntity) {
	skill.Crit = 0
	CritRate := utils.Max(skill.CritRate, 1)
	CritRateR := this.Input.FightC.GetRand().Int31n(16)
	//CritAtkFirst: 先出手时必定致命一击; 默认: 0
	if skill.CritAtkFirst != 0 && this.Input.First {
		CritRate = 16
	}
	//CritAtkSecond: 后出手时必定致命一击; 默认: 0
	if skill.CritAtkSecond != 0 && !this.Input.First {
		CritRate = 16
	}
	// CritSelfHalfHp: 自身体力低于一半时必定致命一击; 默认: 0
	if skill.CritSelfHalfHp != 0 && (this.Input.CurrentPet.HP < int(this.Input.CurrentPet.Info.MaxHp)/2) {
		CritRate = 16
	}
	//  CritFoeHalfHp: 对方体力低于一半时必定致命一击; 默认: 0
	if skill.CritSelfHalfHp != 0 && (opp.CurrentPet.HP < int(opp.CurrentPet.Info.MaxHp)/2) {
		CritRate = 16
	}

	//todo 暴击伤害
	if CritRateR <= int32(CritRate) {
		skill.Crit = 1
	}

}
func init() {
	input.InitDamageEffect(0, &Effect0{})

}
