import { BrowserModule } from '@angular/platform-browser'
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
import { Injector, NgModule, LOCALE_ID } from '@angular/core'
import { FormsModule } from '@angular/forms'
import { HttpClientModule } from '@angular/common/http'

import { MatButtonModule } from '@angular/material/button'
import { MatIconModule } from '@angular/material/icon'
import { MatSnackBarModule } from '@angular/material/snack-bar'
import { MatSnackBar } from '@angular/material/snack-bar'
import { MatToolbarModule } from '@angular/material/toolbar'
import { MatTooltipModule } from '@angular/material/tooltip'

import { FlexLayoutModule } from 'ngx-flexible-layout'

// import { ApolloModule, APOLLO_OPTIONS } from 'apollo-angular'
// import { HttpLink } from 'apollo-angular/http'
// import { onError } from '@apollo/client/link/error'
// import { split, from, ApolloLink, InMemoryCache, ApolloClientOptions } from '@apollo/client/core'
// import { WebSocketLink } from '@apollo/client/link/ws'
// import { getMainDefinition, getOperationName } from '@apollo/client/utilities'
// import { DefinitionNode } from 'graphql'

import { AppComponent } from './app.component'
import { PageNotFoundComponent } from './pageNotFound.component'
import { LoginModule } from './login/login.module'
import { AuthenticationService } from './_services/authentication.service'
import { BootstrapService } from './_services/bootstrap.service'
import { ModelDataService } from './_services/apollo.service'
import { ActionsService } from './_services/actions.service'
import { ConfigService } from './config.service'

import { ApiService } from './openapi/services/ApiService'
import { AdminService } from './openapi/services/AdminService'
import { DashboardService } from './openapi/services/DashboardService'
import { GeoapiService } from './openapi/services/GeoapiService'
import { MapService } from './openapi/services/MapService'

import { HtmlSnackbarComponent } from './custom-snackbar/custom-snackbar.component'

import { AppRoutingModule } from './app-routing.module'
import { environment } from '../environments/environment'

@NgModule({
    declarations: [
        AppComponent,
        PageNotFoundComponent,
        HtmlSnackbarComponent,
    ],
    imports: [
        // ApolloModule,

        BrowserModule,
        BrowserAnimationsModule,

        FormsModule,
        HttpClientModule,

        MatToolbarModule,
        MatButtonModule,
        MatIconModule,
        MatSnackBarModule,

        MatTooltipModule,

        FlexLayoutModule,

        AppRoutingModule,
        LoginModule,
    ],
    providers: [
        ActionsService,
        AuthenticationService,
        BootstrapService,
        ApiService,
        AdminService,
        GeoapiService,
        DashboardService,
        MapService,
        ConfigService,
        ModelDataService,
        {
            provide: LOCALE_ID,
            useValue: "en-IN"
        },
        // {
        //     provide: APOLLO_OPTIONS,
        //     useFactory(httpLink: HttpLink, snackBar: MatSnackBar) {
        //         const definitionIsMutation = (d: DefinitionNode) => {
        //             return d.kind === 'OperationDefinition' && d.operation === 'mutation'
        //         }

        //         // See https://github.com/apollographql/apollo-angular/issues/1013
        //         const linkQueries = httpLink.create({
        //             uri: '/graphql',
        //             method: 'GET',
        //         })

        //         const linkMutations = httpLink.create({
        //             uri: '/graphql',
        //         })

        //         const splittedLink = split(
        //             ({ query }) => query.definitions.some(definitionIsMutation),
        //             linkMutations,
        //             linkQueries,
        //         )

        //         const schedulerQueriesLink = httpLink.create({
        //             uri: '/graphql_sched',
        //             method: 'GET',
        //         })

        //         const proxyLink = split(
        //             ({ query }) => {
        //                 let res = query.definitions[0]['name']['value'].startsWith('scheduler_')
        //                 return res
        //             },
        //             schedulerQueriesLink,
        //             splittedLink
        //         )

        //         const errorLink = onError(({ graphQLErrors, networkError }) => {
        //             if (graphQLErrors)
        //                 graphQLErrors.map(({ message, locations, path }) => {
        //                     snackBar.open(`Error: ${message}`, 'close')
        //                     console.error(
        //                         `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
        //                         graphQLErrors
        //                     )
        //                 }
        //             )
        //             if (networkError) {
        //                 snackBar.open(
        //                     `Network error: ${networkError['statusText']}`,
        //                     'close',
        //                 )
        //                 console.error(networkError)
        //             }
        //         })

        //         const httpLinkWithErrorHandling = from([
        //             errorLink,
        //             proxyLink,
        //         ])

        //         return {
        //             link: httpLinkWithErrorHandling,
        //             cache: new InMemoryCache(),
        //             defaultOptions: {
        //                 watchQuery: {
        //                     fetchPolicy: 'network-only',
        //                     errorPolicy: 'ignore',
        //                 },
        //                 query: {
        //                     fetchPolicy: 'network-only',
        //                     errorPolicy: 'all',
        //                 },
        //                 subscription: {
        //                     fetchPolicy: 'network-only',
        //                     errorPolicy: 'all',
        //                 }
        //             },
        //         }
        //     },
        //     deps: [
        //         HttpLink,
        //         MatSnackBar
        //     ],
        // }
    ],
    bootstrap: [
        AppComponent
    ]
})
export class AppModule {}