var eHaveApp = angular.module('eHaveApp', [ "kendo.directives" ]);

    // create the controller and inject Angular's $scope
    eHaveApp.controller('mainController', ['$scope', '$filter', 'dataService', function($scope, $filter, dataService) {
        
        var init = function () {
            makeGrid();
            //$scope.masterGrid.dataSource.read();
        }

        $scope.masterGrid = {};
        
         var getPatientData = function (options){
            return dataService.getPatientData(options.data).then(
                function successCallback(response) {
                    options.success(response);
                },
                function errorCallback(response) {
                    // handle error
                }
            );
        };
        
        var getTestData = function (options){
            return dataService.getTestData(options.data).then(
                function successCallback(response) {
                    options.success(response);
                },
                function errorCallback(response) {
                    // handle error
                }
            );
        };
        
        $scope.ctrlRemovePatient = function (dataItem) {
            if (confirm('Are you sure you want to delete patient ' + dataItem.firstName + " " + dataItem.lastName + '?\n(DISABLED while using mock data)')) {
                // TODO:  Do something here if the answer is "Ok".
            }
        };
        
        $scope.ctrlRemoveTest = function (dataItem) {
            if (confirm('Are you sure you want to delete test ' + dataItem.timestamp + '?\n(DISABLED while using mock data)')) {
                // TODO:  Do something here if the answer is "Ok".
            }
        };
        
        var makeGrid = function () {
            $scope.masterGrid.dataSource = new kendo.data.DataSource({
                schema:{
                    data: function (response) {
                        return response.data.Data;
                    },
                    model: {
                        fields: {
                            MRN:  { type: "string", editable: false },
                        }
                    },
                    total: function(response) {
                        return response.data.Total;
                    }
                },
                transport:{
                    read: function(options) {
                        return getPatientData(options);
                    },
                },
                pageSize: 5,
                serverPaging: true,
                serverSorting: true,
                sort: { field: "MRN", dir: "desc" }
            });

            $scope.masterGrid.gridOptions = {
                columns: [
                    {
                        title: "MRN",
                        field: "MRN",
                        width: "110px",
                        sortable: true
                    },{
                        title: "First Name",
                        field: "firstName"
                    },{
                        title: "Last Name",
                        field: "lastName"
                    },{
                        title: "Gender",
                        field: "sex",
                        width: "80px",
                        template: kendo.template($("#genderTemplate").html()),
                    },{
                        title: "DoB",
                        field: "birthDate",
                        width: "110px",
                    },{
                        template: kendo.template($("#removePatientTemplate").html()),
                        title: "Remove",
                        width: "80px",
                        filterable: false,
                        sortable: false
                    }
                ],
                dataBound: function() {
                    this.expandRow(this.tbody.find("tr.k-master-row").first());
                },
                selectable: 'row',
                pageable: true,
                change: function () {
                    var selectedRows = this.select();
                    var rowData = this.dataItem(selectedRows[0]);
                }
            };
            
            $scope.detailGridOptions = function(dataItem) {
                return {
                    dataSource: {
                        transport: {
                            read: function(options) {
                                options.data.MRN = dataItem.MRN; 
                                return getTestData(options);
                            }
                        },
                        serverPaging: true,
                        serverSorting: true,
                        serverFiltering: true,
                        pageSize: 5
                    },
                    scrollable: false,
                    sortable: true,
                    pageable: true,
                    columns: [
                        { 
                            field: "timestamp", 
                            title:"Date/Time",
                            template: function(item) {
                                return $filter('date')(item.timestamp, 'MMM dd, yyyy h:mm a');
                            }
                        },{ 
                            field: "resultType",
                            title:"Type" 
                        },{ 
                            field: "resultValue", 
                            title:"Value" 
                        },{
                            template: kendo.template($("#removeTestTemplate").html()),
                            title: "Remove",
                            width: "80px",
                            filterable: false,
                            sortable: false
                        }
                    ]
                };
            };            
        };
        
        init();
        
    }]);