import { NgModule, ModuleWithProviders } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { RouterModule, RouteReuseStrategy } from '@angular/router';
import { HttpClientModule, HttpClientXsrfModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';


import { ModalModule } from 'ngx-bootstrap/modal';
import { NgCircleProgressModule } from 'ng-circle-progress';
import { ToastNoAnimationModule } from 'ngx-toastr';
import { BsDatepickerModule } from 'ngx-bootstrap/datepicker';

{{#if loadAllComponents}}
// Basic widgets
import { BasicModule } from '@wm/components/basic';
import { ProgressModule } from '@wm/components/basic/progress';
import { RichTextEditorModule } from '@wm/components/basic/rich-text-editor';
import { SearchModule } from '@wm/components/basic/search';
import { TreeModule } from '@wm/components/basic/tree';

// input
import { InputModule } from '@wm/components/input';
import { CalendarModule } from '@wm/components/input/calendar';
import { ChipsModule } from '@wm/components/input/chips';
import { ColorPickerModule } from '@wm/components/input/color-picker';
import { CurrencyModule } from '@wm/components/input/currency';
import { EpochModule } from '@wm/components/input/epoch';
import { FileUploadModule } from '@wm/components/input/file-upload';
import { RatingModule } from '@wm/components/input/rating';
import { SliderModule } from '@wm/components/input/slider';

// Data
import { CardModule } from '@wm/components/data/card';
import { FormModule } from '@wm/components/data/form';
import { ListModule } from '@wm/components/data/list';
import { LiveTableModule } from '@wm/components/data/live-table';
import { PaginationModule } from '@wm/components/data/pagination';
import { TableModule } from '@wm/components/data/table';

//Chart
import { ChartModule } from '@wm/components/chart';

//Containers
import { AccordionModule } from '@wm/components/containers/accordion';
import { LayoutGridModule } from '@wm/components/containers/layout-grid';
import { PanelModule } from '@wm/components/containers/panel';
import { TabsModule } from '@wm/components/containers/tabs';
import { TileModule } from '@wm/components/containers/tile';
import { WizardModule } from '@wm/components/containers/wizard';

//Dialogs
import { AlertDialogModule } from '@wm/components/dialogs/alert-dialog';
import { IframeDialogModule } from '@wm/components/dialogs/iframe-dialog';
import { LoginDialogModule } from '@wm/components/dialogs/login-dialog';
import { PartialDialogModule } from '@wm/components/dialogs/partial-dialog';

// Navigation
import { BreadcrumbModule } from '@wm/components/navigation/breadcrumb';
import { MenuModule } from '@wm/components/navigation/menu';
import { NavbarModule } from '@wm/components/navigation/navbar';
import { PopoverModule } from '@wm/components/navigation/popover';

// Advanced
import { CarouselModule } from '@wm/components/advanced/carousel';
import { LoginModule } from '@wm/components/advanced/login';
import { MarqueeModule } from '@wm/components/advanced/marquee';

import { PageModule } from '@wm/components/page';
import { FooterModule } from '@wm/components/page/footer';
import { HeaderModule } from '@wm/components/page/header';
import { LeftPanelModule } from '@wm/components/page/left-panel';
import { RightPanelModule } from '@wm/components/page/right-panel';
import { TopNavModule } from '@wm/components/page/top-nav';

import { PrefabModule } from '@wm/components/prefab';
{{#if isMobileApp}}
import { BasicModule as MobileBasicModule} from '@wm/mobile/components/basic';
import { SearchModule as MobileSearchModule } from '@wm/mobile/components/basic/search';

import { SegmentedControlModule as MobileSegmentedControlModule } from '@wm/mobile/components/containers/segmented-control';

import { MediaListModule as MobileMediaListModule } from '@wm/mobile/components/data/media-list';

import { BarcodeScannerModule as MobileBarcodeScannerModule } from '@wm/mobile/components/device/barcode-scanner';
import { CameraModule as MobileCameraModule } from '@wm/mobile/components/device/camera';

import { FileUploadModule as MobileFileUploadModule } from '@wm/mobile/components/input/file-upload';

import { PageModule as MobilePageModule } from '@wm/mobile/components/page';
import { LeftPanelModule as MobileLeftPanelModule } from '@wm/mobile/components/page/left-panel';
import { MobileNavbarModule as MobileMobileNavbarModule } from '@wm/mobile/components/page/mobile-navbar';
import { TabBarModule as MobileTabBarModule } from '@wm/mobile/components/page/tab-bar';
{{/if}}
{{/if}}
import { PartialRefProvider } from '@wm/core';
import {
    AppComponent,
    AppJSProvider,
    AppVariablesProvider,
    ComponentRefProvider,
    PrefabConfigProvider,
    WmNgRouteReuseStrategy,
    CanDeactivateNgPageGuard,
    WM_MODULES_FOR_ROOT,
    REQUIRED_MODULES_FOR_DYNAMIC_COMPONENTS,
    AppExtensionProvider
} from '@wm/runtime/base';


import { routes } from './app.routes';
import { AppJSProviderService } from '../framework/services/app-js-provider.service';
import { AppVariablesProviderService } from '../framework/services/app-variables-provider.service';
import { AppExtensionProviderService } from '../framework/services/app-extension.service';
import { ComponentRefProviderService } from '../framework/services/component-ref-provider.service';
import { PrefabConfigProviderService } from '../framework/services/prefab-config-provider.service';
import { AppCodeGenModule, xsrfHeaderName } from './app-codegen.module';
import { LazyLoadScriptsResolve } from './lazy-load-scripts.resolve';
import { initPrefabConfig } from './prefabs/prefab-config';

{{#if isPwa}}
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';
{{/if}}

export const modalModule = ModalModule.forRoot();
export const routerModule = RouterModule.forRoot(routes, {useHash: true, scrollPositionRestoration: 'top'});
export const toastrModule = ToastNoAnimationModule.forRoot({maxOpened: 1, autoDismiss: true });
export const bsDatePickerModule: ModuleWithProviders<any> = BsDatepickerModule.forRoot();
export const httpClientXsrfModule = HttpClientXsrfModule.withOptions({
    cookieName: 'wm_xsrf_token',
    headerName: xsrfHeaderName
});

export const ngCircleProgressModule: ModuleWithProviders<any> = NgCircleProgressModule.forRoot({});


{{#if loadAllComponents}}
const componentsModule = [
    NgCircleProgressModule,

    // Basic widgets
    BasicModule,
    ProgressModule,
    RichTextEditorModule,
    SearchModule,
    TreeModule,

    // Input
    CalendarModule,
    ChipsModule,
    ColorPickerModule,
    CurrencyModule,
    EpochModule,
    FileUploadModule,
    InputModule,
    RatingModule,
    SliderModule,

    // Data
    CardModule,
    FormModule,
    ListModule,
    LiveTableModule,
    PaginationModule,
    TableModule,

    // chart
    ChartModule,

    // container modules
    AccordionModule,
    LayoutGridModule,
    PanelModule,
    TabsModule,
    TileModule,
    WizardModule,

    // dialogs
    AlertDialogModule,
    IframeDialogModule,
    LoginDialogModule,
    PartialDialogModule,

    // navigation
    BreadcrumbModule,
    MenuModule,
    NavbarModule,
    PopoverModule,

    //Advanced
    CarouselModule,
    LoginModule,
    MarqueeModule,

    PageModule,
    FooterModule,
    HeaderModule,
    LeftPanelModule,
    RightPanelModule,
    TopNavModule,

    PrefabModule
];

{{#if isMobileApp}}
export const mobileComponentModules = [
    MobileBasicModule,
    MobileSearchModule,
    MobileSegmentedControlModule,
    MobileMediaListModule,
    MobileBarcodeScannerModule,
    MobileCameraModule,
    MobileFileUploadModule,
    MobilePageModule,
    MobileLeftPanelModule,
    MobileMobileNavbarModule,
    MobileTabBarModule
];
{{/if}}

REQUIRED_MODULES_FOR_DYNAMIC_COMPONENTS.push(...componentsModule);
REQUIRED_MODULES_FOR_DYNAMIC_COMPONENTS.push(FormsModule, ReactiveFormsModule);
{{#if isMobileApp}}
REQUIRED_MODULES_FOR_DYNAMIC_COMPONENTS.push(...mobileComponentModules);
{{/if}}
{{/if}}

export const isPrefabInitialized = initPrefabConfig();

@NgModule({
    declarations: [],
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        CommonModule,
        RouterModule,
        HttpClientModule,

        modalModule,

        ngCircleProgressModule,

        routerModule,
        toastrModule,
        httpClientXsrfModule,
        bsDatePickerModule,

        WM_MODULES_FOR_ROOT,
        AppCodeGenModule,

        {{#if isPwa}}
        ServiceWorkerModule.register('wmsw-worker.js', { enabled: environment.production })
        {{/if}}
    ],
    providers: [
        {provide: AppJSProvider, useClass: AppJSProviderService},
        {provide: AppVariablesProvider, useClass: AppVariablesProviderService},
        {provide: AppExtensionProvider,useClass:AppExtensionProviderService},
        {provide: ComponentRefProvider, useClass: ComponentRefProviderService},
        {provide: PartialRefProvider, useClass: ComponentRefProviderService},
        {provide: PrefabConfigProvider, useClass: PrefabConfigProviderService},
        {provide: RouteReuseStrategy, useClass: WmNgRouteReuseStrategy},
        CanDeactivateNgPageGuard,
        LazyLoadScriptsResolve
    ],
    bootstrap: [AppComponent]
})
export class AppModule {}
