import { Hocuspocus } from '@hocuspocus/server';
import { IncomingMessage } from 'http';
import { WebSocket } from 'ws';
import { AuthenticationExtension } from './extensions/authentication.extension';
import { PersistenceExtension } from './extensions/persistence.extension';
import { Injectable, Logger } from '@nestjs/common';
import { EnvironmentService } from '../integrations/environment/environment.service';
import {
  createRetryStrategy,
  parseRedisUrl,
  RedisConfig,
  resolveClientIp,
} from '../common/helpers';
import { LoggerExtension } from './extensions/logger.extension';
import {
  RedisSyncExtension,
  SerializedHTTPRequest,
} from './extensions/redis-sync';
import { toWebRequest } from './extensions/redis-sync/redis-sync.types';
import { WsSocketWrapper } from './extensions/redis-sync/ws-socket-wrapper';
import RedisClient from 'ioredis';
import { pack, unpack } from 'msgpackr';
import { nanoid } from 'nanoid';
import * as os from 'node:os';
import { CollabWsAdapter } from './adapter/collab-ws.adapter';
import {
  CollaborationHandler,
  CollabEventHandlers,
} from './collaboration.handler';

@Injectable()
export class CollaborationGateway {
  private readonly logger = new Logger(CollaborationGateway.name);
  private readonly hocuspocus: Hocuspocus;
  private redisConfig: RedisConfig;
  // @ts-ignore
  private readonly redisSync: RedisSyncExtension<CollabEventHandlers> | null =
    null;
  private readonly withRedis: boolean;

  constructor(
    private authenticationExtension: AuthenticationExtension,
    private persistenceExtension: PersistenceExtension,
    private loggerExtension: LoggerExtension,
    private environmentService: EnvironmentService,
    private collabEventsService: CollaborationHandler,
  ) {
    this.redisConfig = parseRedisUrl(this.environmentService.getRedisUrl());
    this.withRedis = !this.environmentService.isCollabDisableRedis();

    this.hocuspocus = new Hocuspocus({
      debounce: 10000,
      maxDebounce: 45000,
      unloadImmediately: false,
      extensions: [
        this.authenticationExtension,
        this.persistenceExtension,
        this.loggerExtension,
      ],
    });

    if (this.withRedis) {
      // @ts-ignore
      this.redisSync = new RedisSyncExtension({
        redis: new RedisClient({
          // The extension connects its publisher and subscriber duplicates.
          lazyConnect: true,
          host: this.redisConfig.host,
          port: this.redisConfig.port,
          username: this.redisConfig.username,
          password: this.redisConfig.password,
          db: this.redisConfig.db,
          family: this.redisConfig.family,
          tls: this.redisConfig.tls,
          retryStrategy: createRetryStrategy(),
        }),
        serverId: `collab-${os?.hostname()}-${nanoid(10)}`,
        prefix: 'collab',
        pack,
        unpack,
        // @ts-ignore
        customEvents: this.collabEventsService.getHandlers(this.hocuspocus),
      });
      this.hocuspocus.configuration.extensions.push(this.redisSync);
      // @ts-ignore
      this.redisSync.onConfigure({ instance: this.hocuspocus });
    }
  }

  private serializeRequest(request: IncomingMessage): SerializedHTTPRequest {
    return {
      method: request.method ?? 'GET',
      url: request.url ?? '/',
      headers: {
        'sec-websocket-key': request.headers['sec-websocket-key'] ?? '',
        'sec-websocket-protocol':
          request.headers['sec-websocket-protocol'] ?? '',
      },
      socket: { remoteAddress: resolveClientIp(request) ?? '' },
    };
  }

  private openSocket(socket: WsSocketWrapper, request: SerializedHTTPRequest) {
    const relay = this.redisSync;
    if (relay) {
      const socketId = request.headers['sec-websocket-key'];
      relay.onSocketOpen(socket, request);
      return {
        onMessage: (data: Buffer) => relay.onSocketMessage(request, data),
        onClose: (code: number, reason: Buffer) =>
          relay.onSocketClose(socketId, code, new Uint8Array(reason).buffer),
      };
    }

    const connection = this.hocuspocus.handleConnection(socket, toWebRequest(request));
    return {
      onMessage: (data: Buffer) => connection.handleMessage(new Uint8Array(data)),
      onClose: (code: number, reason: Buffer) =>
        connection.handleClose({ code, reason: reason.toString() }),
    };
  }

  handleConnection(client: WebSocket, request: IncomingMessage): void {
    const onFailure = (err: unknown) => {
      this.logger.error({ err }, 'Failed to handle collaboration WebSocket');
      if (client.readyState === WebSocket.OPEN) client.close(1011);
    };
    try {
      const connection = this.openSocket(
        new WsSocketWrapper(client, onFailure),
        this.serializeRequest(request),
      );
      client.on('message', async (data: Buffer) => {
        try {
          await connection.onMessage(data);
        } catch (err) {
          onFailure(err);
        }
      });
      client.on('close', (code: number, reason: Buffer) => {
        try {
          connection.onClose(code, reason);
        } catch (err) {
          onFailure(err);
        }
      });
    } catch (err) {
      onFailure(err);
    }
  }

  getConnectionCount() {
    return this.hocuspocus.getConnectionsCount();
  }

  getDocumentCount() {
    return this.hocuspocus.getDocumentsCount();
  }

  handleYjsEvent<TName extends keyof CollabEventHandlers>(
    eventName: TName,
    documentName: string,
    payload: Parameters<CollabEventHandlers[TName]>[1],
    onlyIfOpen = false,
  ) {
    return this.redisSync?.handleEvent(
      eventName,
      documentName,
      payload,
      onlyIfOpen,
    );
  }

  openDirectConnection(documentName: string, context?: any) {
    return this.hocuspocus.openDirectConnection(documentName, context);
  }

  /*
   *Can be used before calling openDirectConnection directly
   */
  async lockDocument(documentName: string) {
    return this.redisSync.lockDocument(documentName);
  }

  /*
   *Releases a document lock and stops the interval that maintains it.
   */
  async releaseLock(documentName: string) {
    return this.redisSync.releaseLock(documentName);
  }

  async destroy(collabWsAdapter: CollabWsAdapter): Promise<void> {
    // eslint-disable-next-line no-async-promise-executor
    await new Promise(async (resolve) => {
      try {
        // Wait for all documents to unload
        this.hocuspocus.configuration.extensions.push({
          async afterUnloadDocument({ instance }) {
            if (instance.getDocumentsCount() === 0) resolve('');
          },
        });

        collabWsAdapter?.close();

        if (this.hocuspocus.getDocumentsCount() === 0) resolve('');
        this.hocuspocus.closeConnections();
        this.hocuspocus.flushPendingStores();
      } catch (error) {
        console.error(error);
      }
    });

    await this.hocuspocus.hooks('onDestroy', { instance: this.hocuspocus });
  }
}
