/*
 * Copyright (c) 2012 Francisco Salavert (ICM-CIPF)
 * Copyright (c) 2012 Ruben Sanchez (ICM-CIPF)
 * Copyright (c) 2012 Ignacio Medina (ICM-CIPF)
 *
 * This file is part of JS Common Libs.
 *
 * JS Common Libs is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * JS Common Libs is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JS Common Libs. If not, see <http://www.gnu.org/licenses/>.
 */

BEDDataAdapter.prototype.getData = FeatureDataAdapter.prototype.getData;
BEDDataAdapter.prototype._fetchData = FeatureDataAdapter.prototype._fetchData;

function BEDDataAdapter(dataSource, args) {
    FeatureDataAdapter.prototype.constructor.call(this, dataSource, args);
    var _this = this;

    this.async = true;

    this.parseFunction;
    if (args != null) {
        if (args.parseFunction != null) {
            this.parseFunction = args.parseFunction;
        }
    }

    //stat atributes
    this.featuresCount = 0;
    this.featuresByChromosome = {};

    if (args != null) {
        if (args.async != null) {
            this.async = args.async;
        }
    }
};

BEDDataAdapter.prototype.parse = function (data, region) {
    if (this.parseFunction){
        this.parseFunction(data, region);
    }else{
        this._defaultParse(data, region);
    }
};

BEDDataAdapter.prototype._defaultParse = function (data, region) {

    var _this = this;
    var dataType = "value";
    var lines = data.split("\n");
//	console.log("creating objects");
    for (var i = 0; i < lines.length; i++) {
        var line = lines[i].replace(/^\s+|\s+$/g, "");
        if ((line != null) && (line.length > 0)) {
            var fields = line.split("\t");
            var chromosome = fields[0].replace("chr", "");
            if (chromosome == region.chromosome) {// load only one chromosome on the cache

                var feature = {
                    "label": fields[3],
                    "chromosome": chromosome,
                    "start": parseFloat(fields[1]),
                    "end": parseFloat(fields[2]),
                    "score": fields[4],
                    "strand": fields[5],
                    "thickStart": fields[6],
                    "thickEnd": fields[7],
                    "itemRgb": fields[8],
                    "blockCount": fields[9],
                    "blockSizes": fields[10],
                    "blockStarts": fields[11],
                    "featureType": "bed"
                };

                this.featureCache.putFeatures(feature, dataType);

                if (this.featuresByChromosome[chromosome] == null) {
                    this.featuresByChromosome[chromosome] = 0;
                }
                this.featuresByChromosome[chromosome]++;
                this.featuresCount++;
            }
        }
    }
};
