'use strict';

var overlord_service = angular.module('overlord.service', ['ngResource']);

overlord_service.factory('IssueGrid', ['$resource', function ($resource) {
    return $resource('/api/Issues/Services/:date');
}]);

overlord_service.factory('Voucher', ['$resource', function ($resource) {
    return $resource('/api/Voucher/:id',
        {id: '@VoucherID'}, {
            post: {method: 'POST', params: {post: true}}
        });
}]);

overlord_service.factory('Account', ['$resource', function ($resource) {
    return $resource('/api/Account/:id',
        {id: '@LedgerID'}, {
            query: {method: 'GET', params: {list: true}, isArray: true},
            autocomplete: {method: 'GET', params: {term: ''}, isArray: true}
        });
}]);

overlord_service.factory('Employee', ['$resource', function ($resource) {
    return $resource('/api/Employee/:id',
        {id: '@LedgerID'}, {
            query: {method: 'GET', params: {list: true}, isArray: true},
            autocomplete: {method: 'GET', params: {term: ''}, isArray: true}
        });
}]);

overlord_service.factory('Ledger', ['$resource', function ($resource) {
    return $resource('/api/Ledger/:id');
}]);

overlord_service.factory('Reconcile', ['$resource', function ($resource) {
    return $resource('/api/Reconcile/:id', {id: '@Account.LedgerID'});
}]);

overlord_service.factory('ProductLedger', ['$resource', function ($resource) {
    return $resource('/api/ProductLedger/:id');
}]);

overlord_service.factory('CostCenter', ['$resource', function ($resource) {
    return $resource('/api/CostCenter/:id',
        {id: '@CostCenterID'}, {
            query: {method: 'GET', params: {list: true}, isArray: true}
        });
}]);

overlord_service.factory('AccountType', ['$resource', function ($resource) {
    return $resource('/api/AccountTypes');
}]);

overlord_service.factory('User', ['$resource', function ($resource) {
    return $resource('/api/User/:id',
        {id: '@UserID'}, {
            query: {method: 'GET', params: {list: true}, isArray: true},
            names: {method: 'GET', params: {names: true}, isArray: true}
        });
}]);

overlord_service.factory('Group', ['$resource', function ($resource) {
    return $resource('/api/Group/:id',
        {id: '@GroupID'}, {
            query: {method: 'GET', params: {list: true}, isArray: true}
        });
}]);

overlord_service.factory('Client', ['$resource', function ($resource) {
    return $resource('/api/Client/:id',
        {id: '@ClientID'}, {
            query: {method: 'GET', params: {list: true}, isArray: true}
        });
}]);

overlord_service.factory('Product', ['$resource', function ($resource) {
    return $resource('/api/Product/:id',
        {id: '@ProductID'}, {
            query: {method: 'GET', params: {list: true}, isArray: true},
            autocomplete: {method: 'GET', params: {term: ''}, isArray: true}
        });
}]);

overlord_service.factory('Batch', ['$resource', function ($resource) {
    return $resource('/api/Batch', {}, {
        autocomplete: {method: 'GET', params: {term: ''}, isArray: true}
    });
}]);

overlord_service.factory('ProductGroup', ['$resource', function ($resource) {
    return $resource('/api/ProductGroup/:id',
        {id: '@ProductGroupID'}, {
            query: {method: 'GET', params: {list: true}, isArray: true}
        });
}]);

overlord_service.factory('CostCenter', ['$resource', function ($resource) {
    return $resource('/api/CostCenter/:id',
        {id: '@CostCenterID'}, {
            query: {method: 'GET', params: {list: true}, isArray: true}
        });
}]);

overlord_service.factory('TrialBalance', ['$resource', function ($resource) {
    return $resource('/api/TrialBalance/:date');
}]);

overlord_service.factory('ClosingStock', ['$resource', function ($resource) {
    return $resource('/api/ClosingStock/:date');
}]);

overlord_service.factory('CashFlow', ['$resource', function ($resource) {
    return $resource('/api/CashFlow/:id');
}]);

overlord_service.factory('Daybook', ['$resource', function ($resource) {
    return $resource('/api/Daybook');
}]);

overlord_service.factory('Unposted', ['$resource', function ($resource) {
    return $resource('/api/Unposted');
}]);

overlord_service.factory('ProfitLoss', ['$resource', function ($resource) {
    return $resource('/api/ProfitLoss');
}]);

overlord_service.factory('StockMovement', ['$resource', function ($resource) {
    return $resource('/api/StockMovement');
}]);

overlord_service.factory('BalanceSheet', ['$resource', function ($resource) {
    return $resource('/api/BalanceSheet/:date');
}]);

overlord_service.factory('NetTransactions', ['$resource', function ($resource) {
    return $resource('/api/NetTransactions');
}]);

overlord_service.factory('Purchases', ['$resource', function ($resource) {
    return $resource('/api/Purchases');
}]);

overlord_service.factory('PurchaseEntries', ['$resource', function ($resource) {
    return $resource('/api/PurchaseEntries');
}]);

overlord_service.factory('RawMaterialCost', ['$resource', function ($resource) {
    return $resource('/api/RawMaterialCost/:id');
}]);

overlord_service.factory('Auth', ['$resource', function ($resource) {
    return $resource('/api/Auth', {}, {
        login: {method: 'GET', params: {list: true}, isArray: true}
    });
}]);

overlord_service.factory('Attendance', ['$resource', function ($resource) {
    return $resource('/api/Attendance/:date', {date: '@Date'});
}]);

overlord_service.factory('AttendanceTypes', ['$resource', function ($resource) {
    return $resource('/api/AttendanceTypes');
}]);

overlord_service.factory('EmployeeAttendance', ['$resource', function ($resource) {
    return $resource('/api/EmployeeAttendance/:id', {id: '@Employee.LedgerID'});
}]);

overlord_service.factory('Account', ['$resource', function ($resource) {
    return $resource('/api/Account/:id',
        {id: '@LedgerID'}, {
            query: {method: 'GET', params: {list: true}, isArray: true},
            autocomplete: {method: 'GET', params: {term: ''}, isArray: true}
        });
}]);
overlord_service.factory('Message', ['$resource', function ($resource) {
    return $resource('/api/Message/:id',
        {id: '@ThreadID'}, {
            query: {method: 'GET', params: {list: true}}
        });
}]);

overlord_service.factory('Tag', ['$resource', function ($resource) {
    return $resource('/api/Tags');
}]);

overlord_service.service('EventSource', function () {
    this.status = 'Disconnected';
    this.Open = function (url) {
        if (url) {
            this.url = url;
        }
        if (this.url) {
            this.source = new EventSource(url);
            if (this.status !== 'Open') {
                this.source.addEventListener('open', function (e) {
                    this.status = 'Open';
                }, false);
            }
        }
    };
    this.DataCallback = function (callback) {
        this.source.addEventListener('message', function (e) {
            var data = JSON.parse(e.data);
            callback(e);
        }, false);
    };
    this.source.addEventListener('error', function (e) {
        if (e.readyState == EventSource.CLOSED) {
            this.status = 'Error';
        }
    }, false);
    this.ErrorCallback = function (callback) {
        this.source.addEventListener('error', callback, false);
    }

    this.EventCallback = function (event, callback) {
        this.source.addEventListener(event, callback, false);
    }

    this.Close = function () {
        if (this.status === 'Open') {
            this.source.close();
            this.status = 'Closed'
        }
    }
});