import { AudioMgr } from "../../../../main/ViewManage";
import CUtilTime from "../../../../util/CUtilTime";
import FF from "../../FF";
import { GroupType } from "../../object/FObject";
import FSprite, { SpriteActionType } from "../../object/FSprite";
import BaseEvent from "../base/BaseEvent";

/**
 * 泉水/吃苹果/吃蘑菇恢复气血
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FSpring extends BaseEvent {

    @property({
        type:cc.Prefab,
        displayName: '恢复的动画效果'
    })
    public effect:cc.Prefab = null;

    @property({
        type:cc.Font,
        displayName: '恢复的数字'
    })
    public effectNumber:cc.Font = null;

    @property({
        type:sp.Skeleton,
        displayName: '泉水动画'
    })
    public spine:sp.Skeleton = null;


    @property({
        displayName: '靠近的提示',
        type: cc.Node
    })
    icon: cc.Node = null;

    @property({
        displayName: '提示图标',
        type:cc.SpriteFrame
    })
    mTipsIcon: cc.SpriteFrame = null;

    @property({
        displayName: '采摘进度条',
        type:cc.ProgressBar
    })
    mProgressBar: cc.ProgressBar = null;

    @property(cc.Node)
    guides: Array<cc.Node> = [];//引导标识

    private isOpen = false
    onLoad(){
        super.onLoad()
        this.mProgressBar.node.active = false
        if (this.icon) {
            this.icon.active = false;
        }
    }
    onBegin(tag:number){
        if(this.isOpen){
            return
        }
        if(tag == 1){
            this.showButton();
        } else if (tag == 2) {
            if (this.icon) {
                this.icon.active = true;
            }
        }
        
    }
    onEnd(tag:number){
        if(tag == 1){
            this.closeOpt()
        } else if (tag == 2) {
            if (this.icon) {
                this.icon.active = false;
            }
        }
    }
    public showButton(){
        this.showOpt(this.mTipsIcon,()=>{
            this.isOpen = true
            this.huifu()
        })
    }

    private huifu(){
        if(this.spine){
            this.spine.setAnimation(0,'hit',true)
        }

        for (let i = 0; i < this.guides.length; i++) {
            const element = this.guides[i];
            element.destroy();
        }
        this.guides = [];
        this.closeOpt()
        // let cur = CUtilTime.getNowTime();
        // let sy = this.time - (cur - this.lastTime);
        // if(sy > 0){
        //     this.ff.main.showTips('泉水恢复中('+sy+'秒)');
        //     return;
        // }
        // this.lastTime = cur;

        this.ff.pauseSprite(true);
        
        this.ff.mainSprite.playAction(SpriteActionType.shiqu, true)
        this.playProgressBar()

    }

    private playProgressBar(){
        this.mProgressBar.progress = 0
        this.mProgressBar.node.active = true
        this.schedule(this.progressBarUpdate,0.1,11)
    }

    private progressBarUpdate(){
        this.mProgressBar.progress += 0.1
        if(this.mProgressBar.progress >= 1){
            this.ff.main.playerEffectByPath(AudioMgr.blood);
            this.unschedule(this.progressBarUpdate)
            this.mProgressBar.node.active = false
            this.ff.mainSprite.playAction(SpriteActionType.stand, true)
            this.addHP()
        }
    }

    private addHP(){
        this.ff.pauseSprite(false);
        if(this.spine){
            this.spine.setAnimation(0,'dead',false)
            this.spine.setCompleteListener(() => {
                this.spine.setCompleteListener(null);
            });
        }

        cc.tween(this.node).sequence(
            cc.callFunc(()=>{
                this.playEffect();
            }),
            cc.delayTime(0.5,),
            cc.callFunc(()=>{
                this.playEffect();
            }),
            cc.delayTime(0.5,),
            cc.callFunc(()=>{
                this.playEffect();
            }),
        ).start();
    }

    /**
     * 播放特效动画
     * @param sprite 
     */
    private playEffect(){
        let nodes = this.ff.getGroupBy(GroupType.A);
        for (let i = 0; i < nodes.length; i++) {
            const element = nodes[i];
            if(element && element.hp > 0){
                let sprite = element.getComponent(FSprite);
                let addHP = Math.floor(sprite.attrData.hp/5);
                sprite.hp += addHP;
                if(sprite.hp > sprite.attrData.hp){
                    sprite.hp = sprite.attrData.hp;
                }
                sprite.updatePanel();
    
                this.playNumber(sprite,addHP);
            }
        }
    }
    
    private playNumber(sprite:FSprite,addHP){
        let nodeEffect = cc.instantiate(this.effect);
        nodeEffect.parent = sprite.node;
        nodeEffect.group = sprite.node.group;


        let node: cc.Node = new cc.Node('hp');
        node.group = 'map';
        let lable: cc.Label = node.addComponent(cc.Label);

        lable.fontSize = 26;
        lable.font = this.effectNumber;
        
        lable.string = '.'+addHP;

        node.x = sprite.node.x;
        node.y = sprite.node.y + 40;
        
        this.ff.mMap.node.addChild(node);

        cc.tween(node).sequence(
            cc.spawn(
                cc.moveBy(1, cc.v2(0, 60)).easing(cc.easeSineOut()),
                cc.fadeOut(1)
            ),
            cc.callFunc(()=>{
                node.destroy();
                nodeEffect.destroy();
            })
        ).start();
    }
}
