import 'package:flutter/material.dart';

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

import 'package:go_router/go_router.dart';

import 'package:provider/provider.dart';

import 'package:logging/logging.dart';

import 'package:pshared/config/constants.dart';
import 'package:pshared/controllers/balance_mask/ledger_accounts.dart';
import 'package:pshared/controllers/balance_mask/wallets.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/email_verification.dart';
import 'package:pshared/provider/ledger.dart';
import 'package:pshared/provider/payment/wallets.dart';
import 'package:pshared/provider/payment/payments.dart';
import 'package:pshared/provider/invitations.dart';
import 'package:pshared/service/ledger.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/two_factor.dart';
import 'package:pweb/services/posthog.dart';
import 'package:pweb/providers/account.dart';
import 'package:pweb/providers/locale.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());
  GoRouter.optionURLReflectsImperativeAPIs = true;

  initializeTimeagoLocales();

  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider<LocaleProvider>(
          create: (_) => PwebLocaleProvider(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),
        ),
        ChangeNotifierProxyProvider<AccountProvider, OrganizationsProvider>(
          //TODO controll scope of the provider
          create: (_) => OrganizationsProvider(),
          lazy: false,
          update: (_, accountProvider, organizations) =>
              organizations!..updateAccount(accountProvider),
        ),
        ChangeNotifierProxyProvider<OrganizationsProvider, PermissionsProvider>(
          create: (_) => PermissionsProvider(),
          update: (context, orgnization, provider) =>
              provider!..update(orgnization),
        ),
        ChangeNotifierProxyProvider<OrganizationsProvider, EmployeesProvider>(
          create: (_) => EmployeesProvider(),
          update: (context, organizations, provider) =>
              provider!..updateProviders(organizations),
        ),
        ChangeNotifierProxyProvider<OrganizationsProvider, PaymentsProvider>(
          create: (_) => PaymentsProvider(),
          update: (context, organizations, provider) =>
              provider!..update(organizations),
        ),
        ChangeNotifierProvider(create: (_) => EmailVerificationProvider()),
        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),
        ),
        ChangeNotifierProxyProvider<
          OrganizationsProvider,
          LedgerAccountsProvider
        >(
          create: (_) => LedgerAccountsProvider(LedgerService()),
          update: (context, organizations, provider) =>
              provider!..update(organizations),
        ),
        ChangeNotifierProxyProvider<
          LedgerAccountsProvider,
          LedgerBalanceMaskController
        >(
          create: (_) => LedgerBalanceMaskController(),
          update: (context, ledger, controller) => controller!..update(ledger),
        ),
        ChangeNotifierProxyProvider<WalletsProvider, WalletsController>(
          create: (_) => WalletsController(),
          update: (_, wallets, controller) => controller!..update(wallets),
        ),
      ],
      child: const PayApp(),
    ),
  );
}
