/**
 * Created by Malindu Warapitiya on 3/15/16.
 */

angular.module('phonebook.controllers', [])
    .controller('HomeController', ['$uibModal', 'dataService', homeController]);

function homeController($uibModal, dataService) {

    //scope
    var vm = this;

    //list of contacts
    vm.phonebooklist = [];

    //contact details object
    vm.phonebookdetails = {};

    //copy of contact details object with active property
    vm.phonebookselected = {};

    //item click event function
    vm.itemClicked = itemClicked;

    //create a new contact
    vm.createContact = createContact;

    //remove a contact
    vm.removeContact = removeContact;

    vm.isContactSelected = isContactSelected;

    vm.updateContact = updateContact;


    /**
     * get all contacts
     */
    dataService.getContacts().then(function (_data) {
        vm.phonebooklist = _data;
    }, function (_error) {
        console.error(_error);
    });


    /**
     * Update a data record
     * @param type
     * @param _data
     */
    function updateContact(type, _data) {

        if (_.isUndefined(_data) || _.isNull(_data)) {
            return "*Required";
        }

        if (_.isEqual(type, 'number')) {

            var _string = _.clone(_data) + "";

            console.log(_string.length);

            if (_.lt(_string.length, 7)) {
                return "Should have minimum 7 numbers";
            }
        }

        var _temp = {
            _id: vm.phonebookdetails._id
        };

        _temp[type] = _data;

        dataService.updateContact(_temp)
            .then(function (_result) {

                console.log("Saved!");

            }, function (_error) {
                console.error(_error);
            })
    }


    /**
     * Is contact selected to diplay details
     * @returns {boolean}
     */
    function isContactSelected() {
        return _.isEmpty(vm.phonebookdetails);
    }


    /**
     * on click event for item click
     * @param _record
     */
    function itemClicked(_record) {

        //skip if not null
        if (!_.isNull(vm.phonebookselected)) {
            //delete the active property
            delete vm.phonebookselected.active;
        }

        //add active property
        _record.active = true;

        //cache a copy of _record
        vm.phonebookselected = _record;

        //deep clone the object
        vm.phonebookdetails = _.clone(_record);
    }


    function removeContact() {

        console.log(vm.phonebookselected._id);

        var _data = _.find(vm.phonebooklist, function (o) {
            return _.isEqual(o._id, vm.phonebookselected._id);
        });

        console.log(_data);

        dataService.removeContact(_data._id).then(function (_data) {
            console.log(_data);

            var _id = _.findIndex(vm.phonebooklist, function (o) {
                return _.isEqual(o._id, vm.phonebookselected._id);
            });

            delete vm.phonebooklist[_id];

        }, function (_error) {
            console.error(_error);
        });

    }


    /**
     * Create a new contact modal
     */
    function createContact() {

        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'home/create-contact/create.contact.view.html',
            controller: 'CreateContact',
            controllerAs: 'vm',
            size: 'sm',
            resolve: {
                items: function () {
                    return null;
                }
            }
        });

        modalInstance.result.then(function (_data) {
            vm.phonebooklist.push(_data);
        }, function () {
            console.log('Modal dismissed at: ' + new Date());
        });

    }

}
@@ -1057,24 +1057,61 @@
 , _data) %7B%0A%0A
+        //check if undefined or null%0A
         if (
@@ -1188,32 +1188,74 @@
 ed%22;%0A        %7D%0A%0A
+        //check the minimum number length%0A
         if (_.is
@@ -1332,50 +1332,8 @@
 %22;%0A%0A
-            console.log(_string.length);%0A%0A
     
@@ -1444,32 +1444,74 @@
    %7D%0A        %7D%0A%0A
+        //create the _temp object to edit%0A
         var _tem
@@ -1599,16 +1599,60 @@
 _data;%0A%0A
+%0A        //call the data service for update%0A
         
@@ -1852,16 +1852,17 @@
       %7D)
+;
 %0A    %7D%0A%0A
@@ -2147,24 +2147,68 @@
 _record) %7B%0A%0A
+        if (!_.isUndefined(_record)) %7B%0A%0A    
         //sk
@@ -2222,32 +2222,36 @@
 ot null%0A        
+ 
+   
 if (!_.isNull(vm
@@ -2281,24 +2281,28 @@
             
+    
 //delete the
@@ -2322,32 +2322,36 @@
 rty%0A            
+  
+  
 delete vm.phoneb
@@ -2378,27 +2378,34 @@
         
-%7D%0A%0A
+    %7D%0A%0A   
         
 //add ac
@@ -2396,16 +2396,17 @@
         
+ 
 //add ac
@@ -2411,32 +2411,36 @@
 active property%0A
+    
         _record.
@@ -2459,24 +2459,28 @@
 e;%0A%0A        
+   
+ 
 //cache a co
@@ -2489,24 +2489,28 @@
  of _record%0A
+    
         vm.p
@@ -2534,24 +2534,28 @@
 = _record;%0A%0A
+    
         //de
@@ -2574,32 +2574,36 @@
  object%0A        
+    
 vm.phonebookdeta
@@ -2626,27 +2626,85 @@
 ecord);%0A    
-%7D%0A%0A
+    %7D%0A    %7D%0A%0A%0A    /**%0A     * Remove contact from list%0A     */
 %0A    functio
@@ -3009,40 +3009,8 @@
 a) %7B
-%0A            console.log(_data);
 %0A%0A  
@@ -3162,39 +3162,168 @@
 );%0A%0A            
-delete 
+vm.phonebooklist.splice(1, _id);%0A%0A            vm.phonebookdetails = %7B%7D;%0A            vm.phonebookselected = %7B%7D;%0A%0A            console.log(
 vm.phonebooklist
@@ -3326,13 +3326,9 @@
 list
-%5B_id%5D
+)
 ;%0A%0A 

fix bug which add null value to array of phonebooklist