/**
 * Created by swhite on 6/11/14.
 */

var service = angular.module('ReFlowApp');

service.factory('ModelService', function($rootScope, User, Marker, Fluorochrome, Project, Site, ParameterFunction, ParameterValueType) {
    var model = {};
    model.current_site = null;
    model.current_sample = null;
    model.current_panel_template = null;

    model.user = User.get();

    function refresh_projects() {
        model.projects = Project.query();

        model.projects.$promise.then(function (projects) {
            $rootScope.projects = projects;
            projects.forEach(function (p) {
                p.getUserPermissions().$promise.then(function (value) {
                    p.permissions = value.permissions;
                });

                p.update_sites = function() {
                    // Add user's sites
                    p.sites = [];
                    var sites = Site.query({project: p.id});
                    sites.$promise.then(function (sites) {
                        sites.forEach(function (s) {
                            s.can_modify = false;
                            p.sites.push(s);
                            s.getUserPermissions().$promise.then(function (value) {
                                s.permissions = value.permissions;
                                if (value.hasOwnProperty('permissions')) {
                                    value.permissions.forEach(function (p) {
                                        if (p === 'modify_site_data') {
                                            s.can_modify = true;
                                        }
                                    });
                                }
                            });
                        });

                        // create site lookup by primary key
                        p.site_lookup = {};
                        for (var i = 0, len = p.sites.length; i < len; i++) {
                            p.site_lookup[p.sites[i].id] = p.sites[i];
                        }
                    });
                };

                p.update_sites();

            });
            $rootScope.$broadcast('projectsUpdated');
        });
    }

    //model.markers = Marker.query();
    model.getMarkers = function() {
        return Marker.query();
    };

    model.fluorochromes = Fluorochrome.query();

    model.getFluorochromes = function () {
        return this.fluorochromes;
    };

    model.getParameterFunctions = function() {
        return ParameterFunction.query(
            {}
        );
    };

    model.getParameterValueTypes = function() {
        return ParameterValueType.query(
            {}
        );
    };

    model.getProjects = function () {
        return $rootScope.projects;
    };
    model.reloadProjects = function () {
        refresh_projects();
    };

    model.getProjectById = function(id) {
        var project = $.grep($rootScope.projects, function(e){ return e.id == id; });
        if (project.length > 0) {
            return project[0];
        }
        return null;
    };

    model.setCurrentSite = function (value) {
        this.current_site = value;
        $rootScope.$broadcast('siteChanged');
    };

    model.getCurrentSite = function () {
        return this.current_site;
    };

    model.setCurrentSample = function (value) {
        this.current_sample = value;
        $rootScope.$broadcast('sampleChanged');
    };

    model.getCurrentSample = function () {
        return this.current_sample;
    };

    model.setCurrentPanelTemplate = function (value) {
        this.current_panel_template = value;
        $rootScope.$broadcast('panelTemplateChanged');
    };

    model.getCurrentPanelTemplate = function () {
        return this.current_panel_template;
    };

    return model;
});
@@ -120,16 +120,25 @@
 unction(
+%0A        
 $rootSco
@@ -144,22 +144,46 @@
 ope,
- User, Marker,
+%0A        User,%0A        Marker,%0A       
  Flu
@@ -192,16 +192,24 @@
 ochrome,
+%0A       
  Project
@@ -213,14 +213,30 @@
 ect,
- Site,
+%0A        Site,%0A       
  Par
@@ -250,16 +250,24 @@
 unction,
+%0A       
  Paramet
@@ -277,16 +277,40 @@
 alueType
+,%0A        ProcessRequest
 ) %7B%0A    
@@ -313,21 +313,23 @@
     var 
-model
+service
  = %7B%7D;%0A 
@@ -327,29 +327,31 @@
 e = %7B%7D;%0A    
-model
+service
 .current_sit
@@ -356,37 +356,39 @@
 ite = null;%0A    
-model
+service
 .current_sample 
@@ -399,21 +399,23 @@
 ll;%0A    
-model
+service
 .current
@@ -443,21 +443,23 @@
 l;%0A%0A    
-model
+service
 .user = 
@@ -505,37 +505,39 @@
 cts() %7B%0A        
-model
+service
 .projects = Proj
@@ -558,21 +558,23 @@
         
-model
+service
 .project
@@ -2308,51 +2308,15 @@
     
-//model.markers = Marker.query();%0A    model
+service
 .get
@@ -2381,21 +2381,23 @@
 %7D;%0A%0A    
-model
+service
 .fluoroc
@@ -2428,29 +2428,31 @@
 ery();%0A%0A    
-model
+service
 .getFluoroch
@@ -2512,37 +2512,39 @@
 es;%0A    %7D;%0A%0A    
-model
+service
 .getParameterFun
@@ -2639,29 +2639,31 @@
     %7D;%0A%0A    
-model
+service
 .getParamete
@@ -2760,37 +2760,39 @@
  );%0A    %7D;%0A%0A    
-model
+service
 .getProjects = f
@@ -2851,21 +2851,23 @@
  %7D;%0A    
-model
+service
 .reloadP
@@ -2926,29 +2926,31 @@
     %7D;%0A%0A    
-model
+service
 .getProjectB
@@ -3156,37 +3156,39 @@
 ll;%0A    %7D;%0A%0A    
-model
+service
 .setCurrentSite 
@@ -3293,37 +3293,39 @@
 ');%0A    %7D;%0A%0A    
-model
+service
 .getCurrentSite 
@@ -3378,37 +3378,39 @@
 te;%0A    %7D;%0A%0A    
-model
+service
 .setCurrentSampl
@@ -3521,37 +3521,39 @@
 ');%0A    %7D;%0A%0A    
-model
+service
 .getCurrentSampl
@@ -3618,21 +3618,23 @@
 %7D;%0A%0A    
-model
+service
 .setCurr
@@ -3783,21 +3783,23 @@
 %7D;%0A%0A    
-model
+service
 .getCurr
@@ -3898,13 +3898,15 @@
 urn 
-model
+service
 ;%0A%7D)

rename returned object in ModelService: model -> service