/*
 *   Famedly Matrix SDK
 *   Copyright (C) 2019, 2020 Famedly GmbH
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as
 *   published by the Free Software Foundation, either version 3 of the
 *   License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see .
 */
import 'package:test/test.dart';
import 'package:matrix/src/utils/markdown.dart';
void main() {
  group('markdown', () {
    final emotePacks = {
      'room': {
        'fox': 'mxc://roomfox',
        'bunny': 'mxc://roombunny',
      },
      'user': {
        'fox': 'mxc://userfox',
        'bunny': 'mxc://userbunny',
        'raccoon': 'mxc://raccoon',
      },
    };
    final mentionMap = {
      '@Bob': '@bob:example.org',
      '@[Bob Ross]': '@bobross:example.org',
      '@Fox#123': '@fox:example.org',
      '@[Fast Fox]#123': '@fastfox:example.org',
      '@[">]': '@blah:example.org',
    };
    String? getMention(mention) => mentionMap[mention];
    test('simple markdown', () {
      expect(markdown('hey *there* how are **you** doing?'),
          'hey there how are you doing?');
      expect(markdown('wha ~~strike~~ works!'), 'wha strike works!');
    });
    test('spoilers', () {
      expect(markdown('Snape killed ||Dumbledoor||'),
          'Snape killed Dumbledoor');
      expect(markdown('Snape killed ||Story|Dumbledoor||'),
          'Snape killed Dumbledoor');
      expect(markdown('Snape killed ||Some dumb loser|Dumbledoor||'),
          'Snape killed Dumbledoor');
      expect(markdown('Snape killed ||Some dumb loser|Dumbledoor **bold**||'),
          'Snape killed Dumbledoor bold');
      expect(markdown('Snape killed ||Dumbledoor **bold**||'),
          'Snape killed Dumbledoor bold');
    });
    test('multiple paragraphs', () {
      expect(markdown('Heya!\n\nBeep'), '
Heya!
Beep
');
    });
    test('Other block elements', () {
      expect(markdown('# blah\n\nblubb'), 'blah
blubb
');
    });
    test('linebreaks', () {
      expect(markdown('foxies\ncute'), 'foxies
cute');
    });
    test('emotes', () {
      expect(markdown(':fox:', getEmotePacks: () => emotePacks),
          ' ');
      expect(markdown(':user~fox:', getEmotePacks: () => emotePacks),
          '
');
      expect(markdown(':user~fox:', getEmotePacks: () => emotePacks),
          ' ');
      expect(markdown(':raccoon:', getEmotePacks: () => emotePacks),
          '
');
      expect(markdown(':raccoon:', getEmotePacks: () => emotePacks),
          ' ');
      expect(
          markdown(':invalid:', getEmotePacks: () => emotePacks), ':invalid:');
      expect(markdown(':invalid:?!', getEmotePacks: () => emotePacks),
          ':invalid:?!');
      expect(markdown(':room~invalid:', getEmotePacks: () => emotePacks),
          ':room~invalid:');
    });
    test('pills', () {
      expect(markdown('Hey @sorunome:sorunome.de!'),
          'Hey @sorunome:sorunome.de!');
      expect(markdown('#fox:sorunome.de: you all are awesome'),
          '#fox:sorunome.de: you all are awesome');
      expect(markdown('!blah:example.org'),
          '!blah:example.org');
      expect(markdown('https://matrix.to/#/#fox:sorunome.de'),
          'https://matrix.to/#/#fox:sorunome.de');
      expect(markdown('Hey @sorunome:sorunome.de:1234!'),
          'Hey @sorunome:sorunome.de:1234!');
      expect(markdown('Hey @sorunome:127.0.0.1!'),
          'Hey @sorunome:127.0.0.1!');
      expect(markdown('Hey @sorunome:[::1]!'),
          'Hey @sorunome:[::1]!');
    });
    test('mentions', () {
      expect(markdown('Hey @Bob!', getMention: getMention),
          'Hey @Bob!');
      expect(markdown('How is @[Bob Ross] doing?', getMention: getMention),
          'How is @[Bob Ross] doing?');
      expect(
          markdown('Hey @invalid!', getMention: getMention), 'Hey @invalid!');
      expect(markdown('Hey @Fox#123!', getMention: getMention),
          'Hey @Fox#123!');
      expect(markdown('Hey @[Fast Fox]#123!', getMention: getMention),
          'Hey @[Fast Fox]#123!');
      expect(markdown('Hey @[">]!', getMention: getMention),
          'Hey @[">]!');
    });
    test('latex', () {
      expect(markdown('meep \$\\frac{2}{3}\$'),
          'meep
');
      expect(
          markdown(':invalid:', getEmotePacks: () => emotePacks), ':invalid:');
      expect(markdown(':invalid:?!', getEmotePacks: () => emotePacks),
          ':invalid:?!');
      expect(markdown(':room~invalid:', getEmotePacks: () => emotePacks),
          ':room~invalid:');
    });
    test('pills', () {
      expect(markdown('Hey @sorunome:sorunome.de!'),
          'Hey @sorunome:sorunome.de!');
      expect(markdown('#fox:sorunome.de: you all are awesome'),
          '#fox:sorunome.de: you all are awesome');
      expect(markdown('!blah:example.org'),
          '!blah:example.org');
      expect(markdown('https://matrix.to/#/#fox:sorunome.de'),
          'https://matrix.to/#/#fox:sorunome.de');
      expect(markdown('Hey @sorunome:sorunome.de:1234!'),
          'Hey @sorunome:sorunome.de:1234!');
      expect(markdown('Hey @sorunome:127.0.0.1!'),
          'Hey @sorunome:127.0.0.1!');
      expect(markdown('Hey @sorunome:[::1]!'),
          'Hey @sorunome:[::1]!');
    });
    test('mentions', () {
      expect(markdown('Hey @Bob!', getMention: getMention),
          'Hey @Bob!');
      expect(markdown('How is @[Bob Ross] doing?', getMention: getMention),
          'How is @[Bob Ross] doing?');
      expect(
          markdown('Hey @invalid!', getMention: getMention), 'Hey @invalid!');
      expect(markdown('Hey @Fox#123!', getMention: getMention),
          'Hey @Fox#123!');
      expect(markdown('Hey @[Fast Fox]#123!', getMention: getMention),
          'Hey @[Fast Fox]#123!');
      expect(markdown('Hey @[">]!', getMention: getMention),
          'Hey @[">]!');
    });
    test('latex', () {
      expect(markdown('meep \$\\frac{2}{3}\$'),
          'meep \\frac{2}{3}');
      expect(markdown('meep \$hmm *yay*\$'),
          'meep hmm *yay*');
      expect(markdown('you have \$somevar and \$someothervar'),
          'you have \$somevar and \$someothervar');
      expect(markdown('meep ||\$\\frac{2}{3}\$||'),
          'meep \\frac{2}{3}');
      expect(markdown('meep `\$\\frac{2}{3}\$`'),
          'meep \$\\frac{2}{3}\$');
    });
  });
}