import { CommonModule } from '@angular/common';
import { Directive, ElementRef, HostListener, Input, ModuleWithProviders, NgModule, NO_ERRORS_SCHEMA, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { FlexLayoutModule } from '@angular/flex-layout';
import { TranslateModule } from '@ngx-translate/core';

import { KnkMaterialModule } from '../knk-material';

import { CoreRoutingModule } from './core.routing';

import {
  CommonHeaderComponent, HeaderComponent, KnkFilterComponent, NotFoundComponent,
  KnkSearchFilterComponent, KnkSearchSortComponent, KnkSearchStatsComponent
} from './components';
import {
  AccountService, ApiConsumerTokenResolve, ApiUserTokenResolve, ConfigService, CountryService, CurrencyService, ExchangeService, FilterService, HeaderService, KnokService, LanguageResolve,
  LanguageService, LoaderService, TagService, TestParameterResolve, TokenService, UserService, UserTokenResolve, ViewRefWatcherService, RouterExtService, KnokRollService,
  DelayResolve
} from './shared';

import { ReactiveFormsModule } from '@angular/forms';  // <-- #1 import module

import { FeedsComponent } from '../feeds/feeds.component';

import { DiscoverComponent } from '../discover/discover.component';

import { FollowsComponent } from '../follows/follows.component';

import { CreateKnokComponent } from '../createknok/createknok.component';
import { CreateKnokComponent as _CreateKnokComponent, PriceInputSwitcherComponent } from '../todo-createknok/createknok.component';

import { TrackComponent } from '../track/track.component';

import { EndedComponent } from '../ended/ended.component';

import { WizardHeaderComponent } from './components/wizard/wizard-header/wizardheader.component';

import { WizardStepHeaderComponent } from './components/wizard/step-header/stepheader.component';

import { WizardComponent, WizardStepDirective } from './components/wizard/wizard.component';

import { StepBaseComponent } from './components/wizard/wizard.basestep.component';

import { StepOneComponent } from './components/wizard/stepone/wizardstepone.component';

import { StepTwoComponent } from './components/wizard/steptwo/wizardsteptwo.component';

import { UploadGraphComponent } from './components/uploadgraph/uploadgraph.component';

import { TagComponent } from './components/tag/tag.component';

import { LinkToRefPageComponent } from './components/linktorefpage/linktorefpage.component';

import { PriceComponent } from './components/price/price.component';

import { EmailDirective } from '../core/shared/patternValidator';

import { CurrencyFilterPipe, FilterPipe } from '../core/shared/filter';

import { KnokItemComponent } from '../core/components/knokitem';

import { BuyKnokDialogComponent, DiscoveryItemComponent, KnokserProfileDialogComponent } from '../core/components/discoveryitem';

import { OngoingItemComponent, OngoingItemChartComponent } from '../core/components/ongoingitem';
import { KnkEndedItemComponent, KnkEndedItemChartComponent } from '../core/components/knk-ended-item';

import { KnokItemProgressComponent } from '../core/components/knokitemprogress';

import { KnokItemOptionComponent } from '../core/components/knokitemoption';

import { FormlyModule } from '@ngx-formly/core';
import { FormlyMaterialModule } from '@ngx-formly/material';

import { NouisliderModule } from 'ng2-nouislider';

import { NgxEditorModule } from 'ngx-editor';

// import { DropdownModule } from 'ngx-dropdown';

import { BsDropdownModule, TooltipModule } from 'ngx-bootstrap';

import { INgxSelectOptions, NgxSelectModule } from 'ngx-select-ex';

import { EVENT_MANAGER_PLUGINS } from '@angular/platform-browser';
import {
  MDCEventPlugin, MDCAttachToDirective,
  MDCDropdownDirective, MDCDropdownToggleDirective, MDCDropdownToggleLabelDirective, MDCDropdownMenuDirective, MDCDropdownMenuItemDirective
} from './shared/mdc/';
import { InputNumberModule } from './shared/libs/input-number';
import { InputReferenceModule } from './shared/libs/input-reference';
import { InputHelpersModule } from './shared/libs/input-helpers';
import { TristateSwitcherModule } from './shared/libs/switch-button-3-stages';
import { NgButtonDistortedModule } from './shared/libs/ng-button-distorted';
import { RouterDelayModule } from './shared/libs/router-delay';
import { NgSvgModule } from './shared/libs/ng-svg';
import { Pipe, PipeTransform } from '@angular/core';
import { QuillModule } from 'ngx-quill';
import { NgSelectModule, NG_SELECT_DEFAULT_CONFIG } from '@ng-select/ng-select';
import { KnkKnokOutletComponent } from './components/knk-knok-outlet';
import { KnkTrackKnokComponent } from './components/knk-track-knok';
import { KnkKnokScoreComponent } from './components/knk-knok-score';
import { KnkOngoingContentComponent } from './components/knk-ongoing-content';
import { KnkDiscoverContentComponent } from './components/knk-discover-content';
import { KnkPublishedContentComponent } from './components/knk-published-content';
import { KnkPublishedKnokCurrentlyComponent } from './components/knk-published-knok-currently';
import { KnkPublishedKnokPastComponent } from './components/knk-published-knok-past';
import { KnkFeedbackKnokComponent } from './components/knk-feedback-knok';
import { KnkFeedbackEndedComponent } from './components/knk-feedback-ended';

@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
  transform(value: any): any {
    if (!value) {
      return null;
    }
    return Object.keys(value);
  }
}

const customSelectOptions: INgxSelectOptions = { // Check the interface for more options
  optionValueField: 'id',
  optionTextField: 'name'
};

