import 'package:didvan/models/tag.dart';
import 'package:didvan/views/authentication/authentication.dart';
import 'package:didvan/views/authentication/authentication_state.dart';
import 'package:didvan/views/home/comments/comments.dart';
import 'package:didvan/views/home/comments/comments_state.dart';
import 'package:didvan/views/home/direct/direct.dart';
import 'package:didvan/views/home/direct/direct_state.dart';
import 'package:didvan/views/home/hashtag/hashtag.dart';
import 'package:didvan/views/home/hashtag/hashtag_state.dart';
import 'package:didvan/views/home/home.dart';
import 'package:didvan/views/home/home_state.dart';
import 'package:didvan/views/home/news/news_details/news_details.dart';
import 'package:didvan/views/home/news/news_details/news_details_state.dart';
import 'package:didvan/views/home/news/news_state.dart';
import 'package:didvan/views/home/radar/radar_details/radar_details.dart';
import 'package:didvan/views/home/radar/radar_details/radar_details_state.dart';
import 'package:didvan/views/home/radar/radar_state.dart';
import 'package:didvan/views/home/settings/about_us/about_us.dart';
import 'package:didvan/views/home/settings/bookmarks/bookmarks.dart';
import 'package:didvan/views/home/settings/bookmarks/bookmark_state.dart';
import 'package:didvan/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmark.dart';
import 'package:didvan/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmarks_state.dart';
import 'package:didvan/views/home/settings/direct_list/direct_list.dart';
import 'package:didvan/views/home/settings/direct_list/direct_list_state.dart';
import 'package:didvan/views/home/settings/general_settings/settings.dart';
import 'package:didvan/views/home/settings/general_settings/settings_state.dart';
import 'package:didvan/views/home/settings/profile/profile.dart';
import 'package:didvan/views/home/studio/studio_state.dart';
import 'package:didvan/views/splash/splash.dart';
import 'package:didvan/routes/routes.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class RouteGenerator {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case Routes.splash:
        return _createRoute(
          const Splash(),
        );
      case Routes.authenticaion:
        return _createRoute(
          ChangeNotifierProvider<AuthenticationState>(
            create: (context) => AuthenticationState(),
            child: Authentication(isResetPassword: settings.arguments as bool),
          ),
        );
      case Routes.home:
        return _createRoute(
          MultiProvider(
            providers: [
              ChangeNotifierProvider<HomeState>(
                create: (context) => HomeState(),
              ),
              ChangeNotifierProvider<RadarState>(
                create: (context) => RadarState(),
              ),
              ChangeNotifierProvider<NewsState>(
                create: (context) => NewsState(),
              ),
              ChangeNotifierProvider<StudioState>(
                create: (context) => StudioState(),
              ),
            ],
            child: const Home(),
          ),
        );
      case Routes.profile:
        return _createRoute(
          const Profile(),
        );
      case Routes.aboutUs:
        return _createRoute(
          const AboutUs(),
        );
      case Routes.generalSettings:
        return _createRoute(
          ChangeNotifierProvider<GeneralSettingsState>(
            create: (context) => GeneralSettingsState(),
            child: const GeneralSettings(),
          ),
        );
      case Routes.radarDetails:
        return _createRoute(
          ChangeNotifierProvider<RadarDetailsState>(
            create: (context) => RadarDetailsState(),
            child: RadarDetails(
              pageData: settings.arguments as Map<String, dynamic>,
            ),
          ),
        );
      case Routes.newsDetails:
        return _createRoute(
          ChangeNotifierProvider<NewsDetailsState>(
            create: (context) => NewsDetailsState(),
            child: NewsDetails(
              pageData: settings.arguments as Map<String, dynamic>,
            ),
          ),
        );
      case Routes.directList:
        return _createRoute(
          ChangeNotifierProvider<DirectListState>(
            create: (context) => DirectListState(),
            child: const DirectList(),
          ),
        );
      case Routes.direct:
        return _createRoute(
          ChangeNotifierProvider<DirectState>(
            create: (context) => DirectState(),
            child: Direct(pageData: settings.arguments as Map<String, dynamic>),
          ),
        );
      case Routes.comments:
        return _createRoute(
          ChangeNotifierProvider<CommentsState>(
            create: (context) => CommentsState(),
            child: Comments(
              pageData: settings.arguments as Map<String, dynamic>,
            ),
          ),
        );
      case Routes.bookmarks:
        return _createRoute(
          ChangeNotifierProvider<BookmarksState>(
            create: (context) => BookmarksState(),
            child: const Bookmarks(),
          ),
        );
      case Routes.hashtag:
        return _createRoute(
          ChangeNotifierProvider<HashtagState>(
            create: (context) => HashtagState(),
            child: Hashtag(tag: settings.arguments as Tag),
          ),
        );

      case Routes.filteredBookmarks:
        return _createRoute(
          ChangeNotifierProvider<FilteredBookmarksState>(
            create: (context) => FilteredBookmarksState(
              settings.arguments as String,
            ),
            child: const FilteredBookmarks(),
          ),
        );
      default:
        return _errorRoute();
    }
  }

  static Route<dynamic> _errorRoute() {
    return MaterialPageRoute(builder: (_) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('Error'),
        ),
        body: const Center(
          child: Text('ERROR'),
        ),
      );
    });
  }

  static Route _createRoute(page) {
    return MaterialPageRoute(
      builder: (context) {
        final shortestSide = MediaQuery.of(context).size.shortestSide;
        final bool useMobileLayout = shortestSide < 600;
        if (kIsWeb && !useMobileLayout) {
          return Container(
            color: Theme.of(context).colorScheme.background,
            alignment: Alignment.center,
            child: AspectRatio(aspectRatio: 9 / 16, child: page),
          );
        }
        return Container(
          color: Theme.of(context).colorScheme.surface,
          child: SafeArea(
            child: page,
            top: false,
          ),
        );
      },
    );
  }
}
