import { getSpriteFrimeByUrl,setSpriteWH, RandomInt,playAudioByUrl} 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 = [360, 420][type];
        this.resetWH();
        this.changeBackground();
    },

    resetWH(){
        if (this._type == 0){
            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;
            this.pic.width = this.node.width;
            this.pic.height = this.node.height;
            this.node._xoff = this.pic_node.x;
        }
        else if (this._type == 1){
            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;
            this.node._xoff = this.word_node.x;
        }
    },

    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;
            this.pic_node.getComponent(cc.Sprite).spriteFrame = o.pic_node.getComponent(cc.Sprite).spriteFrame;
            this.pic_finish.getComponent(cc.Sprite).spriteFrame = o.pic_finish.getComponent(cc.Sprite).spriteFrame;
        }
        else if (this._type == 1){
            this.setWord(o.word.string);
            this.word_node.getComponent(cc.Sprite).spriteFrame = o.word_node.getComponent(cc.Sprite).spriteFrame;
            this.word_finish.getComponent(cc.Sprite).spriteFrame = o.word_finish.getComponent(cc.Sprite).spriteFrame;
        }

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

    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;
            this.node.width = this.pic_finish.width;
            this.node.height = this.pic_finish.height;
        }
        else{
            this.word_finish.active = true;
            this.node.width = this.word_finish.width;
            this.node.height = this.word_finish.height;
        }
    },

    selectedSize(){
        if (this._type == 0){
            this.node.width = 256;
        }
        else{
            this.node.width = 321;
        }
    },

    changeBackground(){
        let type = this._type;
        let fname;
        if (type == 0){
            fname = 'bg_picture' + (RandomInt(2)+1);
            this.pic_node.getComponent(cc.Sprite).spriteFrame = cc.find('res/option_bg/'+fname).getComponent(cc.Sprite).spriteFrame;
            fname += '-finish';
            this.pic_finish.getComponent(cc.Sprite).spriteFrame = cc.find('res/option_bg/'+fname).getComponent(cc.Sprite).spriteFrame;
        }
        else if (type == 1){
            fname = 'bg_word' + (RandomInt(2)+1);
            this.word_node.getComponent(cc.Sprite).spriteFrame = cc.find('res/option_bg/'+fname).getComponent(cc.Sprite).spriteFrame;
            fname += '-finish';
            this.word_finish.getComponent(cc.Sprite).spriteFrame = cc.find('res/option_bg/'+fname).getComponent(cc.Sprite).spriteFrame;
        }
    },

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