﻿function GISPlanning_SearchResults() {
    var me = this;

    this._SavedResultsIndex = []//a one dimensional array of the IDS of all the saved results
    this._StopProcessCurrentIterativeAsyncResultOperation = false; //set this flag to interrupt the processing of search results;

    this._Table;
    this._PageSizeDEFAULT = 26; //Do not modify this value

    this.Tracker = function(pDefaultPageSize) {
        var me = this;
        this.Results = []; //an array of pages of results
        this.Count = 0;
        this.CurrentIndex = 0;
        this.PageSize = 26;
        this.StopAtIndex = 0;
        this.NumPages = 0;
        this.SortBy = "default";
        this.Pages = [];
        this.PageSizeDefault = pDefaultPageSize;

        this.Reset = function() {
            me.Count = 0;
            me.CurrentIndex = 0;
            me.NumPages = 0;
            me.PageSize = me.PageSizeDefault;
            me.Pages = [];
            me.StopAtIndex = 0;
        }; //end method
    };

    this._SitesTracker = new this.Tracker(me._PageSizeDEFAULT);
    this._SavedTracker = new this.Tracker(me._PageSizeDEFAULT);
    this._CommunityTracker = new this.Tracker(me._PageSizeDEFAULT);
    this._CommunityTracker.SortBy = "ID"; //note, this is the default value for this type of search

    this._Trackers = {
        SITES: this._SitesTracker,
        SAVED: this._SavedTracker,
        COMMUNITY: this._CommunityTracker
    };



    this._CurrentReportID = null;
    this._Reports = [];
    this._SavedReports = [];


    this._CurrentViewType = "SITES"; //"SAVED","COMMUNITY"
    this._LastSearchType = "SITES";

    this._AdditionalPageCallback = null;



    //SEARCH RESULTS****************************************************************************************************************************************

    this.GetResults = function(pType) {
        return me._Trackers[pType].Results;
    }; //end function

    this._SetSearchResults = function(pResults) {
        var myType = pResults.Type;
        me._Trackers[myType].Results[0] = pResults.Results;
        me._Trackers[myType].Count = pResults.Count;
        me._DetermineNumPages(myType);

        //fill the pages collection with empty pages
        me._Trackers[myType].Pages = [];
        for (var i = 0; i < me._Trackers[myType].NumPages; i++) {
            me._Trackers[myType].Pages.push(false);
        }; //end for each page

        //since this is always the first page, set to true
        me._Trackers[myType].Pages[0] = true;
    };     //end function

    this._AddResult = function(pResult, pType) {
        //determine if last page is full
        var myResultCollection = me._Trackers[pType].Results;
        var myLastPage = myResultCollection.length - 1;

        //its possible to have no pages, so create one
        if (myLastPage < 0) {
            myResultCollection.push([]);
            myLastPage = 0;
        }

        //add the result
       
            myResultCollection[0].push(pResult);
        
        //make sure the pages are good
        if (me._Trackers[pType].Pages[0] == null) {
            me._Trackers[pType].Pages.push(true);
        }
        else {
            me._Trackers[pType].Pages[0] = true;
        };//end if page value exists


    };

    this._DetermineNumPages = function(pType) {

        //set up the pages
        me._SetNumPages((me._GetCount(pType) - (me._GetCount(pType) % me._GetPageSize(pType))) / me._GetPageSize(pType) + ((me._GetCount(pType) % me._GetPageSize(pType) > 0) ? 1 : 0), pType);

    }; //end function




    this._GetCount = function(pType) { return me._Trackers[pType].Count };
    this._GetCurrentIndex = function(pType) { return me._Trackers[pType].CurrentIndex };
    this._GetPageSize = function(pType) { return me._Trackers[pType].PageSize };
    this._GetPages = function(pType) { return me._Trackers[pType].Pages };
    this._GetStopAtIndex = function(pType) { return me._Trackers[pType].StopAtIndex };
    this._GetNumPages = function(pType) { return me._Trackers[pType].NumPages };
    this._GetSortBy = function(pType) { return me._Trackers[pType].SortBy };
    this._GetCurrentPage = function(pType) {
        return Math.floor((me._GetCurrentIndex(pType)) / me._GetPageSize(pType));
    };

    this._SetCount = function(pValue, pType) { me._Trackers[pType].Count = pValue };
    this._SetCurrentIndex = function(pValue, pType) { me._Trackers[pType].CurrentIndex = pValue };
    this._SetPageSize = function(pValue, pType) { me._Trackers[pType].PageSize = pValue };
    this._SetStopAtIndex = function(pValue, pType) { me._Trackers[pType].StopAtIndex = pValue };
    this._SetNumPages = function(pValue, pType) { me._Trackers[pType].NumPages = pValue };
    this._SetSortBy = function(pValue, pType) { me._Trackers[pType].SortBy = pValue };


    this.GetResultByID = function(pID, pType) {
        //Because results is an array of arrays, it can be difficut to find the item so this method does it for you
        var myFoundResult = null;

        for (var i = 0; i < me._Trackers[pType].Results.length; i++) {
            if (me._Trackers[pType].Pages[i]) {
                for (var ii = 0; ii < me._Trackers[pType].Results[i].length; ii++) {
                    if (me._Trackers[pType].Results[i][ii].ID == pID) {
                        myFoundResult = me._Trackers[pType].Results[i][ii];
                        break;
                    }; //end if match
                }; //end for each result in a page
                if (myFoundResult != null) { break; } //break out once found
            }; //end if page exists
        }; //end for each page

        return myFoundResult;
    };  //end method

    this.GetResultByIndex = function(pIndex, pType) {
        var myPageSize = me._PageSizeDEFAULT;
        var myPage = Math.floor(pIndex / myPageSize);
        return me.GetResults(pType)[myPage][pIndex % myPageSize];
    };   //end function

    this.SaveResult = function(pID, pType) {
        //find the result
        var myResult = me.GetResultByID(pID, pType);
        if (myResult != null) {
            //add the result to the last page of saved results, if the page is full, create a new page
            if (me._Trackers["SAVED"].Results.length == 0) {
                me._Trackers["SAVED"].Results.push([]); //adds a new page
            }; //end if no saved results

            if (me._Trackers["SAVED"].Results[me._Trackers["SAVED"].Results.length - 1].length == me._PageSizeDEFAULT) {
                me._Trackers["SAVED"].Results.push([]); //adds a new page
            }; //end if last page is full

            var mySavedLastPage = me._Trackers["SAVED"].Results[me._Trackers["SAVED"].Results.length - 1];


            //push a clone of the result to the saved array
            mySavedLastPage.push(new GISPlanning_CloneObject(myResult));
            me._SavedResultsIndex.push(myResult.ID);

            //increament the count
            me._Trackers["SAVED"].Count++;

            //makes sure the pages collection is good
            for (var i = 0; i < me._Trackers["SAVED"].Results.length; i++) {
                me._Trackers["SAVED"].Pages[i] = true;
            }
        }; //end if result found
    };    //end method

    this.RemoveResult = function(pID, pType) {
        //find the result
        var myPageIndex = 0;
        var myItemIndex = 0;

        for (var i = 0; i < me._Trackers["SAVED"].Results.length; i++) {

            for (var ii = 0; ii < me._Trackers["SAVED"].Results[i].length; ii++) {
                if (me._Trackers["SAVED"].Results[i][ii].ID == pID) {
                    myPageIndex = i;
                    myItemIndex = ii;
                    break;
                }; //end if match
            }; //end for each result in a page
        }; //end for each page

        //remove it
        me._Trackers["SAVED"].Results[myPageIndex].splice(myItemIndex, 1);

        //find the index value and remove it
        var myIndexIndex = 0;
        for (var i = 0; i < me._SavedResultsIndex.length; i++) {
            if (me._SavedResultsIndex[i] == pID) {
                myIndexIndex = i;
            }; //end if found
        }; //end for each index item

        me._SavedResultsIndex.splice(myIndexIndex, 1);

        //increament the count
        me._Trackers["SAVED"].Count--;

        //TODO: rebalance pages if neccessary


    };    //end method

    this.IsResultSaved = function(pID) {
        var myReturnValue = false;
        for (var i = 0; i < me._SavedResultsIndex.length; i++) {
            if (me._SavedResultsIndex[i] == pID) {
                myReturnValue = true;
                break;
            }; //end if found
            if (myReturnValue == true) { break; }
        }; //end for each saved result

        return myReturnValue;
    };        //end method

    this.ResetSearchResults = function(pType) {
        me._Trackers[pType].Reset();
        me._AdditionalPageCallback = null;
    };            //end method




    //REPORTS*******************************************************************************************************************************
    this.GetReportByUniqueID = function(pUniqueID, refIndexOfReport) {
        var myReport = null;
        if (refIndexOfReport == null) {
            refIndexOfReport = {};
        }; //end if refIndexOfReport is null

        for (var i = 0; i < me._Reports.length; i++) {
            if (me._Reports[i].UniqueID == pUniqueID) {
                myReport = me._Reports[i];
                refIndexOfReport["value"] = i;
                break;
            }; //end if found
        }; //end for each report

        return myReport;
    };               //end function

    this.GetCurrentReport = function() {
        return me.GetReportByUniqueID(me._CurrentReportID);
    };

    this.GetReportByTypeAndID = function(pType, pID) {
        return FindFirstReportInCollectionByTypeAndID(pType, pID, me._Reports);
    };         //end function


    this.LoadReportOverlays = function(pUniqueReportID) {
        var myReport = me.GetReportByUniqueID(pUniqueReportID);
        for (var o in myReport.Overlays) {
            _mapBar.map.addOverlay(myReport.Overlays[o]);
        }
    };

    this.ClearReportOverlays = function(pUniqueID) {
        var myReport = me.GetReportByUniqueID(pUniqueID);

        for (var o in myReport.Overlays) {
            _mapBar.map.removeOverlay(myReport.Overlays[o]);
        }; //end for each overlay
    };

    this.ClearCurrentReportOverlays = function() {
        var myReport = me.GetReportByUniqueID(me._CurrentReportID);

        if (myReport != null) {
            me.ClearReportOverlays(myReport.UniqueID);
            //Clear any subreport overlays. NOTE: this is not iterative, will not clear a subreports subreport if one exists
            for (var subReport in myReport.SubReports) {
                me.ClearReportOverlays(myReport.SubReports[subReport]);
            }; //end for each subreport
        }; //end if report
    };

    this.DeleteReport = function(pUniqueReportID) {
        var myReportIndex = {};
        var myReport = me.GetReportByUniqueID(pUniqueReportID, myReportIndex);
        myReport.Dispose();
        if (myReportIndex.value != -1) {
            me._Reports[myReportIndex.value] = null;
            me._Reports.splice(myReportIndex.value, 1);
        }; //end if report found

        //remove the reportID from the saved list
        var mySavedReportIndex = me.GetSavedReportIndex(pUniqueReportID);
        if (mySavedReportIndex != -1) {
            me._SavedReports.splice(mySavedReportIndex, 1);
        }; //end if saved
    };

    this.GetSavedReportIndex = function(pUniqueReportID) {
        var myIndex = -1;
        for (var i = 0; i < me._SavedReports.length; i++) {
            if (me._SavedReports[i] == pUniqueReportID) {
                myIndex = i;
                break;
            }; //end if found
        }; //end for each report

        return myIndex;
    };  //end function

    this.DeleteReportIfNotSaved = function(pUniqueReportID) {
        if (me.GetSavedReportIndex(pUniqueReportID) == -1) {
            me.DeleteReport(pUniqueReportID);
        }; //end if not saved
    };

    this.SaveReport = function(pUniqueReportID) {
        if (me.GetSavedReportIndex(pUniqueReportID) == -1) {
            me._SavedReports.push(pUniqueReportID);
        }; //end if not already saved
    };  //end function


    //*******************COMMUNITIES
    this._SetCommunityResults = function(pResults) {
        me._Commnites[0] = pResults.Results;
        me._CommunityCount = pResults.Count;
        me._DetermineNumPages("COMMUNITY");

        //fill the pages collection with empty pages
        for (var i = 0; i < me._SitesTracker.NumPages; i++) {
            me._SitePages.push(false);
        }; //end for each page

        //since this is always the first page, set to true
        me._SitePages[0] = true;
    };    //end function


};