module insite.catalog {
    "use strict";

    export class VmiReportingController extends BaseVmiBinsController {
        paginationStorageKey = "DefaultPaginationVmiBinList";
        time = new Date().getTime();
        numberOfPreviousOrders: number;
        numberOfTimesMinQtyReached: number;
        numberOfVisits: number;

        $onInit(): void {
            this.initParams();

            this.$scope.$on("$locationChangeSuccess", (event, newUrl: string, oldUrl: string) => {
                if (newUrl.toLowerCase().indexOf(this.vmiReportingPageUrl.toLowerCase()) !== -1 && newUrl !== oldUrl) {
                    this.initParams();
                    this.getVmiBins();
                }
            });

            super.$onInit();
        }

        initParams(): void {
            this.locationId = this.queryString.get("locationId");

            if (!this.locationId) {
                this.selectedLocation = undefined;
            }
            else if (this.vmiLocations && this.vmiLocations.length > 0) {
                if (this.locationId === "") {
                    this.selectedLocation = this.vmiLocations[0];
                    this.locationId = this.selectedLocation.id.toString();
                    this.updateLocationIdQueryString(true);
                } else {
                    this.selectedLocation = this.vmiLocations?.find(location => location.id.toLowerCase() === this.locationId?.toLowerCase());
                }
            }

            var metric = this.queryString.get("metric");
            var numberOfPreviousOrders = this.queryString.get("numberOfPreviousOrders");
            var numberOfTimesMinQtyReached = this.queryString.get("numberOfTimesMinQtyReached");
            var numberOfVisits = this.queryString.get("numberOfVisits");

            this.metric = (metric && metric != '') ? metric : "allproducts";

            this.numberOfPreviousOrders = (numberOfPreviousOrders !== "") ? numberOfPreviousOrders : (this.metric === "slowmoving") ? 0 : undefined;
            this.numberOfTimesMinQtyReached = (numberOfTimesMinQtyReached !== "") ? numberOfTimesMinQtyReached : (this.metric === "fastmoving") ? 2 : undefined;
            this.numberOfVisits = (numberOfVisits !== "") ? numberOfVisits : (this.metric === "slowmoving") ? 2 : (this.metric === "fastmoving") ? 4 : undefined;

            this.searchFilter.isBelowMinimum = this.metric === "belowminimum";
            this.searchFilter.numberOfPreviousOrders = this.numberOfPreviousOrders;
            this.searchFilter.numberOfTimesMinQtyReached = this.numberOfTimesMinQtyReached;
            this.searchFilter.numberOfVisits = this.numberOfVisits;

            this.coreService.replaceState({ filter: this.searchFilter });
        }

        changeMetric(): void {
            if (this.pagination) {
                this.pagination.page = 1;
            }
            this.resetFilter();
        }

        resetFilter(): void {
            this.numberOfPreviousOrders = (this.metric === "slowmoving") ? 0 : undefined;
            this.numberOfTimesMinQtyReached = (this.metric === "fastmoving") ? 2 : undefined;
            this.numberOfVisits = (this.metric === "slowmoving") ? 2 : (this.metric === "fastmoving") ? 4 : undefined;

            this.searchFilter.isBelowMinimum = this.metric === "belowminimum";
            this.searchFilter.numberOfPreviousOrders = this.numberOfPreviousOrders;
            this.searchFilter.numberOfTimesMinQtyReached = this.numberOfTimesMinQtyReached;
            this.searchFilter.numberOfVisits = this.numberOfVisits;

            this.updateQueryStrings();
        }

        reloadVmiBins(): void {
            this.updateQueryStrings();
        }

        updateQueryStrings(): void {
            this.$timeout(() => {
                this.$location.search("metric", this.metric);
                this.$location.search("numberOfPreviousOrders", this.numberOfPreviousOrders);
                this.$location.search("numberOfTimesMinQtyReached", this.numberOfTimesMinQtyReached);
                this.$location.search("numberOfVisits", this.numberOfVisits);
            }, 0);
        }
    }

    angular
        .module("insite")
        .controller("VmiReportingController", VmiReportingController);
}