import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:prosapp_web_app/providers/auth_provider.dart';
import 'package:prosapp_web_app/providers/sidemenu_provider.dart';
import 'package:prosapp_web_app/router/router.dart';
import 'package:prosapp_web_app/ui/views/calendar_view.dart';
import 'package:prosapp_web_app/ui/views/chat_view.dart';
import 'package:prosapp_web_app/ui/views/email_view.dart';
import 'package:prosapp_web_app/ui/views/phone_view.dart';
import 'package:prosapp_web_app/ui/views/professional_calendar_view.dart';
import 'package:prosapp_web_app/ui/views/dashboard_view.dart';
import 'package:prosapp_web_app/ui/views/login_view.dart';
import 'package:prosapp_web_app/ui/views/no_page_found_view.dart';
import 'package:prosapp_web_app/ui/views/professional_profile_view.dart';
import 'package:prosapp_web_app/ui/views/professionals_view.dart';
import 'package:prosapp_web_app/ui/views/profile_view.dart';
import 'package:prosapp_web_app/ui/views/rating_view.dart';
import 'package:prosapp_web_app/ui/views/request_professional_view.dart';
import 'package:prosapp_web_app/ui/views/schedule_view.dart';
import 'package:prosapp_web_app/ui/views/service_view.dart';
import 'package:prosapp_web_app/ui/views/services_history_view.dart';
import 'package:prosapp_web_app/ui/views/services_requests_view.dart';
import 'package:prosapp_web_app/ui/views/services_view.dart';
import 'package:prosapp_web_app/ui/views/setup_name_view.dart';
import 'package:prosapp_web_app/ui/views/setup_city_view.dart';
import 'package:prosapp_web_app/ui/views/support_view.dart';
import 'package:provider/provider.dart';

