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 (e *Effect0) TurnEnd(opp *input.Input) {
	e.Input.AttackValue.RemainHp = int32(e.Input.CurrentPet.Info.Hp)

}

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

		e.Stack(int(e.Input.CalculatePower(opp, skill).IntPart()))
	})

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

		e.Input.AttackValue.IsCritical = skill.Crit
		return e.Input.AttackValue.IsCritical == 0
	})
	if e.Input.AttackValue.IsCritical == 1 {
		e.Input.Exec(func(t input.Effect) bool { //加伤

			t.AddZone(e.Input, &input.EffectID{
				ID:     1,
				Effect: e,
			})
			return true
		})
		e.Input.Exec(func(t input.Effect) bool { //乘伤

			t.MulZone(e.Input, &input.EffectID{
				ID:     1,
				Effect: e,
			})
			return true
		})

		e.Stack(e.Stack() * 2)
		if e.MaxStack != 0 && e.Stack() > e.MaxStack { //限制最大伤害
			e.Stack(e.MaxStack)
		}
	}

}
func (this *Effect0) BeforHit(opp *input.Input, skill *info.SkillEntity) {
	skill.AttackTimeC(int(opp.GetProp(5, true))) //计算命中
	skill.Crit = 0
	CritRate := utils.Max(skill.CritRate, 1)

	//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 t, _, _ := this.Input.Player.Roll(625*CritRate, 10000); t {
		skill.Crit = 1
	}

}

// 受击触发
func (e *Effect0) AfterAttacked(opp *input.Input, skill *info.SkillEntity) {

	e.Input.Exec(func(t input.Effect) bool { //加伤

		t.AddZone(opp, &input.EffectID{
			ID:     1,
			Effect: e,
		})
		return true
	})
	e.Input.Exec(func(t input.Effect) bool { //乘伤

		t.MulZone(opp, &input.EffectID{
			ID:     1,
			Effect: e,
		})
		return true
	})

	//在这里修改伤害

	if uint32(opp.AttackValue.LostHp) > e.Input.CurrentPet.Info.Hp {
		//这里其实是受到致死伤害
		//然后先触发死亡效果，消除所有buff
		//然后触发回神效果
		e.Input.CurrentPet.Info.Hp = 0
	} else {
		e.Input.CurrentPet.Info.Hp = e.Input.CurrentPet.Info.Hp - opp.AttackValue.LostHp
	}
}

func init() {
	input.InitEffect(input.EffectType.Damage, 0, &Effect0{})

}
