import 'package:flutter/material.dart';
import 'package:hmg_qline/constants/app_constants.dart';
import 'package:hmg_qline/utilities/enums.dart';
import 'package:hmg_qline/utilities/extensions.dart';
import 'package:hmg_qline/view_models/queuing_view_model.dart';
import 'package:hmg_qline/view_models/screen_config_view_model.dart';
import 'package:hmg_qline/views/common_widgets/app_footer.dart';
import 'package:hmg_qline/views/common_widgets/app_general_widgets.dart';
import 'package:hmg_qline/views/common_widgets/app_header.dart';
import 'package:hmg_qline/views/common_widgets/app_scaffold.dart';
import 'package:hmg_qline/views/main_queue_screen/components/priority_tickets.dart';
import 'package:hmg_qline/views/main_queue_screen/components/priority_tickets_sidelist.dart';
import 'package:provider/provider.dart';

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

  Widget dataContent({required BuildContext context}) {
    return Consumer2(
      builder: (BuildContext context, ScreenConfigViewModel screenConfigViewModel, QueuingViewModel queuingViewModel, Widget? child) {
        //
        // // TODO:  FOR TEST
        // queuingViewModel.textToSpeechService.speechText(
        //   globalConfigurationsModel: screenConfigViewModel.globalConfigurationsModel,
        //   ticket: queuingViewModel.currentTickets.first,
        // );
        // screenConfigViewModel.getNextPrayerToShow();



        Widget widget = const SizedBox();
        String text = AppStrings.awaitingArrivalEng;
        String fontFamily = AppStrings.fontNamePoppins;

        if (screenConfigViewModel.currentQTypeEnum == QTypeEnum.general) {
          text = AppStrings.awaitingQueueNumberEng;
        }
        if (screenConfigViewModel.globalConfigurationsModel.screenLanguageEnum == LanguageEnum.arabic) {
          if (screenConfigViewModel.currentQTypeEnum == QTypeEnum.general) {
            text = AppStrings.awaitingQueueNumberAr;
          }
          text = AppStrings.awaitingArrivalAr;
          fontFamily = AppStrings.fontNameCairo;
        }
        if (queuingViewModel.currentTickets.isEmpty) {
          bool isForRoomLevel = screenConfigViewModel.globalConfigurationsModel.screenTypeEnum == ScreenTypeEnum.roomLevelScreen;
          widget = noPatientInQueue(
            text: text,
            fontName: fontFamily,
            isForRoomLevel: isForRoomLevel,
            isRoomNoRequired: screenConfigViewModel.globalConfigurationsModel.isRoomNoRequired ?? true,
            counterNo: screenConfigViewModel.globalConfigurationsModel.roomNo ?? 0,
            roomText: screenConfigViewModel.globalConfigurationsModel.roomText ?? AppStrings.counterNo,
          );
        } else if (screenConfigViewModel.globalConfigurationsModel.screenTypeEnum == ScreenTypeEnum.roomLevelScreen) {
          widget = Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  counterNoText(
                    counterNo: screenConfigViewModel.globalConfigurationsModel.roomNo ?? 0,
                    roomText: screenConfigViewModel.globalConfigurationsModel.roomText ?? AppStrings.counterNo,
                    isRoomNoRequired: screenConfigViewModel.globalConfigurationsModel.isRoomNoRequired ?? true,
                  ),
                  PriorityTickets(
                    tickets: [queuingViewModel.currentTickets.first],
                    globalConfigurationsModel: screenConfigViewModel.globalConfigurationsModel,
                  ),
                  const SizedBox.shrink()
                ],
              ),
            ],
          );
        } else if (queuingViewModel.currentTickets.length > AppConstants.thresholdForListUI) {
          widget = PriorityTicketsWithSidelist(
            tickets: queuingViewModel.currentTickets,
            globalConfigurationsModel: screenConfigViewModel.globalConfigurationsModel,
            screenOrientationEnum: screenConfigViewModel.globalConfigurationsModel.orientationTypeEnum,
          );
        } else {
          widget = PriorityTickets(
            tickets: queuingViewModel.currentTickets,
            globalConfigurationsModel: screenConfigViewModel.globalConfigurationsModel,
          );
        }

        return widget;
      },
    );
  }

  Widget getBody({required BuildContext context}) {
    return Consumer<ScreenConfigViewModel>(
      builder: (BuildContext context, ScreenConfigViewModel screenConfigVM, Widget? child) {
        return Column(
          children: [
            if (screenConfigVM.globalConfigurationsModel.screenTypeEnum != ScreenTypeEnum.roomLevelScreen) ...[
              Expanded(
                flex: getFlexForScreenTypes(screenConfigVM),
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Row(
                    children: [
                      if (screenConfigVM.globalConfigurationsModel.isWeatherReq) ...[
                        getWeatherWidget(screenConfigVM),
                      ],
                      const SizedBox(width: 20),
                      if (screenConfigVM.globalConfigurationsModel.isPrayerTimeReq) ...[
                        getPrayerWidget(screenConfigVM),
                      ],
                    ],
                  ),
                ),
              )
            ],
            const SizedBox(height: 12),
            Expanded(flex: 10, child: dataContent(context: context)),
            if (!screenConfigVM.globalConfigurationsModel.isWeatherReq && !screenConfigVM.globalConfigurationsModel.isPrayerTimeReq) ...[
              const SizedBox(height: 100),
            ],
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Selector<ScreenConfigViewModel, ScreenOrientationEnum>(
      selector: (context, screenConfigViewModel) => screenConfigViewModel.globalConfigurationsModel.orientationTypeEnum,
      builder: (BuildContext context, ScreenOrientationEnum screenOrientationEnum, Widget? child) {
        //TODO: For Testing Only
        // context.read<ScreenConfigViewModel>().createAutoTickets(numOfTicketsToCreate: 20);
        // context.read<QueuingViewModel>().testSpeech();
        // context.read<QueuingViewModel>().voiceCallTicket(ticketData: context.read<QueuingViewModel>().currentTickets.first.ticketModel);
        return RotatedBox(
          quarterTurns: screenOrientationEnum.getTurnsByOrientation(),
          child: AppScaffold(
            appBar: const AppHeader(),
            body: getBody(context: context),
            bottomNavigationBar: const AppFooter(),
          ),
        );
      },
    );
  }
}
