import { PermissionCheckerService } from '@abp/auth/permission-checker.service';
import { AppSessionService } from '@shared/common/session/app-session.service';

import { Injectable } from '@angular/core';
import { AppMenu } from './app-menu';
import { AppMenuItem } from './app-menu-item';

@Injectable()
export class AppNavigationService {

    constructor(
        private _permissionCheckerService: PermissionCheckerService,
        private _appSessionService: AppSessionService
    ) {

    }

    getMenu(): AppMenu {
        return new AppMenu('MainMenu', 'MainMenu', [    
            new AppMenuItem('Dashboard', 'Pages.Administration.Host.Dashboard', 'flaticon-line-graph', '/app/admin/hostDashboard'),
            new AppMenuItem('Dashboard', 'Pages.Tenant.Dashboard', 'flaticon-line-graph', '/app/main/dashboard'),
           
            // Controller
            new AppMenuItem('Controller', '', 'flaticon-analytics', '/app/sprintship/controller-routes'),

            //Locations
            new AppMenuItem('Locations', '', 'flaticon2-map', '', [
                new AppMenuItem('Location', 'Pages.Administration.Locations', 'flaticon-location', '/app/sprintship/locations'),
                new AppMenuItem('Timezones', 'Pages.Administration.TimeZones', 'flaticon-globe', '/app/sprintship/timezones'),
                new AppMenuItem('Proximities', 'Pages.Administration.Proximities', 'flaticon2-wifi', '/app/sprintship/proximity'),
                new AppMenuItem('UserLocations', 'Pages.Administration.UserLocations', 'flaticon-user', '/app/sprintship/user-location'),
                new AppMenuItem('VehicleLocations', 'Pages.Administration.VehicleLocations', 'flaticon-car', '/app/sprintship/locationvehicle')
            ]),
            
            //Contacts
            new AppMenuItem('Contacts', '', 'flaticon-whatsapp', '',[
                new AppMenuItem('Contacts', 'Pages.Shipping.Contacts', 'flaticon-user-ok', '/app/sprintship/contacts'),
                new AppMenuItem('AddressTypes', 'Pages.Shipping.Contacts.AddressTypes', 'flaticon-layer', '/app/sprintship/address-types'),
                new AppMenuItem('Addresses', 'Pages.Shipping.Contacts.Address', 'flaticon-home', '/app/sprintship/addresses'),
                new AppMenuItem('Postal Codes', 'Pages.Shipping.Contacts.PostalCodes', 'flaticon-placeholder-2', '/app/sprintship/postal-codes')
                // new AppMenuItem('Emails', 'Pages.Shipping.Contacts.Emails', 'flaticon-book', '/app/sprintship/emails'),
            ]),

            //Employees
            new AppMenuItem('Employees', 'Pages.Administration.Employees', 'flaticon-users', '/app/sprintship/employees', [
                new AppMenuItem('Timesheets', 'Pages.Administration.Employees.TimeSheet', 'flaticon-time', '/app/sprintship/employees/timesheets'),
                new AppMenuItem('Reviews', 'Pages.Administration.Employees.EmployeeReview', 'flaticon-star', '/app/sprintship/employees/reviews'),
                new AppMenuItem('Driver Incidents', 'Pages.Administration.Employees.DriverIncidents', 'flaticon-folder-1', '/app/sprintship/employees/driver-incidents')
            ]),

            //Fleets
            new AppMenuItem('Fleets', '', 'flaticon-paper-plane', '', [
                new AppMenuItem('Fleets', 'Pages.Shipping.Fleets', 'flaticon-paper-plane-1', '/app/sprintship/fleets'),
                new AppMenuItem('Drivers', 'Pages.Administration.Users', 'flaticon2-avatar', '/app/sprintship/drivers'),
                new AppMenuItem('Vehicles', 'Pages.Shipping.Vehicles', 'flaticon-truck', '/app/sprintship/vehicle'),
                
                //new AppMenuItem('VehicleMileage', 'Pages.Shipping.Vehicle.Mileage', 'flaticon-truck', '/app/sprintship/vehiclemileage'), 
                //new AppMenuItem('Vehicles', 'Pages.Shipping.Fleet.Vehicles', 'flaticon-truck', '/app/sprintship/fleetvehicles'),

                //From Vehicle Tab
                //new AppMenuItem('Mileage', 'Pages.Shipping.Mileage', 'flaticon-time-1', '/app/sprintship/mileage'),
                //new AppMenuItem('VehicleMileage', 'Pages.Shipping.Vehicle.Mileage', 'flaticon-stopwatch', '/app/sprintship/vehiclemileage'), 
            ]),

            //Vendors
            new AppMenuItem('Vendors', '', 'flaticon-network', '', 
            [
                new AppMenuItem('Vendors', 'Pages.Shipping.Contacts', 'flaticon-shapes', '/app/sprintship/vendors')
            ]),

            //Packages
         
            new AppMenuItem('Packages', 'Pages.Shipping.Package', 'flaticon-open-box', '', [
                new AppMenuItem('Package Types', 'Pages.Shipping.Package.PackageType', 'flaticon2-setup', '/app/sprintship/package-types')
            ]),

            //Orders
            new AppMenuItem('Orders', '', 'flaticon-squares-4', '', [
                new AppMenuItem('Orders', 'Pages.Orders', 'flaticon2-box-1', '/app/sprintship/order'),
                new AppMenuItem('Tags', 'Pages.Orders.Tags', 'flaticon-tabs', '/app/sprintship/tags'),
                new AppMenuItem('OrderPackage', 'Pages.Orders.Package', 'flaticon2-box', '/app/sprintship/package-order'),
                new AppMenuItem('OrderStatus', 'Pages.Orders.Status', 'flaticon-internet', '/app/sprintship/status-order'),
            ]),

            //Routes
            new AppMenuItem('Routes', 'Pages.Shipping.Routes', 'flaticon-bus-stop', '', [
                new AppMenuItem('Routes', 'Pages.Shipping.Routes', 'flaticon2-favourite', '/app/sprintship/routes'),
                new AppMenuItem('Templates', 'Pages.Shipping.RoutesTemplate', 'flaticon-interface', '/app/sprintship/routes-template'),
                new AppMenuItem('Template Types', 'Pages.Shipping.RoutesTemplate', 'flaticon2-expand', '/app/sprintship/routes-template-types')
            ]),

            //Reports
            new AppMenuItem('Reports', '', 'flaticon-diagram', '', 
            [
            ]),

            //Notifications
            new AppMenuItem('Notifications', '', 'flaticon-alert', '', 
            [
            ]),

            //Schedules
            new AppMenuItem('Schedules', '', 'flaticon-clock-2', '', 
            [
            ]),

            //Incidents
            new AppMenuItem('Incidents', 'Pages.Administration.Employees.Incidents', 'flaticon-info', '/app/sprintship/incidents'),

            //System
            new AppMenuItem('System', '', 'flaticon-settings-1', '', 
            [
                new AppMenuItem('OrderStatusType', 'Pages.Orders.OrderStatusTypes', 'flaticon-bag ', '/app/sprintship/status-type'),
                new AppMenuItem('TagType', 'Pages.Orders.TagTypes', 'flaticon-book', '/app/sprintship/tag-type'),
            ]),
              
            new AppMenuItem('Tenants', 'Pages.Tenants', 'flaticon-list-3', '/app/admin/tenants'),
            new AppMenuItem('Editions', 'Pages.Editions', 'flaticon-app', '/app/admin/editions'),
              
            new AppMenuItem('Administration', '', 'flaticon-interface-8', '', [
                new AppMenuItem('OrganizationUnits', 'Pages.Administration.OrganizationUnits', 'flaticon-map', '/app/admin/organization-units'),
                new AppMenuItem('Roles', 'Pages.Administration.Roles', 'flaticon-suitcase', '/app/admin/roles'),
                new AppMenuItem('Users', 'Pages.Administration.Users', 'flaticon-user-settings', '/app/admin/users'),
                new AppMenuItem('Languages', 'Pages.Administration.Languages', 'flaticon2-open-text-book', '/app/admin/languages'),
                new AppMenuItem('AuditLogs', 'Pages.Administration.AuditLogs', 'flaticon-signs-1', '/app/admin/auditLogs'),
                new AppMenuItem('Maintenance', 'Pages.Administration.Host.Maintenance', 'flaticon-lock', '/app/admin/maintenance'),
                new AppMenuItem('Subscription', 'Pages.Administration.Tenant.SubscriptionManagement', 'flaticon-refresh', '/app/admin/subscription-management'),
                new AppMenuItem('VisualSettings', 'Pages.Administration.UiCustomization', 'flaticon-medical', '/app/admin/ui-customization'),
                new AppMenuItem('Settings', 'Pages.Administration.Host.Settings', 'flaticon-settings', '/app/admin/hostSettings'),
                new AppMenuItem('Settings', 'Pages.Administration.Tenant.Settings', 'flaticon-settings', '/app/admin/tenantSettings'),
               
            ]),
            new AppMenuItem('DemoUiComponents', 'Pages.DemoUiComponents', 'flaticon-shapes', '/app/admin/demo-ui-components')
        ]);
    }

