part of '../di_providers.dart';


final appDataDirProvider = FutureProvider<Directory>((ref) async {
  final base = await getApplicationSupportDirectory();
  final dir = Directory('${base.path}/smplayer');
  if (!await dir.exists()) {
    await dir.create(recursive: true);
  }
  return dir;
});


final deviceIdProvider = FutureProvider<String>((ref) async {
  final store = DeviceIdStore();
  return store.getOrCreate();
});


final serverRepositoryProvider = FutureProvider<ServerRepository>((ref) async {
  final dir = await ref.watch(appDataDirProvider.future);
  return JsonServerRepository(file: File('${dir.path}/servers.json'));
});


final sessionRepositoryProvider = FutureProvider<SessionRepository>((
  ref,
) async {
  final dir = await ref.watch(appDataDirProvider.future);
  return JsonSessionRepository(file: File('${dir.path}/sessions.json'));
});


final searchHistoryStoreProvider = Provider<SearchHistoryStore>(
  (ref) => SearchHistoryStore(),
);


final danmakuSourcesStoreProvider = Provider<DanmakuSourcesStore>(
  (ref) => PrefsDanmakuSourcesStore(),
);


final homePageCacheStoreProvider = FutureProvider<HomePageCacheStore>((
  ref,
) async {
  final dir = await ref.watch(appDataDirProvider.future);
  return HomePageCacheStore(directory: dir);
});


final discoverPageCacheStoreProvider = FutureProvider<DiscoverPageCacheStore>((
  ref,
) async {
  final dir = await ref.watch(appDataDirProvider.future);
  return DiscoverPageCacheStore(directory: dir);
});


final embyGatewayProvider = FutureProvider<EmbyGateway>((ref) async {
  final deviceId = await ref.read(deviceIdProvider.future);
  return EmbyApiClient(deviceId: deviceId, deviceName: kEmbyDeviceName);
});


final tmdbGatewayProvider = Provider<TmdbGateway>((ref) => TmdbApiClient());


final danmakuGatewayProvider = Provider<DanmakuGateway>(
  (ref) => DispatchingDanmakuGateway(
    httpGateway: DanmakuApiClient(),
    scriptWidgetGateway: ScriptWidgetDanmakuGateway(
      loadService: () => ref.read(scriptWidgetServiceProvider.future),
    ),
  ),
);