@NgModule({
  imports: [CoreRoutingModule, CommonModule, FormsModule, KnkMaterialModule, TranslateModule,
    FlexLayoutModule, ReactiveFormsModule, FormlyModule, FormlyMaterialModule, NouisliderModule, NgxEditorModule, BsDropdownModule.forRoot(), TooltipModule.forRoot(), NgxSelectModule.forRoot(customSelectOptions),
    QuillModule, NgSelectModule,
    InputNumberModule, InputReferenceModule, InputHelpersModule, NgSvgModule, TristateSwitcherModule, NgButtonDistortedModule, RouterDelayModule],
  exports: [KnkMaterialModule, NotFoundComponent, HeaderComponent, EmailDirective, FeedsComponent, DiscoveryItemComponent, KnokserProfileDialogComponent, BuyKnokDialogComponent, OngoingItemComponent, KnkEndedItemComponent,
    KnokItemComponent, KnokItemProgressComponent, DiscoverComponent, FollowsComponent, KnkFilterComponent, KnkSearchFilterComponent, KnkSearchSortComponent, KnkSearchStatsComponent,
    MDCAttachToDirective, MDCDropdownDirective, MDCDropdownToggleDirective, MDCDropdownToggleLabelDirective, MDCDropdownMenuDirective, MDCDropdownMenuItemDirective,
    QuillModule, NgSelectModule,
    InputNumberModule, InputReferenceModule, InputHelpersModule, NgSvgModule, TristateSwitcherModule, NgButtonDistortedModule, RouterDelayModule,
    FormlyModule, FormlyMaterialModule, WizardComponent, WizardStepHeaderComponent, WizardHeaderComponent, StepOneComponent, StepTwoComponent, UploadGraphComponent, TagComponent, LinkToRefPageComponent, PriceComponent, // StepThreeComponent, StepFourComponent, StepFiveComponent,
    CreateKnokComponent, _CreateKnokComponent, PriceInputSwitcherComponent,
    TrackComponent, EndedComponent, KnkTrackKnokComponent, KnkKnokOutletComponent, KnkKnokScoreComponent, KnkOngoingContentComponent, KnkDiscoverContentComponent, KnkFeedbackKnokComponent, KnkFeedbackEndedComponent,
    KnkPublishedContentComponent, KnkPublishedKnokCurrentlyComponent, KnkPublishedKnokPastComponent,
    KnokItemOptionComponent, WizardStepDirective, StepBaseComponent, NouisliderModule],
  declarations: [NotFoundComponent, HeaderComponent, CommonHeaderComponent, EmailDirective, FeedsComponent, DiscoveryItemComponent, KnokserProfileDialogComponent, BuyKnokDialogComponent, OngoingItemComponent, KnkEndedItemComponent,
    OngoingItemChartComponent, KnkEndedItemChartComponent, KnokItemComponent, KnokItemProgressComponent, DiscoverComponent, FollowsComponent, KnkFilterComponent, KnkSearchFilterComponent, KnkSearchSortComponent, KnkSearchStatsComponent,
    MDCAttachToDirective, MDCDropdownDirective, MDCDropdownToggleDirective, MDCDropdownToggleLabelDirective, MDCDropdownMenuDirective, MDCDropdownMenuItemDirective,
    WizardComponent, WizardStepHeaderComponent, WizardHeaderComponent, StepOneComponent, StepTwoComponent, UploadGraphComponent, TagComponent, LinkToRefPageComponent, PriceComponent,  // StepThreeComponent, StepFourComponent, StepFiveComponent,
    CreateKnokComponent, _CreateKnokComponent, PriceInputSwitcherComponent,
    TrackComponent, EndedComponent, KnkTrackKnokComponent, KnkKnokOutletComponent, KnkKnokScoreComponent, KnkOngoingContentComponent, KnkDiscoverContentComponent, KnkFeedbackKnokComponent, KnkFeedbackEndedComponent,
    KnkPublishedContentComponent, KnkPublishedKnokCurrentlyComponent, KnkPublishedKnokPastComponent,
    KnokItemOptionComponent, WizardStepDirective, StepBaseComponent, FilterPipe, CurrencyFilterPipe, KeysPipe],
  providers: [
    UserService,
    HeaderService,
    ViewRefWatcherService,
    RouterExtService,
    KnokRollService,
    {
      provide: EVENT_MANAGER_PLUGINS,
      useClass: MDCEventPlugin,
      multi: true
    },
    {
      provide: NG_SELECT_DEFAULT_CONFIG,
      useValue: {
        notFoundText: 'No items found',
        typeToSearchText: 'Type to search',
        addTagText: 'Add item',
        loadingText: 'Loading...',
        clearAllText: 'Clear all',
        disableVirtualScroll: false
      }
    }
  ],
  entryComponents: [
    KnokserProfileDialogComponent,
    BuyKnokDialogComponent,
    KnkTrackKnokComponent,
    KnkFeedbackKnokComponent,
    KnkFeedbackEndedComponent
  ],
  schemas: []
})
export class CoreModule {
  static forRoot(): ModuleWithProviders {
    console.log('core module for root');
    return {
      ngModule: CoreModule,
      providers: [
        UserService, ApiUserTokenResolve, ApiConsumerTokenResolve, ConfigService, CountryService, CurrencyService, ExchangeService, FilterService, LanguageResolve, UserTokenResolve,
        TokenService, TestParameterResolve, HeaderService, KnokService, TagService, LoaderService, LanguageService, AccountService, ViewRefWatcherService, RouterExtService, KnokRollService,
        DelayResolve]
    };
  }

  constructor() {
    console.log('core module ctor');
  }
}
