﻿var addresses = {};
var address;
var docProcessor = {
    data: null, prec: null, speed: 6000, continueLatLongQueue: true, url: null,
    start: function() {
        callLatLongServer({ nextIndex: 0, rowCount: "null" });
        processQueue();
    },
    gotData: function(data) {
        this.data = data;

        if (this.data.endOfFile)
            this.continueLatLongQueue = false;
        else
            this.processDoc(0);
    },
    processDoc: function(prec) {
        var doc = this.data;

        switch (prec) {
            case 0:
                if (doc.City && doc.City != "" && doc.Address_1 && doc.Address_2)
                    address = doc.Address_1 + ' ' + doc.Address_2 + ' ' + doc.City + ', ' + doc.State;
                else
                    address = "";
                break;
            case 1:
                if (doc.City && doc.City != "" && doc.Address_1)
                    address = doc.Address_1 + ' ' + doc.City + ', ' + doc.State;
                else
                    address = "";
                break;
            case 2:
                if (doc.City && doc.City != "")
                    address = doc.City + ', ' + doc.State;
                else
                    address = "";
                break;
            case 3:
                address = doc.State;
                break;
        }

        getLatLong(address, prec);
    },
    docProcessed: function(point, prec, addressUsed) {
        var doc = this.data;

        if (point && point.lat()) {
            doc.docLat = point.lat();
            doc.docLong = point.lng();
            doc.precision = prec;
            doc.addressUsed = addressUsed;

            this.done();
        } else {
            if (prec == 3) {
                this.processDoc(0);
            } else {
                this.processDoc(prec + 1);
            }
        }
    },
    done: function() {
        gotLatsAndLongs(this.data);
    }
}

// process doc list with lats and longs added here
// send it back to server, and get new set of doc data
var params;
function gotLatsAndLongs(data) {
    params = {
        rowCount: data.rowCount,
        nextIndex: data.nextIndex + 1,
        id: data.Id,
        lat: data.docLat,
        lng: data.docLong
    };

    callLatLongServer(params);
}

var googleReqs = 0;
var geocoder = new GClientGeocoder();
var callbackQueue = [];
function getLatLong(docAddress, prec) {
    var addr = docAddress;
    var precision = prec;
    var callback = function(point) {
        docProcessor.docProcessed(point, precision, addr);
    };

    callbackQueue.push({ callback: callback, addr: addr });
}

function processQueue() {
    if (callbackQueue.length > 0) {
        googleReqs++;

        if (window.gRequestsLabel)
            gRequestsLabel.setContents("Total Address Lookups: " + googleReqs);

        var callbackObj = callbackQueue.pop();
        geocoder.getLatLng(callbackObj.addr, callbackObj.callback);
    }

    if (docProcessor.continueLatLongQueue)
        isc.Timer.setTimeout(processQueue, docProcessor.speed);
}

var done;
var total;
var progressPct;
var reqs = 0;
function gotDocData(request, data, response) {
    done = data.nextIndex - 1;
    total = data.rowCount;
    progressPct = (done / total) * 100;
    reqs++;

    if (window.progress)
        progress.setPercentDone(Math.min(progressPct, 100));
    if (window.progressLabel)
        progressLabel.setContents("Progress: " + done + " of " + total);
    if (window.requestsLabel)
        requestsLabel.setContents("Total Requests: " + reqs);

    docProcessor.gotData(data);
}

var actionURL;
var serverCallback;
function callLatLongServer(params, url, callback) {
    if (url != undefined) actionURL = url;
    else if (docProcessor.url) actionURL = docProcessor.url;
    else actionURL = "LatLong.aspx";

    if (callback != undefined) serverCallback = callback;
    else serverCallback = gotDocData;

    isc.RPCManager.delayCall("send", [null, serverCallback, {
        actionURL: actionURL, evalResult: true,
        useSimpleHttp: true, httpMethod: "POST", clientContext: params,
        params: params
    }]);
}

