/*
 * Author: 众产®  https://ciy.cn/code
 * Version: 0.1.1
 */
'use strict';
ciyclass.bigpanel = function (opt) {
    var thos = this;
    if (!opt.name)
        return console.error('panel: opt nofind name');
    if (!opt.jspath)
        return console.error('panel: opt nofind jspath');
    opt.style = opt.style || '';
    opt.prop = opt.prop || '';
    this.container = $5('<div id="' + opt.name + '" style="' + opt.style + '" ' + opt.prop + '></div>');

    if (opt.animation) {
        if (this.container[0].animate) {
            this.container.show(opt.animation, 400);
        } else {
            opt.animation = null;
        }
    }
    if (opt.mask) {
        var maskdiv = $5('<div style="background:' + opt.mask + ';position: absolute;top:0;left:0;right:0;bottom:0;"></div>');
        maskdiv.append(this.container);
        $5('body').append(maskdiv);
    } else {
        $5('body').append(this.container);
    }

    var plots = {};
    var bfuncload = false;
    var funcsecs = {};
    var tick = null;
    var tick_btns = null;
    var t_btns;
    this.addplot = function (opn) {
        plots[opn.jsname] = { load: 0 };
        // if (window['ciy_chart_' + opn.jsname])
        //     return;
        opn.style = opn.style || '';
        opn.prop = opn.prop || '';
        ciyfn.loadjs('ciy_chart_' + opn.jsname, opt.jspath + opn.jsname + '.js', function () {
            plots[opn.jsname].load = 1;
            plots[opn.jsname].container = $5('<div style="' + opn.style + '"' + opn.prop + '></div>');
            thos.container.append(plots[opn.jsname].container);
            plots[opn.jsname].load = 2;
            plots[opn.jsname].clss = new window['ciy_chart_' + opn.jsname]({ container: plots[opn.jsname].container });

        });
    }
    this.setbtns = function (opn) {
        opn.html = opn.html || '';
        if (opn.hideopacity === undefined)
            opn.hideopacity = 0.9;
        opn.hidesec = opn.hidesec || 5;
        if (!opn.html) {
            if (opn.template == 'close1')
                opn.html = '<div data-act="close" style="z-index:100001;opacity:' + opn.hideopacity + ';transition:all 0.5s;padding: 0;position: absolute;right: 2px;top: ' + (opn.top ? opn.top : '2px') + ';color: #000000;font-size: 2em;width: 1em;height: 1em;display: block;cursor: pointer;background: #c7f4f8;text-align: center;line-height: 1em;border: 1px solid #3fc7f5;border-radius: 5px;">×</div>';

        }
        if (!opn.html)
            return console.error('setbtns缺少html或template参数');
        var btns = $5(opn.html);
        thos.container.append(btns);
        t_btns = 0;
        thos.container.on('mousemove', function () {
            t_btns = +new Date();
        });
        tick_btns = setInterval(function () {
            if (+new Date() - t_btns > opn.hidesec * 1000)
                btns.css('opacity', opn.hideopacity);
            else
                btns.css('opacity', 1);
        }, 500);
        btns.on('click', function (e) {
            var act = e.currentTarget.getAttribute('data-act');
            if (act == 'close')
                thos.distory();
            else {
                if (typeof (opn.click) == 'function')
                    opn.click(e);
            }
        });
    }
    this.run = function (fsec) {
        function _run() {
            if (!bfuncload) {
                var bload = true;
                for (var iplot in plots) {
                    if (plots[iplot].load == 0)
                        bload = false;
                    if (!plots[iplot].clss)
                        continue;
                    if (!plots[iplot].clss.func)
                        continue;
                    var func = plots[iplot].clss.func;
                    for (var i in func) {
                        var fkey = (plots[iplot].clss.prefunc || '') + func[i];
                        funcsecs[fkey] = {};
                        funcsecs[fkey].t = 0;
                        funcsecs[fkey].prefunc = plots[iplot].clss.prefunc;
                        funcsecs[fkey].max = fsec[fkey] ? fsec[fkey] : 0;
                    }
                }
                bfuncload = bload;
            }
            //console.log(funcsecs);
            for (var ifu in funcsecs) {
                if (funcsecs[ifu].t == 0) {
                    (function (ifu) {
                        var post = ciyfn.objclone(Glob.post);
                        ciyfn.callfunc(ifu, Object.assign({}, post, thos.post), function (jsondata) {
                            if (funcsecs[ifu].max > 0)
                                funcsecs[ifu].t = funcsecs[ifu].max;
                            for (var iplot in plots) {
                                if (!plots[iplot].clss)
                                    continue;
                                if (plots[iplot].clss.prefunc != funcsecs[ifu].prefunc)
                                    continue;
                                var funcstr = ifu.substring((funcsecs[ifu].prefunc || '').length);
                                if (plots[iplot].clss.func.indexOf(funcstr) == -1)
                                    continue;
                                if (typeof (plots[iplot].clss.setdata) != 'function')
                                    continue;
                                try {
                                    plots[iplot].clss.setdata(funcstr, jsondata);
                                } catch (e) {
                                    console.error('setdata:' + ifu, jsondata, e);
                                }
                            }
                        }, {
                            showload: false
                            , fail: function (jsondata, err) {
                                if (jsondata && jsondata.errmsg) {
                                    if (typeof (plots[iplot].clss.errdata) == 'function')
                                        plots[iplot].clss.errdata(ifu, jsondata);
                                    return;
                                }
                                funcsecs[ifu].t = 5;//5秒重试
                            }
                        });
                    })(ifu);
                    //console.log('run', ifu);
                }
                funcsecs[ifu].t--;
            }
        }
        _run();
        tick = setInterval(_run, 500);
    }
    this.setdata = function () { //临时
        for (var iplot in plots) {
            if (plots[iplot].load < 2)
                continue;
            if (typeof (plots[iplot].clss.setdata) == 'function')
                plots[iplot].clss.setdata();
        }
    }
    this.resize = function () {
        for (var iplot in plots) {
            if (plots[iplot].load < 2)
                continue;
            if (typeof (plots[iplot].clss.resize) == 'function')
                plots[iplot].clss.resize();
        }
    }
    this.distory = function () {
        clearInterval(tick);
        clearInterval(tick_btns);
        for (var iplot in plots) {
            //window['ciy_chart_' + iplot] = null;
            if (plots[iplot].load < 2)
                continue;
            if (plots[iplot].clss && typeof (plots[iplot].clss.distory) == 'function')
                plots[iplot].clss.distory();
            delete plots[iplot].clss;
            delete plots[iplot];
        }
        if (opt.animation) {
            if (opt.mask) {
                thos.container.parent()[0].animate([
                    { opacity: 1 },
                    { opacity: 0 }
                ], { duration: 400 });
                thos.container.parent().css('opacity', 0);
            }
            if (opt.animation == 'ani2') {

            } else {
                this.container[0].animate([
                    { opacity: 1, transform: 'scale(1)' },
                    { opacity: 0.9, transform: 'scale(1.05)', offset: 0.2 },
                    { opacity: 0, transform: 'scale(0)' }
                ], { duration: 400 });
                this.container.css('transform', 'scale(0)');
            }
            setTimeout(function () {
                removediv();
            }, 1000);

        } else {
            removediv();
        }
        function removediv() {
            if (opt.mask) {
                thos.container.parent().remove();
            } else {
                thos.container.remove();
            }
        }
    }
    return this;
};

