import 'package:flutter_test/flutter_test.dart';

import 'package:chanora_flutter/services/channel_spacer.dart';

void main() {
  test('classifies only valid bracketed spacer tags as spacer channels', () {
    expect(isSpacerChannelName('[Spacer0]Lobby'), isTrue);
    expect(isSpacerChannelName('[cSpacer]Lobby'), isTrue);
    expect(isSpacerChannelName('[*spacer0]#=='), isTrue);
    expect(isSpacerChannelName('Lobby spacer room'), isFalse);
    expect(isSpacerChannelName('prefix [Spacer0]Lobby'), isFalse);
    expect(isSpacerChannelName('[xSpacer0]Lobby'), isFalse);
  });

  test('parses valid spacer names without requiring numeric suffixes', () {
    final parsed = parseSpacerChannelName('[cSpAcErabc-01]  Lobby  ');

    expect(parsed.isSpacer, isTrue);
    expect(parsed.isValid, isTrue);
    expect(parsed.alignment, SpacerAlignment.center);
    expect(parsed.isRepeating, isFalse);
    expect(parsed.uniqueSuffix, 'abc-01');
    expect(parsed.text, '  Lobby  ');
    expect(parsed.specialType, isNull);
    expect(parsed.isBlankSpacer, isFalse);
    expect(parsed.reason, isNull);
  });

  test('parses all special separator line values', () {
    expect(
      parseSpacerChannelName('[Spacer0]___').specialType,
      SpacerSpecialType.solidLine,
    );
    expect(
      parseSpacerChannelName('[Spacer0]---').specialType,
      SpacerSpecialType.dashLine,
    );
    expect(
      parseSpacerChannelName('[Spacer0]...').specialType,
      SpacerSpecialType.dotLine,
    );
    expect(
      parseSpacerChannelName('[Spacer0]-.-').specialType,
      SpacerSpecialType.dashDotLine,
    );
    expect(
      parseSpacerChannelName('[Spacer0]-..').specialType,
      SpacerSpecialType.dashDotDotLine,
    );
  });

  test('parses repeating spacer text exactly', () {
    final parsed = parseSpacerChannelName('[*spacer0]#==');

    expect(parsed.isSpacer, isTrue);
    expect(parsed.isValid, isTrue);
    expect(parsed.isRepeating, isTrue);
    expect(parsed.uniqueSuffix, '0');
    expect(parsed.text, '#==');
    expect(channelSpacerLabel('[*spacer0]#=='), startsWith('#==#=='));
  });

  test('parses known blank-looking right spacer', () {
    final parsed = parseSpacerChannelName('[rSpacer0].');

    expect(parsed.isSpacer, isTrue);
    expect(parsed.alignment, SpacerAlignment.right);
    expect(parsed.text, '.');
    expect(parsed.isBlankSpacer, isTrue);
    expect(channelSpacerLabel('[rSpacer0].'), isEmpty);
  });

  test('reports malformed spacer-like names', () {
    final missingBracket = parseSpacerChannelName('[cSpacer0');
    expect(missingBracket.isSpacer, isFalse);
    expect(missingBracket.isValid, isFalse);
    expect(missingBracket.reason, 'missing closing bracket');

    final invalidFlag = parseSpacerChannelName('[xSpacer0]Lobby');
    expect(invalidFlag.isSpacer, isFalse);
    expect(invalidFlag.isValid, isFalse);
    expect(invalidFlag.reason, 'invalid spacer tag');

    final duplicateAlignment = parseSpacerChannelName('[lcSpacer0]Lobby');
    expect(duplicateAlignment.isSpacer, isFalse);
    expect(duplicateAlignment.isValid, isFalse);
    expect(duplicateAlignment.reason, 'multiple alignment flags');
  });

  test('reports ordinary channel names as non-spacers', () {
    final parsed = parseSpacerChannelName('A channel with spacer in its name');

    expect(parsed.isSpacer, isFalse);
    expect(parsed.isValid, isFalse);
    expect(parsed.reason, 'not a spacer channel name');
    expect(
      channelSpacerLabel('A channel with spacer in its name'),
      'A channel with spacer in its name',
    );
  });

  test('formats spacer names deterministically', () {
    final formatted = formatSpacerChannelName(
      const SpacerChannelNameFormatOptions(
        alignment: SpacerAlignment.right,
        isRepeating: true,
        uniqueSuffix: 'abc',
        text: '#==',
      ),
    );

    expect(formatted, '[*rSpacerabc]#==');

    final parsed = parseSpacerChannelName(formatted);
    expect(parsed.isSpacer, isTrue);
    expect(parsed.alignment, SpacerAlignment.right);
    expect(parsed.isRepeating, isTrue);
    expect(parsed.uniqueSuffix, 'abc');
    expect(parsed.text, '#==');
  });

  test('formats existing display labels through the utility', () {
    expect(channelSpacerLabel('[cSpacer]Lobby'), 'Lobby');
    expect(channelSpacerLabel('[Spacer0]___'), '────────');
    expect(channelSpacerLabel('Lobby'), 'Lobby');
  });
}
