var webView = require("../WebView");
var lib = require("../Library");
cc.Class({
    extends: cc.Component,

    properties: {
        playerCarorieBar: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        turntable: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        costTime: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },
    init()
    {
        if (lib.openInWebview())
        {
            // 在app内Webview打开
            webView.register(this.node);
            this.node.on('onFruitInfo',this.onFruitInfo,this);
        }
        else {
            this.node.active = true;
        }

        this.calorie = this.playerCarorieBar.getChildByName('CalorieRichtext');
        this.calorieAvatar = this.playerCarorieBar.getChildByName('Mask').getChildByName('Avatar');

        this.timeRichText = this.costTime.getChildByName('TimeRichtext');

        this.scrollView1 = this.turntable.getChildByName('ScrollView1').getComponent(cc.ScrollView);
        this.scrollView2 = this.turntable.getChildByName('ScrollView2').getComponent(cc.ScrollView);
        this.scrollView3 = this.turntable.getChildByName('ScrollView3').getComponent(cc.ScrollView);
        this.scrollViewArr = [this.scrollView1,this.scrollView2,this.scrollView3];
    },
    updateAvatar()
    {
        lib.setImageBase64(webView.avatarBase64, function (texture2D) {
            this.calorieAvatar.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture2D);
        }.bind(this));
    },
    updateCalorie(calorie)
    {
        console.log('1111=',calorie);
        if (lib.openInWebview())
        {
            // 在app内Webview打开
            webView.getFruitInfo(calorie);
        }
    // <outline  color=white width=2><size=50>300</></><size=30>卡</>
        this.calorie.getComponent(cc.RichText).string = '<outline color=white width=2><size=50>'+calorie+'</></><size=30>卡</>';
    },
    onFruitInfo(data) {
        /**
         * 获取水果图片信息
         * fruitBase64Url: "",// 水果雪碧图 base64
         * unitWidth: 100,// 雪碧图 单张图片宽
         * unitHeight: 100,// 雪碧图 单张图片高
         * unit: "px",// 雪碧图 单位
         * imageStartPosY: 0,// 雪碧图 起始图 Y方向位置
         * imageEndPosY: -1200,// 雪碧图 结束图 Y方向位置
         * fruitIndexArray: [0,0,0] // 输入卡路里后计算的返回结果，当前的数组
         */

        this.updateTurntable(data);
        console.log('22222=',data);
    },
    updateTurntable(data)
    {
        this.node.active = true;
        this.scheduleOnce(function () {
            let fArr = data.fruitIndexArray;
            for(let i = 0 ;i<fArr.length; i++)
            {
                let offset = fArr[i];
                if(offset!=0)
                {
                    this.scrollViewArr[i].scrollToOffset(cc.v2(0, (offset+1)*100), (offset+1)/2, true);
                }
            }
        }, 1);
    },
    updateCostTime(time)
    {
        // <size=30>耗时:  </><size=25>1天4小时5分3秒</>
        this.timeRichText.getComponent(cc.RichText).string = '<size=30>耗时: </><size=25>'+this.formatSeconds(time)+'</>';
    },
    /**
     * 格式化秒
     * @param int  value 总秒数
     * @return string result 格式化后的字符串
     */
    formatSeconds(value) {
        let theTime = parseInt(value);// 需要转换的时间秒
        let theTime1 = 0;// 分
        let theTime2 = 0;// 小时
        let theTime3 = 0;// 天
        if (theTime > 60) {
            theTime1 = parseInt(theTime / 60);
            theTime = parseInt(theTime % 60);
            if (theTime1 > 60) {
                theTime2 = parseInt(theTime1 / 60);
                theTime1 = parseInt(theTime1 % 60);
                if (theTime2 > 24) {
                    //大于24小时
                    theTime3 = parseInt(theTime2 / 24);
                    theTime2 = parseInt(theTime2 % 24);
                }
            }
        }
        let result = '';
        if (theTime > 0) {
            result = "" + parseInt(theTime) + "秒";
        }
        if (theTime1 > 0) {
            result = "" + parseInt(theTime1) + "分" + result;
        }
        if (theTime2 > 0) {
            result = "" + parseInt(theTime2) + "小时" + result;
        }
        if (theTime3 > 0) {
            result = "" + parseInt(theTime3) + "天" + result;
        }
        return result;
    }
});
