import CMath from "../../util/CMath";
import CUtil from "../../util/CUtil";
import { __SkillData } from "../data/sdata/SManage";
import BObject from "./bullet/BObject";
import EventButton from "./EventButton";
import FF from "./FF";
import { SpriteActionType, SpriteType } from "./object/FSprite";

const { ccclass, property } = cc._decorator;
/**
 * 控制按钮处理
 * 技能，事件，攻击按钮
 */
@ccclass
export default class FControl extends cc.Component {

    @property(FF)
    ff: FF = null;

    @property(EventButton)
    mEventButton: EventButton = null;//事件按钮

    @property(cc.Node)
    mShooting: cc.Node = null;//射击按钮

    @property(cc.Button)
    mBtSkill1: cc.Button = null;//技能1

    @property(cc.Label)
    mBtLabel1: cc.Label = null;//技能1倒计时

    @property(cc.Button)
    mBtSkill2: cc.Button = null;//技能2

    @property(cc.Label)
    mBtLabel2: cc.Label = null;//技能2倒计时

    @property(cc.Node)
    mBtSkillEffect: cc.Node = null;//技能2特效

    @property(cc.Sprite)
    mSkillIcon: cc.Sprite = null;//技能图标


    private _skillData1: __SkillData
    /**
     * 技能是否准备好
     */
    private skillOK1: boolean = true
    private lastTime1: number = 0;

    private skillOK2: boolean = true
    private lastTime2: number = 0;

    onLoad() {
        this.mEventButton.node.active = false;

        this.mShooting.on(cc.Node.EventType.TOUCH_START, this._touchShootStartEvent, this);
        this.mShooting.on(cc.Node.EventType.TOUCH_END, this._touchShootEndEvent, this);
        this.mShooting.on(cc.Node.EventType.TOUCH_CANCEL, this._touchShootEndEvent, this);
    }
    public updateSkill() {
        let attrData = this.ff.mainSprite.attrData
        this._skillData1 = this.ff.main.sManage.getSkillById(attrData.weaponSkill)
        if(this._skillData1){
            cc.resources.load('icon/skill/'+this._skillData1.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
                if(err){
                    cc.error(err);
                }else{
                   this.mSkillIcon.spriteFrame = spriteFrame;
                }
            } );
        }
        this.mBtSkill1.node.active = true;
        
        let role = this.ff.main.player.role
        if(role.openSkill){
            this.mBtSkill2.node.active = false;
        }else{
            this.mBtSkill2.node.active = false;
        }
    }
    /**
     * 显示技能2
     */
    public showSkill2(){
        // this.mBtSkill2.node.active = true;

        // this.mBtSkillEffect.active = true;

        // cc.tween(this.mBtSkillEffect).sequence(
        //     cc.delayTime(2),
        //     cc.destroySelf()
        // ).start()


    }

    /**
     * 设置事件按钮
     * @param spriteFrame 
     */
    public showEventBt(spriteFrame: cc.SpriteFrame, callback: () => void) {
        this.ff.mainSprite.setShooting(false);
        this.ff.mainSprite.status == SpriteType.NONE
        this.mEventButton.node.active = true
        this.mEventButton.mIcon.spriteFrame = spriteFrame
        this.mShooting.active = false
        this.mEventButton.setCallback(() => {
            callback()
        })
    }
    public closeEventBt() {
        this.mEventButton.node.active = false
        this.mShooting.active = true
        this.mEventButton.setCallback(null)
    }


    private _touchRunningStartEvent() {
        if (this.ff && this.ff.mainSprite) {
            this.ff.mainSprite.setRuning(true);
        }
    }
    private _touchRunningEndEvent() {
        if (this.ff && this.ff.mainSprite) {
            this.ff.mainSprite.setRuning(false);
        }
    }

    private _touchShootStartEvent() {
        if (this.ff && this.ff.mainSprite) {
            this.ff.mainSprite.setShooting(true);
        }
    }
    private _touchShootEndEvent() {
        if (this.ff && this.ff.mainSprite) {
            this.ff.mainSprite.setShooting(false);
        }
    }

    public onclickSkill1() {
        if (!this.skillOK1) {
            return
        }
        this.skillOK1 = false
        this.lastTime1 = CUtil.getNowTime()
        this.mBtSkill1.interactable = false
        this.mBtLabel1.node.active = true
        let target = this.ff.mainSprite.findEnemy(3000).sprite
        this.ff.mainSprite.skill1.exe(target,()=>{
            
        })
    }
   

    public onclickSkill2() {
        if (!this.skillOK2) {
            return
        }
        this.skillOK2 = false
        this.lastTime2 = CUtil.getNowTime()
        this.mBtSkill2.interactable = false
        this.mBtLabel2.node.active = true
        let target = this.ff.mainSprite.findEnemy(3000).sprite
        this.ff.mainSprite.skill1.exe(target,()=>{

        })
    }

    update(dt) {
        if (this._skillData1 && !this.skillOK1) {
            let curTime = CUtil.getNowTime()
            let x = curTime - this.lastTime1
            if (x >= this._skillData1.time) {
                this.skillOK1 = true
                this.mBtSkill1.interactable = true
                this.mBtLabel1.node.active = false
            } else {
                this.mBtLabel1.string = '' + (this._skillData1.time - x)
            }
        }

        if (this._skillData1 && !this.skillOK2) {
            let curTime = CUtil.getNowTime()
            let x = curTime - this.lastTime2
            if (x >= this._skillData1.time) {
                this.skillOK2 = true
                this.mBtSkill2.interactable = true
                this.mBtLabel2.node.active = false
            } else {
                this.mBtLabel2.string = '' + (this._skillData1.time - x)
            }
        }

    }

}
