/***
 * Data Object Related helper classes (vue applicable)
 * dependency:
 *
 * jQuery (ajax, selector)
 */

const DEFAULT = 2;
const ONDEMAND = 4;
const AUTO = 8;
const FAST = 16;
const PASSIVE = 32;


/***
 * DataObject DO
 * @param cname
 * @param serverJsonObj
 * @constructor
 */
var DataObject = function(cname, serverJsonObj){
    this.ctlName = cname;
    this.jsonObj = serverJsonObj;
    this.getVal = function (key, obj) {
        return obj.jsonObj[key];
    };
    this.serVal = function (key, val, obj) {
        obj.jsonObj[key] = val;
    };

};

/***
 * DataObjectList
 * @param name
 * @constructor
 */
var DataObjectList = function(name){
    this.ctlName = name;
    this.list = null;
    this.isSorted = false;
    this.preSort = [];
    /***
     * sorting
     * @param comparator (a,b)  =====>  a - b
     */
    this.sortBy = function(comparator){
        this.preSort = new Array();
        for (let i = 0; i < this.list.length; i++) this.preSort.push(this.list[i]);
        this.quickSort(this.preSort, 0, this.preSort.length - 1, comparator);
    };

    this.clearData = function () {
        this.isSorted = false;
        this.preSort = null;
        this.list = null;
    }

    this.len = function() {
        if(this.list == null) return 0;
        return this.list.length;
    };

    this.mergeJsonData = function (data) {
        this.list = new Array();
        for (let i = 0; i < data.length; i++) this.addObject(data[i]);
    };

    this.addObject = function (jsonObj) {
        this.list.push(new DataObject(this.ctlName, jsonObj));
    };

    this.assignJsonData = function (data) {
        this.clearData();
        this.mergeJsonData(data);
    };
    this.getRaw = function (index) {
        if(this.list == null) return null;
        return this.list[index];
    };
    this.getSorted = function (index) {
        if(this.list == null) return null;
        if(!this.isSorted) return this.getRaw(index);
        return this.preSort[index];
    };
    this.quickSort = function (a,left,right,comp){
        if(left>right){ //一定要有这个判断，因为有递归left和i-1，若没有这个判断条件，该函数会进入无限死错位递归
            return;
        }

        var i=left,
            j=right,
            pivot=a[left]; //基准总是取序列开头的元素

        while(i!=j){
            while((comp(a[j],pivot) > 0)&&i<j){j--}
            while((comp(a[j],pivot) <= 0)&&i<j){i++}
            if(i<j){  //如果i==j跳出外层while
                var t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        }

        a[left]=a[i];//交换基准数和k位置上的数
        a[i]=pivot;

        this.quicksort(a,left,i-1,comp);
        this.quicksort(a,i+1,right,comp);
    };
};

/***
 * DOService
 * @param controlName
 * @constructor
 */
var DataObjectService = function(controlName, syncType, extparm){
    this.ctlName    = controlName;
    // 2=default 4=on-demand, 8=auto, 16:fast, 32=passive
    this.syncType   = syncType;
    this.extParm    = extparm;
    this.isDefault  =  ((syncType & DEFAULT)  !== 0);
    this.isOnDemand = ((syncType & ONDEMAND)  !== 0);
    this.isAuto =     ((syncType & AUTO)  !== 0);
    this.isFast =     ((syncType & FAST) !== 0);
    this.isPassive  = ((syncType & PASSIVE) !== 0);
    this.failedCount= 0;
    this.list       = new DataObjectList(controlName);
    this.autoUpdateTimer = 0;
    this.autoInterval = 1000;
    this.callerThis = this;

    this.setUpInterval = function() {
        //URL + exparm
        if(this.autoUpdateTimer != 0) clearInterval(this.autoUpdateTimer);
        this.autoUpdateTimer = setInterval(this.selfUpdateTick, this.autoInterval, this);
        setTimeout(this.selfUpdateTick, 1000, this);
    }

    this.init = function(){
        this.appUrl = controlName + "/list" + extparm;
        if(!this.isFast || this.isOnDemand){
            this.autoInterval = 300;
        }

        if(this.isFast) {
            this.appUrl = controlName + "/get" + extparm;
        }

        if(this.isAuto){
            // timer
            this.setUpInterval();
        }
    };

    this.stopTimers = function () {
        clearInterval(this.autoUpdateTimer);
        this.autoUpdateTimer = 0;
    };

    /***
     * Automatica Update Tick, where obj is a replacement for "this" where "this" might be incorrect
     * @param obj : DataObjectService
     */
    this.selfUpdateTick = function(obj){
        get_data(obj.appUrl, obj.onDataReceived, obj);
    };

    this.onDemandUpdate = function(obj){
        get_data(obj.appUrl, obj.onDataReceived, obj);
    };

    this.onDataReceived = function(data, sta){
        let obj = this.callerThis;
        if(obj == undefined){console.error("Cannot get callerThis in onDataReceived, please check"); console.error(this)}
        let output = first_parse(this, data);
        if (output.list == undefined || output.list.length == 0) {
            obj.failedCount++;
            if(obj.failedCount > 100){
                obj.failedCount = 0;
                obj.stopTimers();
                console.log("[排行榜返回为空git clone https://github.com/pentaho/mondrian-tck.git次数过多]，暂停刷新");
            }
            if(obj.failedCount == 2) {
                send_alert("排行榜多次返回没有数据，请尝试重新加载！ [" + obj.appUrl +"]");
            }
        }else{
            obj.list.assignJsonData(output.list);
        }
    };

    this.getList = function () {
        return this.list;
    };

    this.passiveUpdate = function(data){
        this.failedCount = 0;
        this.list.assignJsonData(data);
    };

    this.init();
};

var SinglePage = function () {
    this.modules = [];
};

var Module = function(chartContainerSelector, dataCtlName){
    this.selector = chartContainerSelector;
    this.dataCtl = dataCtlName;
    this.activeData = null;
    this.lastActive = 0;
    this.tickInterval = 1000;
    /***
     * bindData to UI
     * @param data DataObjectList
     */
    this.setData = function (data) {
        if(data.ctlName == this.dataCtl){
            console.log(`-------- Module ${this.selector} got data ----------- `);
            this.activeData = data;
            this.drawData(this.activeData);
        }else{
            console.log(`-------- Module ${this.selector} cannot use data of ${data.ctlName} ----------- `);
        }
    };
    this.init = function () {
        // do init
        this.setupInterval();
    };
    this.tick = function () {
        // do tick
    };
    this.setupInterval = function () {
        // do tick
        if(this.tickInterval != 0) clearInterval(this.tickInterval);
        this.tickInterval = setInterval(this.tick, this.tickInterval);
    };
    this.drawData = function () {
        // do drawData
    };
};


export {
    DataObjectService,
    DataObjectList,
    DataObject,
    Module,
    SinglePage
}