﻿angular.module('searchOtherService', []).factory('searchOtherService', ['customerRecyclingHistoryService', 'orderAndInvoiceService', function (customerRecyclingHistoryService, orderAndInvoiceService) {
    var service = {
        search: search,
        searchOrderInvoice: searchOrderInvoice,
        shower: {
            showLoader: false,
        },
        customerRecyclingHistorysProperties: [],
        orderAndInvoiceProperties:[],
        searchResult: {
            data: [],
            searchTotal: 0,
            searchPages: 0,
        },
        searchProperties: {
            properties: [],
            query: "",
            countryCode:"",
            invoiceId:"",
            orderNo:"",
            page: 1,
            pageAmount: 10
        },
        loadCustomerRecyclingHistorysProperties: loadCustomerRecyclingHistorysProperties,
        loadorderAndInvoiceProperties: loadorderAndInvoiceProperties,
        setSearchPropertiesDefaults: setSearchPropertiesDefaults,
        setSearchResultDefaults: setSearchResultDefaults
    }

    function setSearchResultDefaults() {
        service.searchResult.data = [];
        service.searchResult.searchTotal = "";
        service.searchResult.searchPages = 0;
        service.shower.showLoader = false;
    }

    function setSearchPropertiesDefaults() {
        service.searchProperties = {
            properties: [],
            query: "",
            countryCode: "",
            page: 1,
            pageAmount: 10
        }
    }

    function loadCustomerRecyclingHistorysProperties() {
        service.customerRecyclingHistorysProperties = customerRecyclingHistoryService.getProperties().then(
            function (response) {
                var customerRecyclingHistorysProperties = [];
                angular.forEach(response.d, function (value, key) {
                    customerRecyclingHistorysProperties.push({ checked: false, value: value.Name, type: value.Type });
                }, customerRecyclingHistorysProperties);
                service.customerRecyclingHistorysProperties = customerRecyclingHistorysProperties;
            });
    }

    function loadorderAndInvoiceProperties() {
        service.orderAndInvoiceProperties = orderAndInvoiceService.getProperties().then(
            function (response) {
                var orderAndInvoiceProperties = [];
                angular.forEach(response.d, function (value, key) {
                    orderAndInvoiceProperties.push({ checked: false, value: value.Name, type: value.Type });
                }, orderAndInvoiceProperties);
                service.orderAndInvoiceProperties = orderAndInvoiceProperties;
            });
    }

    function calcPagesCount(total, pageSize) {
        return Math.round(total / pageSize);
    }
    function search(query) {
        service.searchProperties.properties = [];
        angular.forEach(service.customerRecyclingHistorysProperties, function (value, key) {
            if (value.checked) {
                service.searchProperties.properties.push({ Name: value.value, Type: value.type });
            }
        });
        if (service.searchProperties.properties.length === 0) {
            angular.forEach(service.customerRecyclingHistorysProperties, function (value, key) {
                service.searchProperties.properties.push({ Name: value.value, Type: value.type });
            });
        }
        service.searchProperties.query = query
        service.searchResult.data = [];
        service.searchResult.searchTotal = "";
        service.searchResult.searchPages = 0;
        service.shower.showLoader = true;
        customerRecyclingHistoryService.search(
            {
                Query: service.searchProperties.query,
                Page: service.searchProperties.page,
                PageAmount: service.searchProperties.pageAmount,
                Properties: service.searchProperties.properties
            }).then(
            function (response) {
                angular.forEach(service.customerRecyclingHistorysProperties, function (value, key) {
                    if (value.checked) {
                        service.searchProperties.properties.push({ Name: value.value, Type: value.type });
                    }
                });
                service.searchResult.data = response.d.RecyclingSEs
                service.searchResult.searchTotal = response.d.Total;
                service.searchResult.searchPages = calcPagesCount(response.d.Total, service.searchProperties.pageAmount);
                service.shower.showLoader = false
            });;
    }

    function searchOrderInvoice(countryCode, query) {
        service.searchProperties.properties = [];
        angular.forEach(service.orderAndInvoiceProperties, function (value, key) {
            if (value.checked) {
                service.searchProperties.properties.push({ Name: value.value, Type: value.type });
            }
        });
        if (service.searchProperties.properties.length === 0) {
            angular.forEach(service.orderAndInvoiceProperties, function (value, key) {
                service.searchProperties.properties.push({ Name: value.value, Type: value.type });
            });
        }
        service.searchProperties.countryCode = countryCode;
        service.searchProperties.query = query;

        service.searchResult.data = [];
        service.searchResult.searchTotal = "";
        service.searchResult.searchPages = 0;
        service.shower.showLoader = true;

        var useInvoiceId = service.searchProperties
                .properties.filter(function (value) {
                    return value.Name == "InvoiceId"
                }).length > 0;

        var useOrderNo = service.searchProperties
                .properties.filter(function (value) {
                    return value.Name == "ERPOrderId"
                }).length > 0;

        orderAndInvoiceService.search(
            {
                CountryCode: service.searchProperties.countryCode,
                InvoiceId: useInvoiceId ? service.searchProperties.query : "",
                OrderNo: useOrderNo ? service.searchProperties.query : "",
                Page: service.searchProperties.page,
                PageAmount: service.searchProperties.pageAmount,
                Properties: service.searchProperties.properties
            }).then(
                function (response) {
                    angular.forEach(service.orderAndInvoiceProperties, function (value, key) {
                        if (value.checked) {
                            service.searchProperties.properties.push({ Name: value.value, Type: value.type });
                        }
                    });
                    console.log(response.d)
                    service.searchResult.data = response.d.OrderAndInvoices;
                    service.searchResult.searchTotal = response.d.Total;
                    service.searchResult.searchPages = calcPagesCount(response.d.Total, service.searchProperties.pageAmount);
                    service.shower.showLoader = false
                });;
    }
    return service;
}]);
