// Copyright 2017, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@TestOn('vm')
import 'dart:io';
import 'package:io/ansi.dart';
import 'package:test/test.dart';

const _ansiEscapeLiteral = '\x1B';
const _ansiEscapeForScript = '\\033';
const sampleInput = 'sample input';

void main() {
  group('ansiOutputEnabled', () {
    test('default value matches dart:io', () {
      expect(ansiOutputEnabled,
          stdout.supportsAnsiEscapes && stderr.supportsAnsiEscapes);
    });

    test('override true', () {
      overrideAnsiOutput(true, () {
        expect(ansiOutputEnabled, isTrue);
      });
    });

    test('override false', () {
      overrideAnsiOutput(false, () {
        expect(ansiOutputEnabled, isFalse);
      });
    });

    test('forScript variaents ignore `ansiOutputEnabled`', () {
      final expected =
          '$_ansiEscapeForScript[34m$sampleInput$_ansiEscapeForScript[0m';

      for (var override in [true, false]) {
        overrideAnsiOutput(override, () {
          expect(blue.escapeForScript, '$_ansiEscapeForScript[34m');
          expect(blue.wrap(sampleInput, forScript: true), expected);
          expect(wrapWith(sampleInput, [blue], forScript: true), expected);
        });
      }
    });
  });

  test('foreground and background colors match', () {
    expect(foregroundColors, hasLength(backgroundColors.length));

    for (var i = 0; i < foregroundColors.length; i++) {
      final foreground = foregroundColors[i];
      expect(foreground.type, AnsiCodeType.foreground);
      expect(foreground.name.toLowerCase(), foreground.name,
          reason: 'All names should be lower case');
      final background = backgroundColors[i];
      expect(background.type, AnsiCodeType.background);
      expect(background.name.toLowerCase(), background.name,
          reason: 'All names should be lower case');

      expect(foreground.name, background.name);

      // The last base-10 digit also matches – good to sanity check
      expect(foreground.code % 10, background.code % 10);
    }
  });

  test('all styles are styles', () {
    for (var style in styles) {
      expect(style.type, AnsiCodeType.style);
      expect(style.name.toLowerCase(), style.name,
          reason: 'All names should be lower case');
      if (style == styleBold) {
        expect(style.reset, resetBold);
      } else {
        expect(style.reset.code, equals(style.code + 20));
      }
      expect(style.name, equals(style.reset.name));
    }
  });

  for (var forScript in [true, false]) {
    group(forScript ? 'forScript' : 'escaped', () {
      final escapeLiteral =
          forScript ? _ansiEscapeForScript : _ansiEscapeLiteral;

      group('wrap', () {
        _test('color', () {
          final expected = '$escapeLiteral[34m$sampleInput$escapeLiteral[0m';

          expect(blue.wrap(sampleInput, forScript: forScript), expected);
        });

        _test('style', () {
          final expected = '$escapeLiteral[1m$sampleInput$escapeLiteral[22m';

          expect(styleBold.wrap(sampleInput, forScript: forScript), expected);
        });

        _test('style', () {
          final expected = '$escapeLiteral[34m$sampleInput$escapeLiteral[0m';

          expect(blue.wrap(sampleInput, forScript: forScript), expected);
        });

        test('empty', () {
          expect(blue.wrap('', forScript: forScript), '');
        });

        test(null, () {
          expect(blue.wrap(null, forScript: forScript), isNull);
        });
      });

      group('wrapWith', () {
        _test('foreground', () {
          final expected = '$escapeLiteral[34m$sampleInput$escapeLiteral[0m';

          expect(wrapWith(sampleInput, [blue], forScript: forScript), expected);
        });

        _test('background', () {
          final expected = '$escapeLiteral[44m$sampleInput$escapeLiteral[0m';

          expect(wrapWith(sampleInput, [backgroundBlue], forScript: forScript),
              expected);
        });

        _test('style', () {
          final expected = '$escapeLiteral[1m$sampleInput$escapeLiteral[0m';

          expect(wrapWith(sampleInput, [styleBold], forScript: forScript),
              expected);
        });

        _test('2 styles', () {
          final expected = '$escapeLiteral[1;3m$sampleInput$escapeLiteral[0m';

          expect(
              wrapWith(sampleInput, [styleBold, styleItalic],
                  forScript: forScript),
              expected);
        });

        _test('2 foregrounds', () {
          expect(
              () => wrapWith(sampleInput, [blue, white], forScript: forScript),
              throwsArgumentError);
        });

        _test('multi', () {
          final expected =
              '$escapeLiteral[1;4;34;107m$sampleInput$escapeLiteral[0m';

          expect(
              wrapWith(sampleInput,
                  [blue, backgroundWhite, styleBold, styleUnderlined],
                  forScript: forScript),
              expected);
        });

        test('no codes', () {
          expect(wrapWith(sampleInput, []), sampleInput);
        });

        _test('empty', () {
          expect(
              wrapWith('', [blue, backgroundWhite, styleBold],
                  forScript: forScript),
              '');
        });

        _test(null, () {
          expect(
              wrapWith(null, [blue, backgroundWhite, styleBold],
                  forScript: forScript),
              isNull);
        });
      });
    });
  }
}

void _test<T>(String name, T Function() body) =>
    test(name, () => overrideAnsiOutput<T>(true, body));
