import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:pharmacy_mobile/auth/auth_gate.dart';
import 'package:pharmacy_mobile/blocs/caches/categorylist/categorylist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/distributorlist/distributorlist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/genericlist/genericlist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/manufacturerlist/manufacturerlist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/medicinelist/medicinelist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/stocklist/stocklist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/supplierlist/supplierlist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/caches/typelist/typelist_cache_bloc.dart';
import 'package:pharmacy_mobile/blocs/guest/guest_bloc.dart';
import 'package:pharmacy_mobile/blocs/language/language_bloc.dart';
import 'package:pharmacy_mobile/blocs/user/user_bloc.dart';
import 'package:pharmacy_mobile/pages/add_category_page.dart';
import 'package:pharmacy_mobile/pages/add_distributor_page.dart';
import 'package:pharmacy_mobile/pages/add_generics_page.dart';
import 'package:pharmacy_mobile/pages/add_manufacturer_page.dart';
import 'package:pharmacy_mobile/pages/add_medicine_page.dart';
import 'package:pharmacy_mobile/pages/add_stock_page.dart';
import 'package:pharmacy_mobile/pages/add_supplier_page.dart';
import 'package:pharmacy_mobile/pages/add_type_page.dart';
import 'package:pharmacy_mobile/pages/customer_page.dart';
import 'package:pharmacy_mobile/pages/customer_pages/customer_itemview_page.dart';
import 'package:pharmacy_mobile/pages/delete_stock_page.dart';
import 'package:pharmacy_mobile/pages/list_stocks_page.dart';
import 'package:pharmacy_mobile/pages/login_page.dart';
import 'package:go_router/go_router.dart';
import 'package:pharmacy_mobile/pages/main_page.dart';
import 'package:pharmacy_mobile/pages/register_page.dart';
import 'package:pharmacy_mobile/pages/settings_pages/language_setting_page.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

Future<void> main() async {
  await dotenv.load(fileName: ".env");
  WidgetsFlutterBinding.ensureInitialized();

  final supUrl = dotenv.env['SUPABASE_URL']!;
  final supAnonkey = dotenv.env['SUPABASE_ANON_KEY']!;

  await Supabase.initialize(
    url: supUrl,
    anonKey: supAnonkey,
  );

  runApp(MyApp());
}

final _router = GoRouter(
  initialLocation: '/',
  routes: [
    GoRoute(
      name: 'index',
      path: '/',
      builder: (context, state) => const AuthGate(),
    ),
    GoRoute(
      name: 'login',
      path: '/login',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const LoginPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0, 0.95), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'register',
      path: '/register',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const RegisterPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0, 0.95), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'main',
      path: '/main',
      builder: (context, state) => const MainPage(),
    ),
    GoRoute(
      name: 'addmanufactorer',
      path: '/addmanufactorer',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const AddManufacturerPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'adddistributor',
      path: '/adddistributor',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const AddDistributorPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'addsupplier',
      path: '/addsupplier',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const AddSupplierPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'addmedicines',
      path: '/addmedicines',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const AddMedicinePage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'addgenerics',
      path: '/addgenerics',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const AddGenericsPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'addtype',
      path: '/addtype',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const AddTypePage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'addcategory',
      path: '/addcategory',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const AddCategoryPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'addstock',
      path: '/addstock',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const AddStockPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'liststocks',
      path: '/liststocks',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const ListStocksPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'deletestock',
      path: '/deletestock',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const DeleteStockPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeOut))),
                child: child),
      ),
    ),
    GoRoute(
      name: 'customer',
      path: '/customer',
      builder: (context, state) => const CustomerPage(),
    ),
    GoRoute(
      name: 'itemview',
      path: '/itemview',
      builder: (context, state) => const CustomerItemviewPage(),
    ),
    GoRoute(
      name: 'languagesetting',
      path: '/languagesetting',
      pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage<void>(
        key: state.pageKey,
        child: const LanguageSettingPage(),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation,
                Widget child) =>
            SlideTransition(
                position: animation.drive(
                    Tween<Offset>(begin: Offset(0.95, 0), end: Offset.zero).chain(CurveTween(curve: Curves.easeIn))),
                child: child),
      ),
    ),
  ],
);

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(
          create: (context) => GuestBloc(),
        ),
        BlocProvider(
          create: (context) => UserBloc(),
        ),
        BlocProvider(
          create: (context) => CategoryListBloc(),
        ),
        BlocProvider(
          create: (context) => GenericListBloc(),
        ),
        BlocProvider(
          create: (context) => TypeListBloc(),
        ),
        BlocProvider(
          create: (context) => ManufacturerListBloc(),
        ),
        BlocProvider(
          create: (context) => MedicineListBloc(),
        ),
        BlocProvider(
          create: (context) => StockListBloc(),
        ),
        BlocProvider(
          create: (context) => LanguageBloc(),
        ),
        BlocProvider(
          create: (context) => DistributorListBloc(),
        ),
        BlocProvider(
          create: (context) => SupplierListBloc(),
        ),
      ],
      child: MaterialApp.router(
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          useMaterial3: true,
        ),
        routerConfig: _router,
      ),
    );
  }
}
