// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
import SceneComponent from "../../scene/duiduipeng";

@ccclass
export default class card extends cc.Component {
    @property(cc.Node)
    iconN: cc.Node = null;
    @property(cc.Node)
    backN: cc.Node = null;
    @property(cc.Label)
    wordLabel: cc.Label = null;
    @property(dragonBones.ArmatureDisplay)
    dragon: dragonBones.ArmatureDisplay = null;
    mainControl:SceneComponent=null;
    data=null

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
      
    }
    initData(data,mainControl){
        this.data=data;
        this.mainControl=mainControl
        this.node.off("click")
        this.node.on("click",(e)=>{
            console.log("e=====",e)
           this.mainControl.clickCard(e.node,this.data);
        })
        if(this.data.type=="img"){
            this.iconN.active=true;
            this.iconN.scaleX=1
            this.mainControl.getSprForNodeByUrl(this.data.image,this.iconN,()=>{
                if(this.iconN.width/this.iconN.height>200/240){
                    this.iconN.scale=200/this.iconN.width
                }else{
                    this.iconN.scale=240/this.iconN.height
                }
            });
            this.wordLabel.string=""
        }else{
            this.wordLabel.node.scaleX=1
            this.wordLabel.string=this.data.text
        }
     
    }
    updateBtnAble(interactable){
        this.node.getComponent(cc.Button).interactable=interactable;
    }
    playDragon(cb){
        return new Promise((resolve,reject)=>{
            this.dragon.node.active=true;
            this.dragon.node.scaleX=this.node.scaleX;
            this.dragon.playAnimation("newAnimation", 1)
            this.dragon.on(dragonBones.EventObject.COMPLETE,()=>{
                if(cb){
                    cb();
                }
                resolve(null)
            })
        })      
    } 
    doHide(cb){
        return new Promise((resolve,reject)=>{
            this.node.stopAllActions()
            cc.tween(this.node)
            .to(0.3,{scale:0})
            .call(()=>{
                if(cb)cb()
                resolve(null)
                this.node.destroy();
            })
            .start();
        })      
    }
    doFlip(cb=null){
        this.node.stopAllActions();
        if(this.node.scaleX>0){
            cc.tween(this.node)
            .to(0.25,{scaleX:0})
            .call(()=>{
                this.iconN.scaleX=-this.iconN.scaleX
                this.wordLabel.node.scaleX=-1
                this.backN.active=false
            })
            .to(0.25,{scaleX:-1})
            .call(()=>{
                if(cb)cb()
            })
            .start();
        }else{
            cc.tween(this.node)
            .to(0.25,{scaleX:0.3})
            .call(()=>{
                this.iconN.scaleX=-this.iconN.scaleX
                this.wordLabel.node.scaleX=1
                this.backN.active=true
            })
            .to(0.25,{scaleX:1})
            .call(()=>{
                if(cb)cb()
            })
            .start();
        }


    }
    asyncDoFlip(cb){
        return new Promise((resolve, reject)=>{
            //做一些异步操作
            this.node.stopAllActions();
            if(this.node.scaleX>0){
                cc.tween(this.node)
                .to(0.25,{scaleX:0})
                .call(()=>{
                    this.iconN.scaleX=-this.iconN.scaleX
                    this.wordLabel.node.scaleX=-1
                    this.backN.active=false
                })
                .to(0.25,{scaleX:-1})
                .call(()=>{
                    if(cb)cb()
                    resolve(null);
                })
                .start();
            }else{
                cc.tween(this.node)
                .to(0.25,{scaleX:0})
                .call(()=>{
                    this.iconN.scaleX=-this.iconN.scaleX
                    this.wordLabel.node.scaleX=1
                    this.backN.active=true
                })
                .to(0.25,{scaleX:1})
                .call(()=>{
                    if(cb)cb()
                    resolve(null);
                })
                .start();
            }
    
        });
    }


    // update (dt) {}
}
