// vehicles.js

// Get the vehicle data from the database. The method
// in is the method that will be used to get the data.
// Method can be the following:
//   1) FILES
function VehicleDB(method) {
	// depending on the defined method, get the data from the DB.
	this.Method = method;
}

VehicleDB.prototype.GetMakeList = function(updateFunc, year) {
    // update the select box once we have a list of car makers
    var make = document.getElementById('make');
    function handleResponse(makeList) {
        updateFunc(make, makeList);
    }

    // call the function that gets the list of car makers
    if (this.Method == 'FILES') {
        this.GetMakeFromFile(handleResponse, year);
    }
}

VehicleDB.prototype.GetModelList = function(updateFunc, year, make) {
    // update the select box once we have a list of car models
    var model = document.getElementById('model');
    function handleResponse(modelList) {
        updateFunc(model, modelList);
    }

	// call the function that gets the list of car models
	if (this.Method == 'FILES') {
	    this.GetModelFromFile(handleResponse, year, make);
	}
}

VehicleDB.prototype.GetTrimList = function(updateFunc, year, make, model) {
	// update the select box once we have a list of car trims
    var trim = document.getElementById('trim');
    function handleResponse(trimList) {
        updateFunc(trim, trimList);
    }

	// call the function that gets the list of car trims
	if (this.Method == 'FILES') {
	    this.GetTrimFromFile(handleResponse, year, make, model);
	}
}



/////////////////// Get the car data from files
VehicleDB.prototype.GetMakeFromFile = function(callback, year) {
    function handleResponse(response) {
        var makeList = parseMake(response);
        callback(makeList);
    }
    GetFile(year + '/make.txt', handleResponse);
}

VehicleDB.prototype.GetModelFromFile = function(callback, year, make) {
	function handleResponse(response) {
        var modelList = parseModel(response);
        callback(modelList);
    }
    GetFile(year + '/make/' + make.toLowerCase().replace(/ /g, '') + '.txt', handleResponse);
}

VehicleDB.prototype.GetTrimFromFile = function(callback, year, make, model) {
	function handleResponse(response) {
        var trimList = parseTrim(response, model);
        callback(trimList);
    }
    GetFile(year + '/make/' + make.toLowerCase().replace(/ /g, '') + '.txt', handleResponse);
}

// parses the text containing car makers and returns an array of car maker names
function parseMake(contents) {
    var makers = contents.replace(/\r/g, '').split('\n');
    var result = new Array();
    var newIndex = 0;
    
    for (var i = 0; i < makers.length; i++) {
        if (makers[i].length < 2) {
            continue;
        }
        result[newIndex] = makers[i];
        newIndex++;
    }
    return result;
}

// parses the text containing car models and returns an array of car model names
function parseModel(contents) {
    // there are 2 columns. we only want the first
    var rows = contents.replace(/\r/g, '').split('\n');
    var models = new Array();
    var temp;
    var newIndex = 0;
    
    // make sure we are not empty
    if (rows != null) {
        // copy the first column in each row after we split it.
        for (var i = 0; i < rows.length; i++) {
            temp = rows[i].split('\t')[0];
            
            // list is already sorted, skip if duplicate
            if (i > 0) {
                if (temp == models[newIndex-1]) {
                    continue;
                }
            }
            
            // skip if empty
            if (temp.length < 2) {
                continue;
            }
            
            // add to the list
            models[newIndex] = temp;
            newIndex++;
        }
    }
    return models;
}

// Parses the text containing car models and trims and returns an array of car trims
// To find the trim in the text, we use the model.
function parseTrim(contents, model) {
    // there are 2 columns. we only want the second
    var rows = contents.replace(/\r/g, '').split('\n');
    var trims = new Array();
    var temp;
    var newIndex = 0;
    
    // make sure we are not empty
    if (rows != null) {
        // copy the second column in each row that contains the correct model
        for (var i = 0; i < rows.length; i++) {
            // get the entire row
            temp = rows[i].split('\t');
            
            // skip models that don't match what we want
            if (temp.length < 2 || temp[0] != model) {
                continue;
            }
                        
            // add to the list
            trims[newIndex] = temp[1];
            newIndex++;
        }
    }
    return trims;
}
