module insite.vmiCount {
    "use strict";
    import VmiCountCollectionModel = Insite.Catalog.WebApi.V1.ApiModels.VmiCountCollectionModel;
    import VmiCountModel = Insite.Catalog.WebApi.V1.ApiModels.VmiCountModel;

    export interface ISearchFilter {
        userName?: string;
        previousCountFromDate?: string;
        previousCountToDate?: string;
        getUniqueByUser?: boolean;
        sort?: string;
        orderStatus?: string;
    }

    export interface IVmiCountService {
        getVmiCounts(vmiLocationId: System.Guid, vmiBinId: System.Guid, filter: ISearchFilter, pagination: PaginationModel): ng.IPromise<VmiCountCollectionModel>;
        addVmiCountCollection(vmiLocationId: System.Guid, vmiCounts: VmiCountModel[]): ng.IPromise<VmiCountCollectionModel>;
        addVmiCount(vmiLocationId: System.Guid, vmiBinId: System.Guid, productId: System.Guid, count: number): ng.IPromise<VmiCountModel>;
    }

    export class VmiCountService implements IVmiCountService {
        serviceUri = "/api/v1/vmiLocations/{vmiLocationId}/bins/{vmiBinId}/binCounts";

        static $inject = ["$http", "httpWrapperService"];

        constructor(
            protected $http: ng.IHttpService,
            protected httpWrapperService: core.HttpWrapperService) {
        }

        getVmiCounts(vmiLocationId: System.Guid, vmiBinId: System.Guid, filter: ISearchFilter, pagination: PaginationModel): ng.IPromise<VmiCountCollectionModel> {
            return this.httpWrapperService.executeHttpRequest(
                this,
                this.$http({
                    url: this.serviceUri.replace("{vmiLocationId}", vmiLocationId.toString()).replace("{vmiBinId}", vmiBinId.toString()),
                    method: "GET",
                    params: this.getVmiCountsParams(filter, pagination)
                }),
                this.getVmiCountsCompleted,
                this.getVmiCountsFailed
            );
        }

        protected getVmiCountsParams(filter: ISearchFilter, pagination: PaginationModel): any {
            const params: any = filter ? JSON.parse(JSON.stringify(filter)) : {};

            if (pagination) {
                params.page = pagination.page;
                params.pageSize = pagination.pageSize;
            }

            return params;
        }

        protected getVmiCountsCompleted(response: ng.IHttpPromiseCallbackArg<VmiCountCollectionModel>): void {
        }

        protected getVmiCountsFailed(error: ng.IHttpPromiseCallbackArg<any>): void {
        }

        addVmiCount(vmiLocationId: System.Guid, vmiBinId: System.Guid, productId: System.Guid, count: number): ng.IPromise<VmiCountModel> {
            return this.httpWrapperService.executeHttpRequest(
                this,
                this.$http({
                    method: "POST",
                    url: this.serviceUri.replace("{vmiLocationId}", vmiLocationId.toString()).replace("{vmiBinId}", vmiBinId.toString()),
                    data: this.addVmiCountParams(productId, count)
                }),
                this.addVmiCountCompleted,
                this.addVmiCountFailed);
        }

        protected addVmiCountParams(productId: System.Guid, count: number): any {
            const params: any = {
                productId: productId,
                count: count
            };

            return params;
        }

        protected addVmiCountCompleted(response: ng.IHttpPromiseCallbackArg<VmiCountModel>): void {
        }

        protected addVmiCountFailed(error: ng.IHttpPromiseCallbackArg<any>): void {
        }
        
        addVmiCountCollection(vmiLocationId: System.Guid, vmiCounts: VmiCountModel[]): ng.IPromise<VmiCountCollectionModel> {
            return this.httpWrapperService.executeHttpRequest(
                this,
                this.$http({
                    url: `${this.serviceUri.replace("{vmiLocationId}", vmiLocationId.toString())}/batch`,
                    method: "POST",
                    data: vmiCounts
                }),
                this.addVmiCountCollectionCompleted,
                this.addVmiCountCollectionFailed
            );
        }

        protected addVmiCountCollectionCompleted(response: ng.IHttpPromiseCallbackArg<VmiCountCollectionModel>): void {
        }

        protected addVmiCountCollectionFailed(error: ng.IHttpPromiseCallbackArg<any>): void {
        }
    }

    angular
        .module("insite")
        .service("vmiCountService", VmiCountService);
}