import { _decorator, Component, Node, sp, UITransform, Vec2, Vec3 ,Animation, Color, tween} from 'cc';
import { ObjectUtil } from '../../../../framework/util/ObjectUtil';
import { BattleNodeBase } from './BattleNodeBase';
import { RenderPriority } from '../data/BattleEnum';
const { ccclass, property } = _decorator;

// 敌人基类
@ccclass('Enemy')
export class Enemy extends BattleNodeBase {


    @property({ type: sp.Skeleton, tooltip: '骨骼动画' })
    modelSpine: sp.Skeleton = null;
    @property({ type: Node, tooltip: '血条' })
    lifeNode: Node = null;


    private _bHurtAnimation: boolean = false;
    life: number = 0;
    lifeMax: number = 0;
    speedVector: Vec2 = new Vec2(0, 0);
    
    hurtColor = new Color(255,100,100,255)
    hurtTime = 0
    bStand: boolean = false;

    dieCallback: Function = null;

    protected onLoad(): void {
        this.priority = RenderPriority.Enemy;
        this.modelSpine.setCompleteListener(this.actionComplete.bind(this))

    }

    start() {
    
    }
    //用自定义更新,不用系统更新
    battleUpdate(deltaTime: number) {
        if(this.hurtTime > 0){
            this.hurtTime -= deltaTime
            if(this.hurtTime <= 0){
                this.modelSpine.color = Color.WHITE;
                this._bHurtAnimation = false
            }
        }

        if((!this.bStand) && (this.life > 0)){
            this.node.position = new Vec3(this.node.position.x+this.speedVector.x*deltaTime,this.node.position.y+this.speedVector.y*deltaTime,0)
        }
    }


    resetData(typeID:number,ID:number,pos:Vec3,life:number,lifeMax:number) {
        this.typeID = typeID;
        this.ID = ID;
        this.node.position = pos
        this.lifeNode.scale = new Vec3(1,1,1)
        this.life = life;
        this.lifeMax = lifeMax;
        this.bStand = false;
        this.dieCallback = null;
        this.updateLife()
    }
    
    startMove(animationName:string){
        this.node.active = true;
        this.modelSpine.setAnimation(0, 'idle', true);
        this.modelSpine.color = Color.WHITE;
        // this.lineAnimation.play(animationName);
    }

    hurt(value:number) {
        this.life -= value;
        this.hurtTime = 0.5
        this.updateLife()
        if(this._bHurtAnimation) return 
        this._bHurtAnimation = true
        // this.modelSpine.setAnimation(0, 'hurt', false);
        this.modelSpine.color = this.hurtColor;
    }
    stand() {
        this.bStand = true;
        if(!this.modelSpine.node.active) return;
        this._bHurtAnimation = false;
        this.modelSpine.setAnimation(0, 'idle', true);
        this.modelSpine.color = Color.WHITE;
        // this._attackCallback = null;
    }

    updateLife(){
        if(this.life < 0) {
            this.lifeNode.scale = new Vec3(0,1,1);
        }
        else {
            this.lifeNode.scale = new Vec3(this.life / this.lifeMax,1,1);
        }
    }

    clearData() {
        this.node.active = false
    }
    
    die(callback?:Function) {
        
        this.dieCallback = callback;
        tween(this.node).delay(0.5).call(() =>  {
            this.node.active = false
            this.modelSpine.color = Color.WHITE;
            this.dieCallback && this.dieCallback()
        }).start()
    }




    actionComplete(trackEntry:sp.spine.TrackEntry){
        // console.log("动作完成:",trackEntry.animation.name,this._posID)
        if (trackEntry.animation.name && trackEntry.animation.name.includes("hurt")) {
            this._bHurtAnimation = false;
            this.modelSpine.addAnimation(0, 'idle', true);
            this.modelSpine.color = Color.WHITE;
        } 
    }

    

}