import { playDragonBoneAnimation, getSpriteFrimeByUrl, playAudioByUrl,setSpriteWH } from "../script/util";

cc.Class({
	extends: cc.Component,
    name: "WordKind",
	properties: {
        layout:{
            default:null,
            type: cc.Layout,
        },
        btn_sound:{
            default:null,
            type:cc.Button,
        },
        tips_text_bg:{
            default:null,
            type:cc.Node,
        },
        tips_pic_bg:{
            default:null,
            type:cc.Node,
        },
        show_node:{
            default:null,
            type:cc.Node,
        },

        animate_node:{
            default:null,
            type:cc.Node,
        },
    },

    onLoad(){
        this.btn_sound.node.on('click', this.playAudio.bind(this));
        this.tips_text_bg.on('click', this.playAudio.bind(this));
        this.tips_pic_bg.on('click', this.playAudio.bind(this));
        this.initData();
    },

    playAudio(){
        if (this._audio && this._audio!=""){
            playAudioByUrl(this._audio);
        }
    },

    init(data){
        this._audio = data.audio;
        this.btn_sound.node.active = false;
        this.tips_text_bg.active = false;
        this.tips_pic_bg.active = false;

        if (data.pic != ""){
            getSpriteFrimeByUrl(data.pic, (spr)=>{
                this.setWordPic(spr);
                this.tips_pic_bg.active = true;
            });
        }
        else if (data.text != ""){
            this.setWordText(data.text);
            this.tips_text_bg.active = true;
        }
        else{
            this.btn_sound.node.active = true;
        }
        this.kind_id = data.kind;
        this.layout.node.removeAllChildren();
    },

    initData(){
        this._word_text = this.tips_text_bg.getChildByName('word_text').getComponent(cc.Label);
        this._word_pic = this.tips_pic_bg.getChildByName('word_pic').getComponent(cc.Sprite);
    },

    setWordText(str){
        if (!this._word_text){
            this.initData();
        }
        this._word_text.string = str;
    },

    setWordPic(spr){
        if (!this._word_pic){
            this.initData();
        }
        //this._word_pic.spriteFrame = spr;
        setSpriteWH(this._word_pic, spr, 188, 122);
    },

    setWidth(mode){
        this.node.width = [479, 368, 273][mode];
        this.node.height = 284;
    },

    playAnimation(cb){
        this.animate_node.active = true;
        playDragonBoneAnimation(this.animate_node, 'newAnimation', 1, cb);
    },

    showCorrect(ot, cb){
        ot.x = 0;
        ot.y = 0;
        let item = ot.getComponent(cc.js.getClassByName('WordOption'));
        this.show_node.addChild(ot);
        item.adjustPicSize();
        this.show_node.active = true;
        this.playAnimation(()=>{
            this.show_node.removeAllChildren();
            let node = cc.instantiate(cc.find('res/option_item'));
            let option = node.getComponent(cc.js.getClassByName('WordOption'));
            option.copyValues(item);
            option.pic.width = item.pic.width;
            option.pic.height = item.pic.height;
            this.layout.node.addChild(node);
            option.adjustPicSize();
            option.finished();
            let sr = 2/3;
            node.width*=sr;
            node.height*=sr;
            node.scale *= sr;
//            node.opacity = 0;
            option.adjustPicSize();
            // cc.tween(node)
            // .to(0.5, {opacity:255})
            // .call(()=>{
            //     option.adjustPicSize();
            //     console.log(option);
            //     cb && cb();
            //     })
            // .start();
            this.show_node.active = false;
            this.animate_node.active = false;
        });
    }


});