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:pshared/provider/accounts/employees.dart';
import 'package:pshared/provider/recipient/pmethods.dart';
import 'package:pshared/provider/recipient/provider.dart';
import 'package:pshared/provider/payment/wallets.dart';
import 'package:pshared/provider/invitations.dart';
import 'package:pshared/service/payment/wallets.dart';

import 'package:pweb/app/app.dart';
import 'package:pweb/pages/invitations/widgets/list/view_model.dart';
import 'package:pweb/app/timeago.dart';
import 'package:pweb/providers/carousel.dart';
import 'package:pweb/providers/operatioins.dart';
import 'package:pweb/providers/two_factor.dart';
import 'package:pweb/providers/upload_history.dart';
import 'package:pweb/providers/wallet_transactions.dart';
import 'package:pweb/services/operations.dart';
import 'package:pweb/services/payments/history.dart';
import 'package:pweb/services/posthog.dart';
import 'package:pweb/services/wallet_transactions.dart';
import 'package:pweb/providers/account.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 {
  WidgetsFlutterBinding.ensureInitialized();
  await Constants.initialize();
  await PosthogService.initialize();


  _setupLogging();
  setUrlStrategy(PathUrlStrategy());

  initializeTimeagoLocales();

  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => LocaleProvider(null)),
        ChangeNotifierProxyProvider<LocaleProvider, AccountProvider>(
          create: (_) => PwebAccountProvider(),
          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),
        ),
        ChangeNotifierProxyProvider<OrganizationsProvider, EmployeesProvider>(
          create: (_) => EmployeesProvider(),
          update: (context, organizations, provider) => provider!..updateProviders(organizations),
        ),
        ChangeNotifierProvider(create: (_) => CarouselIndexProvider()),

        ChangeNotifierProvider(
          create: (_) => UploadHistoryProvider(service: MockUploadHistoryService())..load(),
        ),
        ChangeNotifierProxyProvider<OrganizationsProvider, RecipientsProvider>(
          create: (_) => RecipientsProvider(),
          update: (context, organizations, provider) => provider!..updateProviders(organizations),
        ),
        ChangeNotifierProxyProvider<OrganizationsProvider, InvitationsProvider>(
          create: (_) => InvitationsProvider(),
          update: (context, organizations, provider) => provider!..updateProviders(organizations),
        ),
        ChangeNotifierProxyProvider2<OrganizationsProvider, RecipientsProvider, PaymentMethodsProvider>(
           create: (_) => PaymentMethodsProvider(),
           update: (context, organizations, recipients, provider) => provider!..updateProviders(organizations, recipients),
        ),
        ChangeNotifierProvider(
          create: (_) => InvitationListViewModel(),
        ),
        ChangeNotifierProxyProvider<OrganizationsProvider, WalletsProvider>(
          create: (_) => WalletsProvider(ApiWalletsService()),
          update: (context, organizations, provider) => provider!..update(organizations),
        ),
        ChangeNotifierProvider(
          create: (_) => WalletTransactionsProvider(MockWalletTransactionsService())..load(),
        ),
        ChangeNotifierProvider(
          create: (_) => OperationProvider(OperationService())..loadOperations(),
        ),
      ],
      child: const PayApp(),
    ),

  );
}