    checkChildMenuItemPermission(menuItem): boolean {

        for (let i = 0; i < menuItem.items.length; i++) {
            let subMenuItem = menuItem.items[i];

            if (subMenuItem.permissionName === '' || subMenuItem.permissionName === null || subMenuItem.permissionName && this._permissionCheckerService.isGranted(subMenuItem.permissionName)) {
                return true;
            } else if (subMenuItem.items && subMenuItem.items.length) {
                return this.checkChildMenuItemPermission(subMenuItem);
            }
        }

        return false;
    }

    showMenuItem(menuItem: AppMenuItem): boolean {
        if (menuItem.permissionName === 'Pages.Administration.Tenant.SubscriptionManagement' && this._appSessionService.tenant && !this._appSessionService.tenant.edition) {
            return false;
        }

        let hideMenuItem = false;

        if (menuItem.requiresAuthentication && !this._appSessionService.user) {
            hideMenuItem = true;
        }

        if (menuItem.permissionName && !this._permissionCheckerService.isGranted(menuItem.permissionName)) {
            hideMenuItem = true;
        }

        if (this._appSessionService.tenant || !abp.multiTenancy.ignoreFeatureCheckForHostUsers) {
            if (menuItem.hasFeatureDependency() && !menuItem.featureDependencySatisfied()) {
                hideMenuItem = true;
            }
        }

        if (!hideMenuItem && menuItem.items && menuItem.items.length) {
            return this.checkChildMenuItemPermission(menuItem);
        }

        return !hideMenuItem;
    }
}
