import("system.logging");
import("system.util");
/**
 *  
 * @class
 */
function MultiDataChart() 
{
    this._X = 0;
    this._Y = 1;
    
    this.dataSources = {};
    this.drilldowns = {};
    this._resultData = [];
}

MultiDataChart.begin = function() 
{
    var chart = new MultiDataChart();    
    return chart;
}

/**
 * Add a new datasource
 * Has to be an array with [[x, y, ...], [x, y, ...]]
 * ... means you can add additional columns, used as identification and naming of drilldowns
 */
MultiDataChart.prototype.addDataSource = function(pName, pData, pFixedXValues) 
{        
    this.dataSources[pName] = {
        data: pData,
        fixedValues: pFixedXValues
    }
    return this;
}

/**
 * 
 */
MultiDataChart.prototype.addCumulativeDrillDown = function(pDataSourceName, pGetParentXCallback, pGetCummulationCallback) 
{        
    if (this.dataSources[pDataSourceName] != undefined) 
    {
        this.drilldowns[pDataSourceName].type = "cumulative";
        this.drilldowns[pDataSourceName].dataSource = pDataSourceName;
        this.drilldowns[pDataSourceName].getParentX = pGetParentXCallback;
        this.drilldowns[pDataSourceName].getCumulation = pGetCummulationCallback;
    }
    else 
    {
        throw new Error("MultiDataChart::addCumulativeDrillDown: Data source " + pDataSourceName + " doesn't exist!");
    }
    
    return this;
}

/**
 * 
 */
MultiDataChart.prototype.build = function() 
{        
    for (dataSource in this.dataSources)
    {
        
        
        
        for (let i = 0; i < this.dataSources[dataSource].data.length; i++) 
        {
            var dataRow = this.dataSources[dataSource].data[i];
            var rowId = util.getNewUUID();
                                  // id, parent, group,     x,                      y
            this._resultData.push([rowId, "", dataSource, dataRow[this._X], dataRow[this._Y]]);
        }
    }
    
    logging.log(this._resultData.toSource())
    return this._resultData;
}

/**
 * check if all X-Values exist. If not, create empty values.
 * 
 * 
 * TODO!!!!!!!!!!!!!
 * 
 */
MultiDataChart.prototype._normalize = function(pDataSource)
{
    var fixed = pDataSource.fixedValues;
    var data = pDataSource.data;
    var found = {};
    
    var resultData = [];
    
    for (let i = 0; i < data.length; i++) 
    {
        
        if (found[data[i][this._X]] == undefined)
        {
            if (fixed.indexOf(data[i][this._X]) >= 0)
            {
                
            }
            else
            {
                throw new Error("The X-Value " + data[i][this._X] + " is not in the fixed values of the dataSource!");
            }
        }
        
        found[data[i][this._X]] = true;
        
        resultData.push(data[i]);
    }
    
    return resultData;
}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    for (drilldown in this.drilldowns)
    {
        if (drilldown.type == "cumulative")
        {
            this._processCumulativeDrilldown(drilldown);
        }
    }
}














MultiDataChart.prototype._processDrilldowns = function()
{
    for (drilldown in this.drilldowns)
    {
        if (drilldown.type == "cumulative")
        {
            this._processCumulativeDrilldown(drilldown);
        }
    }
}

MultiDataChart.prototype._processCumulativeDrilldown = function(pDrilldown)
{
    var parentId = util.getNewUUID();
    var data = this.dataSources[pDrilldown.dataSource];
    
    
    
    
   
}