import { Injectable } from '@angular/core';

export interface SelectionRange {
  start: number;
  end: number;
}

export interface ComposeResult {
  text: string;
  selectionStart: number;
  selectionEnd: number;
}

@Injectable({ providedIn: 'root' })
export class ChatMarkdownService {
  applyInline(content: string, selection: SelectionRange, token: string): ComposeResult {
    const { start, end } = selection;
    const before = content.slice(0, start);
    const selected = content.slice(start, end) || 'text';
    const after = content.slice(end);
    const newText = `${before}${token}${selected}${token}${after}`;
    const cursor = before.length + token.length + selected.length + token.length;

    return { text: newText,
      selectionStart: cursor,
      selectionEnd: cursor };
  }

  applyPrefix(content: string, selection: SelectionRange, prefix: string): ComposeResult {
    const { start, end } = selection;
    const before = content.slice(0, start);
    const selected = content.slice(start, end) || 'text';
    const after = content.slice(end);
    const lines = selected.split('\n').map(line => `${prefix}${line}`);
    const newSelected = lines.join('\n');
    const text = `${before}${newSelected}${after}`;
    const cursor = before.length + newSelected.length;

    return { text,
      selectionStart: cursor,
      selectionEnd: cursor };
  }

  applyHeading(content: string, selection: SelectionRange, level: number): ComposeResult {
    const hashes = '#'.repeat(Math.max(1, Math.min(6, level)));
    const { start, end } = selection;
    const before = content.slice(0, start);
    const selected = content.slice(start, end) || 'Heading';
    const after = content.slice(end);
    const needsLeadingNewline = before.length > 0 && !before.endsWith('\n');
    const needsTrailingNewline = after.length > 0 && !after.startsWith('\n');
    const block = `${needsLeadingNewline ? '\n' : ''}${hashes} ${selected}${needsTrailingNewline ? '\n' : ''}`;
    const text = `${before}${block}${after}`;
    const cursor = before.length + block.length;

    return { text,
      selectionStart: cursor,
      selectionEnd: cursor };
  }

  applyOrderedList(content: string, selection: SelectionRange): ComposeResult {
    const { start, end } = selection;
    const before = content.slice(0, start);
    const selected = content.slice(start, end) || 'item\nitem';
    const after = content.slice(end);
    const lines = selected.split('\n').map((line, index) => `${index + 1}. ${line}`);
    const newSelected = lines.join('\n');
    const text = `${before}${newSelected}${after}`;
    const cursor = before.length + newSelected.length;

    return { text,
      selectionStart: cursor,
      selectionEnd: cursor };
  }

  applyCodeBlock(content: string, selection: SelectionRange): ComposeResult {
    const { start, end } = selection;
    const before = content.slice(0, start);
    const selected = content.slice(start, end) || 'code';
    const after = content.slice(end);
    const fenced = `\`\`\`\n${selected}\n\`\`\`\n\n`;
    const text = `${before}${fenced}${after}`;
    const cursor = before.length + fenced.length;

    return { text,
      selectionStart: cursor,
      selectionEnd: cursor };
  }

  applyLink(content: string, selection: SelectionRange): ComposeResult {
    const { start, end } = selection;
    const before = content.slice(0, start);
    const selected = content.slice(start, end) || 'link';
    const after = content.slice(end);
    const link = `[${selected}](https://)`;
    const text = `${before}${link}${after}`;
    const cursorStart = before.length + link.length - 1;

    // Position inside the URL placeholder
    return { text,
      selectionStart: cursorStart - 8,
      selectionEnd: cursorStart - 1 };
  }

  applyImage(content: string, selection: SelectionRange): ComposeResult {
    const { start, end } = selection;
    const before = content.slice(0, start);
    const selected = content.slice(start, end) || 'alt';
    const after = content.slice(end);
    const img = `![${selected}](https://)`;
    const text = `${before}${img}${after}`;
    const cursorStart = before.length + img.length - 1;

    return { text,
      selectionStart: cursorStart - 8,
      selectionEnd: cursorStart - 1 };
  }

  applyHorizontalRule(content: string, selection: SelectionRange): ComposeResult {
    const { start, end } = selection;
    const before = content.slice(0, start);
    const after = content.slice(end);
    const hr = '\n\n---\n\n';
    const text = `${before}${hr}${after}`;
    const cursor = before.length + hr.length;

    return { text,
      selectionStart: cursor,
      selectionEnd: cursor };
  }

  appendImageMarkdown(content: string): string {
    const imageUrlRegex = /(https?:\/\/[^\s)]+?\.(?:png|jpe?g|gif|webp|svg|bmp|tiff)(?:\?[^\s)]*)?)/ig;
    const urls = new Set<string>();

    let match: RegExpExecArray | null;

    const text = content;

    while ((match = imageUrlRegex.exec(text)) !== null) {
      urls.add(match[1]);
    }

    if (urls.size === 0)
      return content;

    let append = '';

    for (const url of urls) {
      const alreadyEmbedded = new RegExp(`!\\[[^\\]]*\\]\\(\\s*${this.escapeRegex(url)}\\s*\\)`, 'i').test(text);

      if (!alreadyEmbedded) {
        append += `\n![](${url})`;
      }
    }

    return append ? content + append : content;
  }

  private escapeRegex(str: string): string {
    return str.replace(/[.*+?^${}()|[\\]\\]/g, '\\$&');
  }
}