ciyfn.big_setbetsize = function (basewidth, fontsize) {
    if (fontsize === undefined)
        fontsize = 'auto';
    var bet = 1;
    if (fontsize !== 0) {
        if (fontsize == 'auto') {
            fontsize = 14;
            var screenwidth = window.screen.width;
            if (urlp.dev) {
                if (window.devicePixelRatio / 1.25 < 1) {
                    screenwidth = screenwidth * 1.25 / window.devicePixelRatio;
                }
            }
            if (screenwidth > basewidth) {
                fontsize = 14 * screenwidth / basewidth;
                bet = screenwidth / basewidth;
            }
        }
        $5('body').css('fontSize', fontsize);
    } else {
        fontsize = 14;
    }
    Glob.fontsize = fontsize - 2;
    Glob.bet = bet;
};

ciyfn.big_resolution = function (width, height) {
    if (window.screen.width != width || window.screen.height != height) {
        ciyfn.toast('最佳分辨率为:' + width + ' x ' + height);
        return;
    }
    if (window.innerWidth != width)
        return;//判断调试模式

    if (window.innerHeight < height)
        return ciyfn.toast('请按F11，进入全屏模式');

    // ciyfn.alert({
    //     content: '进入全屏?'
    //     , cb: function (opn) {
    //         opn.close();
    //         if (opn.btn == "关闭")
    //             return;
    //         var de = document.documentElement;
    //         if (de.requestFullscreen) {
    //             de.requestFullscreen();
    //         } else if (de.mozRequestFullScreen) {
    //             de.mozRequestFullScreen();
    //         } else if (de.webkitRequestFullScreen) {
    //             de.webkitRequestFullScreen();
    //         } else if (de.msRequestFullscreen) {
    //             de.msRequestFullscreen();
    //         }
    //         else {
    //             ciyfn.alert("当前浏览器不支持全屏！");
    //         }
    //     }
    //     , btns: ["全屏", "*关闭"]
    // });
};
ciyclass.bigcychange = function (opt) {
    var thos = this;
    if (Object.prototype.toString.call(opt.values) !== '[object Array]')
        return false;
    var valuelen = opt.values.length;
    if (valuelen == 0)
        return false;
    if (typeof (opt.changecb) != 'function')
        return false;
    if (opt.cycsec < 1)
        opt.cycsec = 5;

    this.value = opt.values[0];

    var _t = 0;
    var _idx = -1;

    function _run() {
        _t--;
        if (_t < 0) {
            _t = opt.cycsec;
            _idx++;
            if (_idx >= valuelen)
                _idx = 0;
            thos.value = opt.values[_idx];
            opt.changecb(thos.value);
        }
    }
    _run();
    var tick = setInterval(_run, 1000);
    this.change = function (mm, longsec) {
        var idx = opt.values.indexOf(mm);
        if (idx == -1)
            return false;
        if (longsec)
            _t = longsec;
        else
            _t = opt.cycsec;
        _idx = idx;
        thos.value = opt.values[_idx];
        opt.changecb(thos.value);
    }
    this.distory = function () {
        clearInterval(tick);
    }
    return this;
};