'use client';

import { Spell } from '@/api/spells.types';
import { levelText, rollDice } from '@/js/spells';
import { capitalize } from '@/js/utils';
import { Grid, Modal } from '@mantine/core';
import { notifications } from '@mantine/notifications';

interface SpellModalProps {
  spell: Spell;
  isOpen: boolean;
  onClose(): void;
}

export default function SpellModal({ spell, isOpen, onClose }: SpellModalProps) {
  return (
    <Modal opened={isOpen} onClose={onClose} withCloseButton={false} size={'50%'} className='modal'>
      <h1 style={{ padding: '0', margin: '0' }}>{spell.name}</h1>
      <Grid gutter={1}>
        <Grid.Col span={4} style={{ paddingBottom: '1rem' }}>
          <span style={{ fontWeight: 'bold' }}>
            {capitalize(spell.school)} {levelText(spell)}
          </span>
        </Grid.Col>
        <Grid.Col span={8} style={{ paddingBottom: '1rem' }}>
          <div style={{ float: 'right' }}>
            <span style={{ float: 'right' }}>
              {spell.components.verbal && spell.components.somatic ? 'V, ' : 'V '}
              {spell.components.somatic && spell.components.material ? 'S, ' : 'S '}
              {spell.components.material && spell.components.materials_needed ? 'M*' : 'M'}
            </span>
            {spell.components.materials_needed && (
              <span style={{ fontSize: '0.8em', color: 'gray' }}>
                <br />*{capitalize(spell.components.materials_needed)}
              </span>
            )}
          </div>
        </Grid.Col>
        <Grid.Col span={6}>
          <span style={{ fontWeight: 'bold', paddingRight: '1em' }}>Sources:</span>
          {spell.sources.map((s, index) => (
            <span style={{ paddingRight: '0.6em' }} key={`spell-source-${index}`}>
              {s.source}
              {s.page ? `.${s.page}` : ''}
            </span>
          ))}
        </Grid.Col>
        <Grid.Col span={6}>
          <span style={{ fontWeight: 'bold', marginRight: '1em' }}>Classes:</span>
          <span style={{ overflowWrap: 'break-word' }}>
            {spell.classes.map((c, index) => (
              <span
                style={{ paddingRight: '0.6em', display: 'inline-block' }}
                className='link'
                key={`spell-class-${index}`}
              >
                {parseText(c, true)}
              </span>
            ))}
          </span>
        </Grid.Col>
        <Grid.Col span={6}>
          <span style={{ fontWeight: 'bold', paddingRight: '1em' }}>Casting Time:</span>
          <span style={{ paddingRight: '0.6em' }}>
            {spell.casting_time.value} {capitalize(spell.casting_time.unit)}
          </span>
        </Grid.Col>
        <Grid.Col span={6}>
          <span style={{ fontWeight: 'bold', paddingRight: '1em' }}>Range:</span>
          <span style={{ paddingRight: '0.6em' }}>
            {spell.range.type != 'point' && capitalize(spell.range.type)} {spell.range.value}{' '}
            {capitalize(spell.range.unit)}
          </span>
        </Grid.Col>
        <Grid.Col span={6}>
          <span style={{ fontWeight: 'bold', paddingRight: '1em' }}>Duration:</span>
          <span style={{ paddingRight: '0.6em' }}>
            {spell.durations.map((d, index) => (
              <span key={`duration-${index}`}>
                {capitalize(d.type)} {d.value} {capitalize(d.unit)}
              </span>
            ))}
          </span>
        </Grid.Col>
        <Grid.Col span={12}>
          <SpellDescription spell={spell} />
        </Grid.Col>
      </Grid>
    </Modal>
  );
}

function parseText(text: string, capitalizeFirst?: boolean): (string | JSX.Element)[] {
  const regex = /{@(.*?) (.*?)}/g;
  const matches = text.matchAll(regex);
  const result = [];
  let lastIndex = 0;
  let noMatches = true;

  for (const match of matches) {
    noMatches = false;
    const key = crypto.randomUUID();
    const [full, type, name] = match;
    result.push(<span key={crypto.randomUUID()}>{text.slice(lastIndex, match.index)}</span>);
    if (match.index !== undefined) {
      if (type == 'dice') {
        result.push(
          <span onClick={() => handleLink(type, name)} className='link' key={key}>
            {name}
          </span>
        );
      } else if (type == 'scaledice') {
        // scaledice format is {@scaledice 1d6|1-9}. Parse this out into dice, levels, and dice again.
        const [dice, levels] = name.split('|');
        result.push(
          <span onClick={() => handleLink('dice', dice)} className='link' key={key}>
            {dice}
          </span>
        );
      } else if (type == 'bold') {
        result.push(
          <span style={{ fontWeight: 'bold' }} key={key}>
            {name}
          </span>
        );
      } else if (type == 'subclass') {
        const [className, subclassName] = name.split('|');
        result.push(
          <span key={key}>
            {capitalize(className)} ({capitalize(subclassName)})
          </span>
        );
      } else {
        result.push(<span key={key}>{capitalizeFirst ? capitalize(name) : name}</span>);
      }
      lastIndex = match.index + full.length;
    }
  }
  const lastString = text.slice(lastIndex);
  result.push(<span key={crypto.randomUUID()}>{noMatches ? capitalize(lastString) : lastString}</span>);

  return result;
}

function handleLink(type: string, name: string) {
  if (type == 'spell') {
    console.log(`Link to spell: ${name}`);
  } else if (type == 'dice') {
    const rolls = rollDice(name);
    notifications.show({
      title: `Rolling ${name}`,
      message: `${rolls.join(' + ')} = ${rolls.reduce((a, b) => a + b, 0)}`,
      color: 'blue',
      autoClose: 5000,
      withCloseButton: false
    });
  } else if (type == 'scaledice') {
    console.log(`Link to scaledice: ${name}`);
  } else {
    console.error(`Unknown link type: ${type}`);
  }
}

function SpellDescription({ spell }: { spell: Spell }) {
  return (
    <>
      {spell.description && (
        <>
          {spell.description.entries.map((e, index) => (
            <div key={`spell-description-${index}`}>
              {e.text && <p>{parseText(e.text)}</p>}
              {e.list && (
                <ul>
                  {e.list.map((text, index) => (
                    <li key={`spell-text-${index}`}>{parseText(text)}</li>
                  ))}
                </ul>
              )}
              {e.table && (
                <table>
                  <thead>
                    <tr>
                      {e.table.headers.map((label, index) => (
                        <th key={`spell-header-${index}`}>{label}</th>
                      ))}
                    </tr>
                  </thead>
                  <tbody>
                    {e.table.rows.map((row, index) => (
                      <tr key={`spell-row-${index}`}>
                        {row.map((cell, index) => (
                          <td key={`spell-cell-${index}`}>{parseText(cell)}</td>
                        ))}
                      </tr>
                    ))}
                  </tbody>
                </table>
              )}
            </div>
          ))}
        </>
      )}
    </>
  );
}
