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/locale.dart';
import 'package:pshared/provider/permissions.dart';
import 'package:pshared/provider/account.dart';
import 'package:pshared/provider/organizations.dart';

import 'package:pweb/app/app.dart';
import 'package:pweb/app/timeago.dart';
import 'package:pweb/providers/carousel.dart';
import 'package:pweb/providers/mock_payment.dart';
import 'package:pweb/providers/operatioins.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/providers/wallet_transactions.dart';
import 'package:pweb/services/amplitude.dart';
import 'package:pweb/services/operations.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/wallet_transactions.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: [
        ChangeNotifierProvider(create: (_) => LocaleProvider(null)),
        ChangeNotifierProxyProvider<LocaleProvider, AccountProvider>(
          create: (_) => AccountProvider(),
          update: (context, localeProvider, provider) => provider!..updateProvider(localeProvider),
        ),
        ChangeNotifierProxyProvider<AccountProvider, TwoFactorProvider>(
          create: (_) => TwoFactorProvider(),
          update: (context, accountProvider, provider) => provider!..update(accountProvider),
        ),
        ChangeNotifierProvider(create: (_) => OrganizationsProvider()),
        ChangeNotifierProxyProvider<OrganizationsProvider, PermissionsProvider>(
          create: (_) => PermissionsProvider(),
          update: (context, orgnization, provider) => provider!..update(orgnization),
        ),
        ChangeNotifierProvider(create: (_) => CarouselIndexProvider()),

        ChangeNotifierProvider(
          create: (_) => UploadHistoryProvider(service: MockUploadHistoryService())..load(),
        ),
        ChangeNotifierProvider(
          create: (_) => PaymentMethodsProvider(service: MockPaymentMethodsService())..loadMethods(),
        ),
        ChangeNotifierProxyProvider<OrganizationsProvider, WalletsProvider>(
          create: (_) => WalletsProvider(ApiWalletsService()),
          update: (context, organizations, provider) => provider!..update(organizations),
        ),
        ChangeNotifierProvider(
          create: (_) => WalletTransactionsProvider(MockWalletTransactionsService())..load(),
        ),
        ChangeNotifierProvider(
          create: (_) => MockPaymentProvider(),
        ),
        ChangeNotifierProvider(
          create: (_) => RecipientProvider(RecipientService())..loadRecipients(),
        ),
        
        ChangeNotifierProxyProvider3<RecipientProvider, WalletsProvider, PaymentMethodsProvider, PageSelectorProvider>(
          create: (context) => PageSelectorProvider(),
          update: (context, recipientProv, walletsProv, methodsProv, previous) =>
            previous ?? PageSelectorProvider(
              recipientProvider: recipientProv,
              walletsProvider: walletsProv,
              methodsProvider: methodsProv,
            )..update(recipientProv, walletsProv, methodsProv),
        ),
        
        ChangeNotifierProvider(
          create: (_) => OperationProvider(OperationService())..loadOperations(),
        ),
      ],
      child: const PayApp(),
    ),

  );
}
