import { inject, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { StepsModule } from 'primeng/steps';
import { DialogModule } from 'primeng/dialog';
import { ToastModule } from 'primeng/toast';
import { ProgressSpinnerModule } from 'primeng/progressspinner';
import { ButtonModule } from 'primeng/button';
import { ButtonGroupModule } from 'primeng/buttongroup';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
import { TableModule } from 'primeng/table';
import { InputTextModule } from 'primeng/inputtext';
import { CheckboxModule } from 'primeng/checkbox';
import { DropdownModule } from 'primeng/dropdown';
import { TranslateModule } from '@ngx-translate/core';
import { ImageModule } from 'primeng/image';
import { TabMenuModule } from 'primeng/tabmenu';
import { MenubarModule } from 'primeng/menubar';
import { PanelMenuModule } from 'primeng/panelmenu';
import { CardModule } from 'primeng/card';
import { MultiSelectModule } from 'primeng/multiselect';
import { SplitButtonModule } from 'primeng/splitbutton';
import { CalendarModule } from 'primeng/calendar';
import { PaginatorModule } from 'primeng/paginator';
import { DataViewModule } from 'primeng/dataview';
import { TagModule } from 'primeng/tag';
import { SidebarModule } from 'primeng/sidebar';
import { FileUploadModule } from 'primeng/fileupload';
import { MenuModule } from 'primeng/menu';
import { ChipModule } from 'primeng/chip';
import { BadgeModule } from 'primeng/badge';
import { ContextMenuModule } from 'primeng/contextmenu';
import { DragDropModule } from 'primeng/dragdrop';
import { InputSwitchModule } from 'primeng/inputswitch';
import { SelectButtonModule } from 'primeng/selectbutton';
import { BreadcrumbModule } from 'primeng/breadcrumb';
import { OverlayPanelModule } from 'primeng/overlaypanel';
import { TerminalModule } from 'primeng/terminal';
import { RatingModule } from 'primeng/rating';
import { FocusTrapModule } from 'primeng/focustrap';
import { InputMaskModule } from 'primeng/inputmask';
import { TriStateCheckboxModule } from 'primeng/tristatecheckbox';
import { InputTextareaModule } from 'primeng/inputtextarea';
import { MenuBarComponent } from 'src/app/modules/shared/components/menu-bar/menu-bar.component';
import { SideMenuComponent } from './components/side-menu/side-menu.component';
import {
  CustomActionDirective,
  TableComponent,
} from './components/table/table.component';
import { BoolPipe } from 'src/app/modules/shared/pipes/bool.pipe';
import { CustomDatePipe } from 'src/app/modules/shared/pipes/customDate.pipe';
import { FormPageComponent } from 'src/app/modules/shared/components/slidein/form-page.component';
import {
  FormPageContentDirective,
  FormPageHeaderDirective,
} from 'src/app/modules/shared/form';
import { ProtectPipe } from './pipes/protect.pipe';
import { provideApollo } from 'apollo-angular';
import { HttpLink } from 'apollo-angular/http';
import { InMemoryCache } from '@apollo/client/core';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { tokenInterceptor } from 'src/app/core/token.interceptor';
import { SettingsService } from 'src/app/service/settings.service';
import { SliderModule } from 'primeng/slider';

const sharedModules = [
  StepsModule,
  DialogModule,
  ToastModule,
  ProgressSpinnerModule,
  ButtonModule,
  ButtonGroupModule,
  FormsModule,
  ReactiveFormsModule,
  ConfirmDialogModule,
  TableModule,
  InputTextModule,
  CheckboxModule,
  DropdownModule,
  TranslateModule,
  ImageModule,
  TabMenuModule,
  MenubarModule,
  PanelMenuModule,
  CardModule,
  MultiSelectModule,
  SplitButtonModule,
  CalendarModule,
  PaginatorModule,
  DataViewModule,
  TagModule,
  SidebarModule,
  FileUploadModule,
  MenuModule,
  ChipModule,
  BadgeModule,
  ContextMenuModule,
  DragDropModule,
  InputSwitchModule,
  SelectButtonModule,
  BreadcrumbModule,
  OverlayPanelModule,
  SplitButtonModule,
  TerminalModule,
  RatingModule,
  FocusTrapModule,
  InputMaskModule,
  TriStateCheckboxModule,
  InputTextareaModule,
  TranslateModule,
  SliderModule,
];

const sharedComponents = [
  MenuBarComponent,
  SideMenuComponent,
  BoolPipe,
  CustomDatePipe,
  FormPageComponent,
  FormPageHeaderDirective,
  FormPageContentDirective,
  CustomActionDirective,
];

@NgModule({
  declarations: [...sharedComponents, TableComponent, ProtectPipe],
  imports: [CommonModule, ...sharedModules],
  exports: [...sharedModules, ...sharedComponents, TableComponent],
  providers: [
    provideHttpClient(withInterceptors([tokenInterceptor])),
    provideApollo(() => {
      const httpLink = inject(HttpLink);
      const settings = inject(SettingsService);

      return {
        link: httpLink.create({ uri: `${settings.settings.api.url}/graphql` }),
        cache: new InMemoryCache(),

        defaultOptions: {
          watchQuery: {
            fetchPolicy: 'no-cache',
          },
          query: {
            fetchPolicy: 'no-cache',
          },
          mutate: {
            fetchPolicy: 'no-cache',
          },
        },
      };
    }),
  ],
})
export class SharedModule {}
