import 'package:flutter/material.dart';

// ignore: depend_on_referenced_packages
import 'package:flutter_web_plugins/flutter_web_plugins.dart';

import 'package:provider/provider.dart';

import 'package:logging/logging.dart';

import 'package:pshared/config/constants.dart';
import 'package:pshared/provider/account.dart';
import 'package:pshared/provider/locale.dart';
import 'package:pshared/provider/organizations.dart';

import 'package:pweb/app/app.dart';
import 'package:pweb/app/timeago.dart';
import 'package:pweb/providers/balance.dart';
import 'package:pweb/providers/carousel.dart';
import 'package:pweb/providers/mock_payment.dart';
import 'package:pweb/providers/page_selector.dart';
import 'package:pweb/providers/payment_methods.dart';
import 'package:pweb/providers/recipient.dart';
import 'package:pweb/providers/two_factor.dart';
import 'package:pweb/providers/upload_history.dart';
import 'package:pweb/providers/wallets.dart';
import 'package:pweb/services/amplitude.dart';
import 'package:pweb/services/auth.dart';
import 'package:pweb/services/balance.dart';
import 'package:pweb/services/payments/payment_methods.dart';
import 'package:pweb/services/payments/upload_history.dart';
import 'package:pweb/services/recipient/recipient.dart';
import 'package:pweb/services/wallets.dart';


void _setupLogging() {
  Logger.root.level = Level.ALL; 
  Logger.root.onRecord.listen((record) {
    // ignore: avoid_print
    print('${record.level.name}: ${record.time}: ${record.loggerName}: ${record.message}');
  });
}

void main() async {
  await Constants.initialize();
  await AmplitudeService.initialize();


  _setupLogging();
  setUrlStrategy(PathUrlStrategy());

  initializeTimeagoLocales();

  runApp(
    MultiProvider(
      providers: [
        Provider<AuthenticationService>(
          create: (_) => AuthenticationService(),
        ),
        ChangeNotifierProxyProvider<AuthenticationService, TwoFactorProvider>(
          create: (context) => TwoFactorProvider(
            context.read<AuthenticationService>(),
          ),
          update: (context, authService, previous) => TwoFactorProvider(authService),
        ),
        ChangeNotifierProvider(create: (_) => LocaleProvider(null)),
        ChangeNotifierProvider(create: (_) => AccountProvider()),
        ChangeNotifierProvider(create: (_) => OrganizationsProvider()),
        ChangeNotifierProvider(create: (_) => AccountProvider()),
        ChangeNotifierProvider(create: (_) => CarouselIndexProvider()),

        ChangeNotifierProvider(
          create: (_) => UploadHistoryProvider(service: MockUploadHistoryService())..load(),
        ),
        ChangeNotifierProvider(
          create: (_) => PaymentMethodsProvider(service: MockPaymentMethodsService())..loadMethods(),
        ),
        ChangeNotifierProvider(
          create: (_) => WalletsProvider(MockWalletsService())..loadData(),
        ),
        ChangeNotifierProvider(
          create: (_) => MockPaymentProvider(),
        ),
        ChangeNotifierProvider(
          create: (_) => RecipientProvider(RecipientService())..loadRecipients(),
        ),
        ChangeNotifierProvider(
          create: (context) {
            final recipient = context.read<RecipientProvider?>();
            final wallets = context.read<WalletsProvider?>();
            return PageSelectorProvider(
              recipientProvider: recipient,
              walletsProvider: wallets,
            );
          },
        ),
        ChangeNotifierProvider(
          create: (_) => BalanceProvider(MockBalanceService())..loadData(),
        ),
      ],
      child: const PayApp(),
    ),
  );
}
