import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:krezus/clients/storage/json_storage_client.dart';
import 'package:krezus/pages/home/home_page.dart';
import 'package:krezus/repositories/json/json_repository.dart';
import 'package:krezus/repositories/metadata/metadata_repository.dart';
import 'package:krezus/repositories/transactions/transactions_repository.dart';

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  Widget build(BuildContext context) {
    return const AppView();
  }
}

class AppView extends StatefulWidget {
  const AppView({super.key});

  @override
  State<AppView> createState() => _AppViewState();
}

class _AppViewState extends State<AppView> {
  late final JsonStorageClient _storageClient;
  late final JsonRepository _jsonRepository;
  late final TransactionsRepository _transactionsRepository;
  late final MetadataRepository _metadataRepository;

  @override
  void initState() {
    super.initState();

    _storageClient = JsonStorageClient();
    _jsonRepository = JsonRepository(storageClient: _storageClient);
    _transactionsRepository = TransactionsRepository(jsonRepository: _jsonRepository);
    _metadataRepository = MetadataRepository(jsonRepository: _jsonRepository);
    
    _transactionsRepository.loadTransactions();
    _metadataRepository.loadMetadata();

    _metadataRepository.getSettingsStream().listen((event) => setState(() {}));
  }

  @override
  Widget build(BuildContext context) {
    return MultiRepositoryProvider(
      providers: [
        RepositoryProvider.value(value: _jsonRepository),
        RepositoryProvider.value(value: _transactionsRepository),
        RepositoryProvider.value(value: _metadataRepository),
      ],
      child: DynamicColorBuilder(
        builder: ((lightDynamic, darkDynamic) {
          ColorScheme lightColorScheme;
          ColorScheme darkColorScheme;

          if (lightDynamic != null && darkDynamic != null) {
            lightColorScheme = lightDynamic.harmonized();
            darkColorScheme = darkDynamic.harmonized();
          } else {
            lightColorScheme = ColorScheme.fromSeed(
              seedColor: const Color.fromARGB(255, 103, 6, 231),
            );

            darkColorScheme = ColorScheme.fromSeed(
              seedColor: const Color.fromARGB(255, 103, 6, 231),
              brightness: Brightness.dark,
            );
          }

          return MaterialApp(
            title: 'Krezus',
            theme: ThemeData(colorScheme: lightColorScheme),
            darkTheme: ThemeData(colorScheme: darkColorScheme),
            themeMode: _metadataRepository.getSettings().themeMode,
            initialRoute: '/home',
            routes: {
              '/home':(context) => const HomePage(),
            },
            localizationsDelegates: const [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate
            ],
            supportedLocales: const [
              Locale('fr')
            ],
          );
        })
      ) 
    );
  }
}