import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

import 'package:smplayer/core/contracts/cancellation.dart';
import 'package:smplayer/core/infra/player_engine/player_engine.dart';

class FakePlayerEngine implements PlayerEngine {
  FakePlayerEngine({
    required bool playing,
    PlayerTracks tracks = const PlayerTracks(),
  }) {
    state
      ..playing = playing
      ..tracks = tracks;
  }

  final PlayerStreams _streams = PlayerStreams();
  final ValueNotifier<int?> _textureId = ValueNotifier<int?>(null);
  final ValueNotifier<int> _textureVersion = ValueNotifier<int>(0);
  final ValueNotifier<ui.Size?> _videoSize = ValueNotifier<ui.Size?>(null);
  final ValueNotifier<PlaybackStats?> _debugStats =
      ValueNotifier<PlaybackStats?>(null);

  @override
  final PlayerState state = PlayerState();

  int playCalls = 0;
  int pauseCalls = 0;

  @override
  PlayerStreams get stream => _streams;

  @override
  ValueListenable<int?> get textureId => _textureId;

  @override
  ValueListenable<int> get textureVersion => _textureVersion;

  @override
  ValueNotifier<ui.Size?> get videoSize => _videoSize;

  @override
  String get displayName => 'fake';

  @override
  bool get usesPlatformSurface => false;

  @override
  Future<int?> totalRxBytes() async => null;

  @override
  bool isNetworkStreamError(String error) => false;

  @override
  ValueListenable<PlaybackStats?> get debugStats => _debugStats;

  @override
  void configureHardwareDecoding(String mode) {}

  @override
  Future<void> open(
    String url, {
    Duration start = Duration.zero,
    bool play = true,
    Map<String, String>? headers,
    CancellationToken? cancel,
  }) async {}

  @override
  Future<void> play() async {
    playCalls++;
    state.playing = true;
    stream.addPlaying(true);
  }

  @override
  Future<void> pause() async {
    pauseCalls++;
    state.playing = false;
    stream.addPlaying(false);
  }

  @override
  Future<void> stop() async {}

  @override
  Future<void> seek(Duration position) async {}

  @override
  Future<void> setRate(double rate) async {}

  @override
  Future<void> setVolume(double volume) async {}

  @override
  Future<void> toggleMute() async {}

  @override
  Future<void> setAudioTrack(AudioTrack track) async {}

  @override
  Future<void> setSubtitleTrack(SubtitleTrack track) async {}

  void addTracks(PlayerTracks tracks) {
    state.tracks = tracks;
    stream.addTracks(tracks);
  }

  @override
  void setNativeSubtitleRendering(bool enabled) {}

  @override
  void setSubtitleStyle({
    required double fontSize,
    required double bgOpacity,
    required int position,
  }) {}

  @override
  Widget buildVideoView(BuildContext context, ValueListenable<BoxFit> fit) {
    return const SizedBox.shrink();
  }

  Future<String> getProperty(String name) async => '';

  @override
  Future<void> dispose() async {
    _streams.dispose();
    _textureId.dispose();
    _textureVersion.dispose();
    _videoSize.dispose();
    _debugStats.dispose();
  }
}
