/*
 * Copyright (c) 2025
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import 'package:flutter_test/flutter_test.dart';
import 'package:media_kit/media_kit.dart';
import 'package:splash_video/splash_video.dart';

void main() {
  // Initialize MediaKit for testing
  setUpAll(() {
    MediaKit.ensureInitialized();
  });

  group('SplashVideoController', () {
    late Player player;

    setUp(() {
      player = Player();
    });

    tearDown(() async {
      await player.dispose();
    });

    test('creates with default values', () {
      final controller = SplashVideoController();
      expect(controller.loopVideo, isFalse);
      expect(controller.skipRequested, isFalse);
    });

    test('creates with loopVideo enabled', () {
      final controller = SplashVideoController(loopVideo: true);
      expect(controller.loopVideo, isTrue);
    });

    test('throws StateError when accessing player before attach', () {
      final controller = SplashVideoController();
      expect(
        () => controller.player,
        throwsStateError,
      );
    });

    test('allows player access after attach', () {
      final controller = SplashVideoController();
      controller.attach(player);
      expect(controller.player, equals(player));
    });

    test('attach throws StateError after disposal', () {
      final controller = SplashVideoController();
      controller.dispose();
      expect(
        () => controller.attach(player),
        throwsStateError,
      );
    });

    test('skip sets skipRequested flag', () async {
      final controller = SplashVideoController();
      controller.attach(player);
      
      expect(controller.skipRequested, isFalse);
      await controller.skip();
      expect(controller.skipRequested, isTrue);
    });

    test('play/pause operations work with attached player', () async {
      final controller = SplashVideoController();
      controller.attach(player);

      // These should complete without error
      await expectLater(controller.play(), completes);
      await expectLater(controller.pause(), completes);
    });

    test('operations are no-ops after disposal', () async {
      final controller = SplashVideoController();
      controller.attach(player);
      controller.dispose();

      // These should complete without error (no-op)
      await expectLater(controller.play(), completes);
      await expectLater(controller.pause(), completes);
      await expectLater(controller.skip(), completes);
    });

    test('multiple dispose calls are safe', () {
      final controller = SplashVideoController();
      controller.dispose();
      controller.dispose();
      // Should not throw
    });
  });

  group('SplashVideoController lifecycle', () {
    test('can create multiple controllers', () {
      final controller1 = SplashVideoController();
      final controller2 = SplashVideoController(loopVideo: true);
      
      expect(controller1.loopVideo, isFalse);
      expect(controller2.loopVideo, isTrue);
      
      controller1.dispose();
      controller2.dispose();
    });

    test('skipRequested resets on new controller', () async {
      final player1 = Player();
      final controller1 = SplashVideoController();
      controller1.attach(player1);
      await controller1.skip();
      expect(controller1.skipRequested, isTrue);
      
      controller1.dispose();
      
      final controller2 = SplashVideoController();
      expect(controller2.skipRequested, isFalse);
      
      controller2.dispose();
      await player1.dispose();
    });
  });
}
