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

cc.Class({
    extends: cc.Component,
    name: "MikeButton",
	properties: {
        db_item:{
            default:null,
            type:cc.Node
        },
        button:{
            default:null,
            type:cc.Button
        }
    },

    ctor: function () {
    },

    onLoad:function(){
        this.node.on(cc.Node.EventType.TOUCH_START, this.tapStart, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.tapEnd, this);
        this._down = 0;
        this._up = 0;
    },

    tapStart(event){
        if (this.button.interactable == false)
            return;
        this.db_item.active = true;
        console.log(scene._words[scene._count].word);
        if (window && window.courseware) {
            window.courseware.startTest(scene._words[scene._count].word);
        }
        scene.tapMikeStart();
    },
    tapEnd(event){
        if (this.button.interactable == false)
            return;

            this.db_item.active = false;
        let data;
        scene.fallAnimation();

        if (window && window.courseware) {
            window.courseware.stopTest((result)=>{
                // data = result;
                data = JSON.parse(result);
                this.handleData(data);
            });
        }
        else{
            data = '{ "result" : { "overall" : 62 } }';
            data = JSON.parse(data);
            data.result.overall = RandomInt(60, 100);
            cc.tween(this)
            .delay(2)
            .call(()=>{
                this.handleData(data);
            })
            .start();
            return;
        }

    },

    handleData(data){
        let v = 2;
        if (data&&data.result&&data.result.overall){
            if (data.result.overall > 85) v = 0;
            else if (data.result.overall > 70) v = 1;
        }

        scene.tapMikeEnd(v);
    }




});