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

cc.Class({
	extends: cc.Component,
    name: "WordOption",
	properties: {
        pic_node:{
            default:null,
            type: cc.Node,
        },
        pic_finish:{
            default:null,
            type: cc.Node,
        },
        pic:{
            default:null,
            type: cc.Sprite,
        },
        word_node:{
            default:null,
            type: cc.Node,
        },
        word_finish:{
            default:null,
            type:cc.Node,
        },
        word:{
            default:null,
            type: cc.Label,
        },
    },

    init(op){
        if (op.pic != ""){
            this.initType(0);
            getSpriteFrimeByUrl(op.pic, (spr)=>{
                this.setPic(spr);
            });
        }
        else if (op.text != ""){
            this.initType(1);
            this.setWord(op.text);
        }

        this.audio = op.audio;
        this.kind_id = op.kind;
    },

    initType(type){
        this._type = type;
        this.pic_node.active = type == 0;
        this.pic.node.active = type == 0;
        this.pic_finish.active = false;
        this.word_node.active = type == 1;
        this.word.node.active = type == 1;
        this.word_finish.active = false;
        this.node._w = [340, 410][type];
        this.resetWH();
    },

    resetWH(){
        if (this._type == 0){
            this.node.width = this.pic_finish.width;
            this.node.height = this.pic_finish.height;
            this.pic.node.x = -this.pic_node.x;
            this.pic.node.y = -this.pic_node.y;
            this.node.width = this.pic_node.width;
            this.node.height = this.pic_node.height;
        }
        else if (this._type == 1){
            this.node.width = this.word_finish.width;
            this.node.height = this.word_finish.height;
            this.node.width = this.word_node.width;
            this.node.height = this.word_node.height;
            // this.word.node.x = -this.word_node.x;
            // this.word.node.y = -this.word_node.y;
        }
    },

    setPic(spr){
        //this.pic.spriteFrame = spr;
        setSpriteWH(this.pic, spr, 258, 174);
    },

    setWord(str){
        this.word.string = str;
    },

    selected(active){
        if (this._type == 0){
            this.pic_finish.active = active;
        }
        else if (this._type == 1){
            this.word_finish.active = active;
        }
    },

    copyValues(o){
        this.initType(o._type);
        if (this._type == 0){
            this.setPic(o.pic.spriteFrame);
            this.pic.width = o.pic.node.width;
            this.pic.height = o.pic.node.height;
        }
        else if (this._type == 1)
            this.setWord(o.word.string);

        this.kind_id = o.kind_id;
        this.node._w = o.node._w;
    },

    adjustPicSize(){
        if (this._type == 0 && this.pic.width){
            this.pic.node.width = this.pic.width;
            this.pic.node.height = this.pic.height;
        }
    },

    finished(){
        this.word_finish.active = false;
        this.pic_finish.active = false;
        this.pic_node.getComponent(cc.Sprite).enabled = false;
        this.word_node.getComponent(cc.Sprite).enabled = false;
        if (this._type == 0){
            this.pic_finish.active = true;
        }
        else{
            this.word_finish.active = true;
        }
    }


});