import 'package:flutter/material.dart';
import 'package:habo/navigation/app_state_manager.dart';
import 'package:habo/navigation/routes.dart';
import 'package:habo/habits/habits_screen.dart';
import 'package:habo/statistics/statistics_screen.dart';
import 'package:habo/settings/settings_screen.dart';
import 'package:habo/onboarding/onboarding.dart';
import 'package:habo/habits/create_habit.dart';
import 'package:habo/habits/edit_habit.dart';

class AppRouter extends RouterDelegate<HaboRouteConfiguration>
    with ChangeNotifier, PopNavigatorRouterDelegateMixin<HaboRouteConfiguration> {
  final AppStateManager appStateManager;

  AppRouter(this.appStateManager) {
    appStateManager.addListener(notifyListeners);
  }

  @override
  GlobalKey<NavigatorState> get navigatorKey => GlobalKey<NavigatorState>();

  @override
  HaboRouteConfiguration? get currentConfiguration {
    if (appStateManager.onboarding) return HaboRouteConfiguration(path: RouteConstants.onboardingPath);
    if (appStateManager.statistics) return HaboRouteConfiguration(path: RouteConstants.statisticsPath);
    if (appStateManager.settings) return HaboRouteConfiguration(path: RouteConstants.settingsPath);
    if (appStateManager.createHabit) return HaboRouteConfiguration(path: RouteConstants.createHabitPath);
    if (appStateManager.editHabit) return HaboRouteConfiguration(path: RouteConstants.editHabitPath);
    return HaboRouteConfiguration(path: RouteConstants.habitsPath);
  }

  @override
  Widget build(BuildContext context) {
    final pages = <Page<dynamic>>[];

    // Always start with habits screen
    pages.add(
      const MaterialPage<dynamic>(
        child: HabitsScreen(),
        key: ValueKey('habits'),
      ),
    );

    if (appStateManager.statistics) {
      pages.add(
        const MaterialPage<dynamic>(
          child: StatisticsScreen(),
          key: ValueKey('statistics'),
        ),
      );
    }

    if (appStateManager.settings) {
      pages.add(
        const MaterialPage<dynamic>(
          child: SettingsScreen(),
          key: ValueKey('settings'),
        ),
      );
    }

    if (appStateManager.onboarding) {
      pages.add(
        const MaterialPage<dynamic>(
          child: OnboardingScreen(),
          key: ValueKey('onboarding'),
        ),
      );
    }

    if (appStateManager.createHabit) {
      pages.add(
        const MaterialPage<dynamic>(
          child: CreateHabitScreen(),
          key: ValueKey('create'),
        ),
      );
    }

    if (appStateManager.editHabit) {
      pages.add(
        const MaterialPage<dynamic>(
          child: EditHabitScreen(),
          key: ValueKey('edit'),
        ),
      );
    }

    return Navigator(
      key: navigatorKey,
      pages: pages,
      onDidRemovePage: (page) {},
    );
  }

  @override
  Future<void> setNewRoutePath(HaboRouteConfiguration configuration) async {
    // Handle deep links
  }
}

class HaboRouteConfiguration {
  final String path;
  HaboRouteConfiguration({required this.path});
}
