import 'package:agrilink_vocpro/features/auth/view/login_screen.dart';
import 'package:agrilink_vocpro/features/dashboard/view/dashboard_screen.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/conductivity/view/conductivity_screen.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/humidity/view/humidity_screen.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/luminosity/view/light_screen.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/nitrogen/view/nitrogen_screen.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/ph/view/ph_screen.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/phosphorus/view/phosphorus_screen.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/potassium/view/potassium_screen.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/soil_moisture/view/soil_moisture_screen.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/soil_temperature/view/soil_temperature_screen.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/temperature/view/temperature_screen.dart';
import 'package:agrilink_vocpro/features/setting/view/account_screen.dart';
import 'package:agrilink_vocpro/features/splash/view/splash_screen.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

class AppRoute {
  static const String root = '/';
  static const String login = '/login';
  static const String dashboard = '/dashboard';
  static const String home = '/home';
  static const String profile = '/profile';
  static const String setting = '/setting';
  static const String account = '/dashboard/account';
  static const String humidity = '/dashboard/humidity';
  static const String temperature = '/dashboard/temperature';
  static const String soilTemperature = '/dashboard/soil_temperature';
  static const String soilMoisture = '/dashboard/soil_moisture';
  static const String light = '/dashboard/light';
  static const String ph = '/dashboard/ph';
  static const String conductivity = '/dashboard/conductivity';
  static const String nitrogen = '/dashboard/nitrogen';
  static const String phosphorus = '/dashboard/phosphorus';
  static const String potassium = '/dashboard/potassium';

  static final GoRouter router = GoRouter(
    initialLocation: root,
    routes: [
      //splash
      GoRoute(
        path: root,
        builder: (context, state) => const SplashScreen(),
      ),
      //login
      GoRoute(
        path: login,
        builder: (context, state) => const LoginScreen(),
      ),
      GoRoute(
        path: dashboard,
        builder: (context, state) => const DashboardScreen(),
        routes: [
          // humidity
          buildHumidityRoute(),
          // light intensity
          buildLightIntensityRoute(),
          // temperature
          buildTemperatureRoute(),
          // ph
          buildAcidityRoute(),
          // soil moisture
          buildSoilTempRoute(),
          // soil moisture
          buildSoilMoistureRoute(),
          // conductivity
          buildConductivityRoute(),
          // nitrogen
          buildNitrogenRoute(),
          // phosphorus
          buildPhosphorusRoute(),
          // potassium
          buildPotassiumRoute(),
          GoRoute(
              path: 'account',
              builder: (context, state) {
                return AccountScreen();
              })
        ],
      ),
    ],
    errorBuilder: (context, state) => Scaffold(
      body: Center(
        child: Text('Error: ${state.error}'),
      ),
    ),
  );

  static GoRoute buildPotassiumRoute() {
    return GoRoute(
      path: 'potassium/:value1/:value2',
      builder: (context, state) {
        final double value1 =
            double.tryParse(state.pathParameters['value1'] ?? '') ?? 0.0;
        final double value2 =
            double.tryParse(state.pathParameters['value2'] ?? '') ?? 0.0;
        return PotassiumScreen(potassiumNpk1: value1, potassiumNpk2: value2);
      },
    );
  }

  static GoRoute buildPhosphorusRoute() {
    return GoRoute(
      path: 'phosphorus/:value1/:value2',
      builder: (context, state) {
        final double value1 =
            double.tryParse(state.pathParameters['value1'] ?? '') ?? 0.0;
        final double value2 =
            double.tryParse(state.pathParameters['value2'] ?? '') ?? 0.0;
        return PhosphorusScreen(
          phosphorusNpk1: value1,
          phosphorusNpk2: value2,
        );
      },
    );
  }

  static GoRoute buildNitrogenRoute() {
    return GoRoute(
      path: 'nitrogen/:value1/:value2',
      builder: (context, state) {
        final double value1 =
            double.tryParse(state.pathParameters['value1'] ?? '') ?? 0.0;
        final double value2 =
            double.tryParse(state.pathParameters['value2'] ?? '') ?? 0.0;
        return NitrogenScreen(nitrogenNpk1: value1, nitrogenNpk2: value2);
      },
    );
  }

  static GoRoute buildConductivityRoute() {
    return GoRoute(
      path: 'conductivity/:value1/:value2',
      builder: (context, state) {
        final double value1 =
            double.tryParse(state.pathParameters['value1'] ?? '') ?? 0.0;
        final double value2 =
            double.tryParse(state.pathParameters['value2'] ?? '') ?? 0.0;
        return ConductivityScreen(
          conductivityNpk1: value1,
          conductivityNpk2: value2,
        );
      },
    );
  }

  static GoRoute buildSoilMoistureRoute() {
    return GoRoute(
      path: 'soil_moisture/:value1/:value2',
      builder: (context, state) {
        final double value1 =
            double.tryParse(state.pathParameters['value1'] ?? '') ?? 0.0;
        final double value2 =
            double.tryParse(state.pathParameters['value2'] ?? '') ?? 0.0;
        return SoilMoistureScreen(moistureNpk1: value1, moistureNpk2: value2);
      },
    );
  }

  static GoRoute buildSoilTempRoute() {
    return GoRoute(
      path: 'soil_temperature/:value1/:value2',
      builder: (context, state) {
        final double value1 =
            double.tryParse(state.pathParameters['value1'] ?? '') ?? 0.0;
        final double value2 =
            double.tryParse(state.pathParameters['value2'] ?? '') ?? 0.0;
        return SoilTemperatureScreen(
            npk1Temperature: value1, npk2Temperature: value2);
      },
    );
  }

  static GoRoute buildAcidityRoute() {
    return GoRoute(
      path: 'ph/:value1/:value2',
      builder: (context, state) {
        final double value1 =
            double.tryParse(state.pathParameters['value1'] ?? '') ?? 0.0;
        final double value2 =
            double.tryParse(state.pathParameters['value2'] ?? '') ?? 0.0;
        return PhScreen(phValueNpk1: value1, phValueNpk2: value2);
      },
    );
  }

  static GoRoute buildTemperatureRoute() {
    return GoRoute(
      path: 'temperature/:value',
      builder: (context, state) {
        final double value =
            double.tryParse(state.pathParameters['value'] ?? '') ?? 0.0;
        return TemperatureScreen(temperature: value);
      },
    );
  }

  static GoRoute buildLightIntensityRoute() {
    return GoRoute(
      path: 'light/:value',
      builder: (context, state) {
        final double value =
            double.tryParse(state.pathParameters['value'] ?? '') ?? 0.0;
        return LightScreen(lightIntensity: value);
      },
    );
  }

  static GoRoute buildHumidityRoute() {
    return GoRoute(
      path: 'humidity/:value',
      builder: (context, state) {
        final double value =
            double.tryParse(state.pathParameters['value'] ?? '') ?? 0.0;
        return HumidityScreen(humidity: value);
      },
    );
  }
}
