import { playDragonBoneAnimation } from "../script/util";

cc.Class({
	extends: cc.Component,
    name: "CompleteUI",
	properties: {
        stars:{
            default:[],
            type: cc.Node
        },

        lists: {
			default: [],
			type: cc.Node,
        },

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

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

    onLoad:function(){
        this.animate_node.active = false;
        let replay = this.node.getChildByName('replay_btn');
        replay.on('click', ()=>{
            this.node.active = false;
            scene.tapReplay();
        });
    },
    ctor: function () {
    },

    show:function(){
        this.node.opacity = 0;
        this.node.active = true;
        this.animate_node.active = true;
        playDragonBoneAnimation(this.animate_node, 'feedback-final');
        
        let ar = scene._result;
		let col, item, lbl, lbls=[];
		for(let i = 0; i < 3; i++){
			col = this.lists[i];
			col.removeAllChildren();
			for(let j = 0; j < ar[i].length; j++){
				item = cc.instantiate(this.template_node);
                lbl = item.getComponent(cc.Label);
                item.x = 0;
                lbl.string = ar[i][j];
                
                
				lbls.push(lbl)
				col.addChild(item);
			}
        }
        console.log(ar);
        console.log(lbls);
        
        
        cc.tween(this.node)
        .delay(0.1)
        .call(()=>{
            let maxw = 0;
            let item;

            for (let i = 0; i < lbls.length; i++) {
                maxw = Math.max(lbls[i].node.width, maxw);
            }
            let rate = 106/maxw;

            let neww = 24 * rate;

            for (let i = 0; i < lbls.length; i++) {
                lbls[i].fontSize = neww;
            }
            this.node.opacity = 150;
        })
        .to(0.2, {opacity: 255})
        .start();

        var maxCount = scene._words.length;
        var starNum = 3;
        if (ar[2].length > maxCount/2){
            starNum = 0;
        }
        else if (ar[2].length > 0){
            starNum = 1;
        }
        else if (ar[1].length > 0){
            starNum = 2;
        }

        for (let i = 0; i < 3; i++)
            this.stars[i].active = i < starNum;
    },

});
