'use strict';

var services = angular.module('picard.services', ['ngResource']);

services.factory('Tax', ['$resource', function ($resource) {
    return $resource('/api/Tax/:id',
        {id: '@TaxID'}, {
            query: {method: 'GET', params: {l: ''}, isArray: true},
            small: {method: 'GET', params: {s: ''}, isArray: true},
            autocomplete: {method: 'GET', params: {q: ''}, isArray: true}
        });
}]);

services.factory('ProductGroup', ['$resource', function ($resource) {
    return $resource('/api/ProductGroup/:id',
        {id: '@ProductGroupID'}, {
            query: {method: 'GET', params: {l: ''}, isArray: true},
            small: {method: 'GET', params: {s: ''}, isArray: true},
            tree: {method: 'GET', params: {t: ''}, isArray: true}
        });
}]);

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

services.factory('FoodTable', ['$resource', function ($resource) {
    return $resource('/api/FoodTable/:id',
        {id: '@FoodTableID'}, {
            query: {method: 'GET', params: {l: ''}, isArray: true},
            small: {method: 'GET', params: {s: ''}, isArray: true},
            autocomplete: {method: 'GET', params: {q: ''}, isArray: true}
        });
}]);

services.factory('Modifier', ['$resource', function ($resource) {
    return $resource('/api/Modifier/:id',
        {id: '@ModifierID'}, {
            query: {method: 'GET', params: {l: ''}, isArray: true},
            small: {method: 'GET', params: {s: ''}, isArray: true},
            autocomplete: {method: 'GET', params: {q: ''}, isArray: true}
        });
}]);

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

services.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}
        });
}]);

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

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

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

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

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

services.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}
        });
}]);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

services.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}
        });
}]);
services.factory('Message', ['$resource', function ($resource) {
    return $resource('/api/Message/:id',
        {id: '@ThreadID'}, {
            query: {method: 'GET', params: {list: true}}
        });
}]);

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

services.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'
        }
    }
});