class DashboardHandlers {
  static Handler dashboard = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);

      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.dashboardRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const DashboardView();
      } else {
        return const LoginView();
      }
    },
  );

  static Handler setupName = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const SetupNameView();
      } else {
        return const LoginView();
      }
    },
  );

  static Handler setupCity = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const SetupCityView();
      } else {
        return const LoginView();
      }
    },
  );

  static Handler support = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.supportRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const SupportView();
      } else {
        return const LoginView();
      }
    },
  );

  // users
  static Handler profile = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.profileRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const ProfileView();
      } else {
        return const LoginView();
      }
    },
  );
  static Handler phone = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.phoneRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const PhoneView();
      } else {
        return const LoginView();
      }
    },
  );
  static Handler email = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.emailRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const EmailView();
      } else {
        return const LoginView();
      }
    },
  );
  static Handler professionals = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.professionalsRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const ProfessionalsView();
      } else {
        return const LoginView();
      }
    },
  );

  static Handler userServices = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.userServicesRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const ServicesView(type: 'user');
      } else {
        return const LoginView();
      }
    },
  );
  static Handler professionalServices = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.professionalServicesRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const ServicesView(type: 'professional');
      } else {
        return const LoginView();
      }
    },
  );

  static Handler userService = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.userServiceRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        if (params['uid']?.first != null) {
          return ServiceView(type: 'user', serviceId: params['uid']!.first);
        } else {
          // return const NoPageFoundView();
          return Center(child: Text('No se encontró el servicio.'));
        }
      } else {
        return const LoginView();
      }
    },
  );
  static Handler professionalService = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.professionalServiceRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        if (params['uid']?.first != null) {
          return ServiceView(
              type: 'professional', serviceId: params['uid']!.first);
        } else {
          return Center(child: Text('No se encontró el servicio.'));
          // return const NoPageFoundView();
        }
      } else {
        return const LoginView();
      }
    },
  );
  static Handler userRating = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.userRatingRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        if (params['serviceId']?.first != null &&
            params['professionalId']?.first != null) {
          final serviceId = params['serviceId']!.first;
          final professionalId = params['professionalId']!.first;

          return RatingView(
              type: 'user',
              serviceId: serviceId,
              professionalId: professionalId);
        } else {
          return const NoPageFoundView();
        }
      } else {
        return const LoginView();
      }
    },
  );
  static Handler professionalRating = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.professionalRatingRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        if (params['serviceId']?.first != null &&
            params['userId']?.first != null) {
          final serviceId = params['serviceId']!.first;
          final userId = params['userId']!.first;

          return RatingView(
              type: 'professional',
              serviceId: serviceId,
              professionalId: userId);
        } else {
          return const NoPageFoundView();
        }
      } else {
        return const LoginView();
      }
    },
  );
  static Handler userChat = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.userChatRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        if (params['serviceId']?.first != null &&
            params['professionalId']?.first != null) {
          final serviceId = params['serviceId']!.first;
          final professionalId = params['professionalId']!.first;

          return ChatView(
              type: 'user',
              serviceId: serviceId,
              professionalId: professionalId);
        } else {
          return const NoPageFoundView();
        }
      } else {
        return const LoginView();
      }
    },
  );
  static Handler professionalChat = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.professionalChatRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        if (params['serviceId']?.first != null &&
            params['userId']?.first != null) {
          final serviceId = params['serviceId']!.first;
          final userId = params['userId']!.first;

          return ChatView(
              type: 'professional',
              serviceId: serviceId,
              professionalId: userId);
        } else {
          return const NoPageFoundView();
        }
      } else {
        return const LoginView();
      }
    },
  );

  static Handler userServicesHistory = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.userServicesHistoryRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const ServicesHistoryView(type: 'user');
      } else {
        return const LoginView();
      }
    },
  );
  static Handler professionalServicesRequests = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.professionalServicesRequestsRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const ServicesRequestsView();
      } else {
        return const LoginView();
      }
    },
  );
  static Handler professionalServicesHistory = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.professionalServicesHistoryRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const ServicesHistoryView(type: 'professional');
      } else {
        return const LoginView();
      }
    },
  );
  static Handler requestProfessional = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.requestProfessionalRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const RequestProfessionalView();
      } else {
        return const LoginView();
      }
    },
  );

  // static Handler servicesHistory = Handler(
  //   handlerFunc: (context, params) {
  //     final authProvider = Provider.of<AuthProvider>(context!);
  //     Provider.of<SideMenuProvider>(context, listen: false)
  //         .setCurrentPageUrl(Flurorouter.servicesRoute);

  //     if (authProvider.authStatus == AuthStatus.authenticated) {
  //       if (params['type']?.first != null &&
  //           (params['type']?.first == 'user' ||
  //               params['type']?.first == 'professional')) {
  //         return ServicesHistoryView(type: params['type']!.first);
  //       } else {
  //         return const NoPageFoundView();
  //       }
  //     } else {
  //       return const LoginView();
  //     }
  //   },
  // );

  // professionals
  static Handler professionalProfile = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.professionalProfileRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const ProfessionalProfileView();
      } else {
        return const LoginView();
      }
    },
  );
  static Handler professionalSchedule = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.professionalProfileRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const ScheduleView();
      } else {
        return const LoginView();
      }
    },
  );

  static Handler calendar = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.calendarRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        if (params['uid']?.first != null) {
          return CalendarView(professionalId: params['uid']!.first);
        } else {
          return const NoPageFoundView();
        }
      } else {
        return const LoginView();
      }
    },
  );

  static Handler professionalCalendar = Handler(
    handlerFunc: (context, params) {
      final authProvider = Provider.of<AuthProvider>(context!);
      Provider.of<SideMenuProvider>(context, listen: false)
          .setCurrentPageUrl(Flurorouter.professionalCalendarRoute);

      if (authProvider.authStatus == AuthStatus.authenticated) {
        return const ProfessionalCalendarView();
      } else {
        return const LoginView();
      }
    },
  );
}
