'use strict';
(function (angular) {
    angular
        .module('peoplePluginContent')
        .controller('ContentPeopleCtrl', ['$scope', 'Location', '$modal', 'Buildfire', 'TAG_NAMES', 'STATUS_CODE', '$routeParams',
            function ($scope, Location, $modal, Buildfire, TAG_NAMES, STATUS_CODE, $routeParams) {
                var ContentPeople = this;
                ContentPeople.isUpdating = false;
                ContentPeople.linksSortableOptions = {
                    handle: '> .cursor-grab'
                };
                ContentPeople.item = {
                    data: {
                        topImage: '',
                        iconImage: '',
                        fName: '',
                        lName: '',
                        position: '',
                        deepLinkUrl: '',
                        dateCreated: +new Date(),
                        socailLinks: [],
                        bodyContent: ''
                    }
                };
                updateMasterItem(ContentPeople.item);
                function updateMasterItem(item) {
                    ContentPeople.masterItem = angular.copy(item);
                }
                function resetItem() {
                    ContentPeople.item = angular.copy(ContentPeople.masterItem);
                }
                function isUnchanged(item) {
                    return angular.equals(item, ContentPeople.masterItem);
                }
                /*On click button done it redirects to home*/
                ContentPeople.done = function () {
                    Location.goToHome();
                };

                /*On click button delete it removes current item from datastore*/
                ContentPeople.deleteItem = function () {
                    var item = ContentPeople.item;
                    if (item.id) {
                        Buildfire.datastore.delete(item.id, TAG_NAMES.PEOPLE, function (err, result) {
                            if (err)
                                return;
                            Location.goToHome();
                        });
                    }
                };
                ContentPeople.getItem = function (itemId) {
                    Buildfire.datastore.getById(itemId, TAG_NAMES.PEOPLE, function (err, data) {
                        if (err)
                            console.error('There was a problem saving your data');
                        ContentPeople.item = data;
                        updateMasterItem(ContentPeople.item);
                        $scope.$digest();
                    });
                };
                if ($routeParams.itemId) {
                    ContentPeople.getItem($routeParams.itemId);
                }
                ContentPeople.addNewItem = function () {
                    ContentPeople.item.data.dateCreated = new Date(),
                        Buildfire.datastore.insert(ContentPeople.item.data, TAG_NAMES.PEOPLE, false, function (err, data) {
                            if (err)
                                return console.error('There was a problem saving your data');
                            ContentPeople.getItem(data.id);
                        });
                };
                ContentPeople.updateItemData = function () {
                    if (ContentPeople.item.id) {
                        Buildfire.datastore.update(ContentPeople.item.id, ContentPeople.item.data, TAG_NAMES.PEOPLE, function (err) {
                            if (err)
                                console.error('There was a problem saving your data');
                        })
                    } else {
                        console.error('Blank id Provided');
                    }
                };
                Buildfire.datastore.onUpdate(function (event) {
                    if (event && event.status) {
                        switch (event.status) {
                            case STATUS_CODE.INSERTED:
                                console.log('Data inserted Successfully');
                                break;
                            case STATUS_CODE.UPDATED:
                                console.log('Data updated Successfully');
                                break;
                        }
                    }
                });

                ContentPeople.openAddLinkPopup = function () {
                    var modalInstance = $modal
                        .open({
                            templateUrl: 'people/modals/add-item-link.html',
                            controller: 'AddItemLinkPopupCtrl',
                            controllerAs: 'AddItemLinkPopup',
                            size: 'sm'
                        });
                    modalInstance.result.then(function (_link) {
                        if (_link) {
                            ContentPeople.item.data.socailLinks.push(JSON.parse(angular.toJson(_link)));
                        }
                    }, function (err) {
                        if (err) {
                            console.error('Error:', err)
                        }
                    });
                };

                ContentPeople.removeLink = function (_index) {
                    ContentPeople.item.data.socailLinks.splice(_index, 1);
                };

                var options = {showIcons: false, multiSelection: false};
                var callback = function (error, result) {
                    if (error) {
                        console.error('Error:', error);
                    } else {
                        ContentPeople.item.data.topImage = result.selectedFiles && result.selectedFiles[0] || null;
                        $scope.$digest();
                    }
                };

                ContentPeople.selectTopImage = function () {
                    Buildfire.imageLib.showDialog(options, callback);
                };

                ContentPeople.removeTopImage = function () {
                    ContentPeople.item.data.topImage = null;
                };


                var tmrDelayForPeoples = null;
                var updateItemsWithDelay = function (newObj) {
                    if (tmrDelayForPeoples) clearTimeout(tmrDelayForPeoples);
                    if (ContentPeople.item && !isUnchanged(ContentPeople.item)) {
                        if (ContentPeople.item.id) {
                            tmrDelayForPeoples = setTimeout(function () {
                                ContentPeople.updateItemData();
                            }, 500);
                        }
                        else {
                            tmrDelayForPeoples = setTimeout(function () {
                                ContentPeople.addNewItem();
                            }, 500);
                        }
                    }

                };

                $scope.$watch(function () {
                    return ContentPeople.item;
                }, updateItemsWithDelay, true);
            }]);
})(window.angular);

@@ -1167,32 +1167,33 @@
               %7D%0A
+%0A
                 
@@ -1306,32 +1306,33 @@
               %7D%0A
+%0A
                 
@@ -1445,32 +1445,33 @@
               %7D%0A
+%0A
                 
@@ -2864,38 +2864,34 @@
 ted = new Date()
-,%0A    
+;%0A
                 
@@ -2990,32 +2990,86 @@
 rr, data) %7B%0A    
+                    ContentPeople.isUpdating = false;%0A
                 
@@ -3077,36 +3077,32 @@
        if (err)%0A
-    
                 
@@ -3191,36 +3191,32 @@
                 
-    
 ContentPeople.ge
@@ -3227,28 +3227,24 @@
 m(data.id);%0A
-    
             
@@ -3510,32 +3510,94 @@
 unction (err) %7B%0A
+                            ContentPeople.isUpdating = false;%0A
                 
@@ -3744,39 +3744,32 @@
                %7D
- else %7B
 %0A               
@@ -3777,69 +3777,41 @@
     
-    console.error('Blank id Provided');%0A                    %7D
+ContentPeople.isUpdating = false;
 %0A   
@@ -6373,16 +6373,44 @@
 .item &&
+ ContentPeople.isUpdating &&
  !isUnch
@@ -6434,24 +6434,81 @@
 le.item)) %7B%0A
+                        ContentPeople.isUpdating = true;%0A
             

refactor of code