import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hmg_qline/constants/app_constants.dart';
import 'package:hmg_qline/models/global_config_model.dart';
import 'package:hmg_qline/models/ticket_model.dart';
import 'package:hmg_qline/utilities/enums.dart';
import 'package:hmg_qline/utilities/extensions.dart';
import 'package:hmg_qline/views/common_widgets/app_general_widgets.dart';
import 'package:hmg_qline/views/common_widgets/app_texts_widget.dart';
import 'package:hmg_qline/views/main_queue_screen/components/priority_tickets.dart';
import 'package:hmg_qline/views/view_helpers/size_config.dart';

class PriorityTicketsWithSideSection extends StatelessWidget {
  final List<TicketDetailsModel> tickets;
  final GlobalConfigurationsModel globalConfigurationsModel;
  final ScreenOrientationEnum screenOrientationEnum;
  final bool showThankyouMessage;

  const PriorityTicketsWithSideSection({
    super.key,
    required this.tickets,
    required this.globalConfigurationsModel,
    required this.screenOrientationEnum,
    required this.showThankyouMessage,
  });

  Widget ticketListItem({required TicketData? ticketModel}) {
    final callMessageEng = (ticketModel?.callTypeEnum.getMessageByCallTypeForEnglish(globalConfigurationsModel, isListView: true)) ?? "";
    final callMessageAr = (ticketModel?.callTypeEnum.getMessageByCallTypeForArabic(globalConfigurationsModel, isListView: true)) ?? "";

    return Directionality(
      textDirection: globalConfigurationsModel.textDirection,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            flex: 3,
            child: Center(
              child: AppText(
                ticketModel!.queueNo ?? "",
                fontWeight: FontWeight.w500,
                color: AppColors.greyTextColor,
                fontSize: SizeConfig.getWidthMultiplier() * 1.8,
                textAlign: TextAlign.center,
                fontFamily: AppStrings.fontNamePoppins,
              ),
            ),
          ),
          // if (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment) ...[
          Expanded(
            flex: 8,
            child: SizedBox(
              height: SizeConfig.getHeightMultiplier() * 0.25,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    flex: 2,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        ticketModel.callTypeEnum.getIconByCallType(SizeConfig.getHeightMultiplier() * 0.2, globalConfigurationsModel.qTypeEnum),
                      ],
                    ),
                  ),
                  Expanded(
                    flex: 9,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Expanded(
                          flex: 3,
                          child: AppText(
                            callMessageAr,
                            color: ticketModel.callTypeEnum.getColorByCallType(),
                            fontSize: SizeConfig.getWidthMultiplier() * 1.8,
                            fontWeight: FontWeight.bold,
                            fontFamily: AppStrings.fontNameGesTwo,
                            fontHeight: 1,
                            textOverflow: TextOverflow.clip,
                            maxLines: 1,
                          ),
                        ),
                        SizedBox(width: SizeConfig.getWidthMultiplier()),
                        if (callMessageEng.isNotEmpty) ...[
                          Expanded(
                            flex: 2,
                            child: AppText(
                              "($callMessageEng)",
                              color: ticketModel.callTypeEnum.getColorByCallType(),
                              fontSize: SizeConfig.getWidthMultiplier() * 1.8,
                              fontFamily: AppStrings.fontNamePoppins,
                              fontHeight: 1,
                            ),
                          ),
                        ]
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          // ],
          Expanded(
            flex: 2,
            child: Center(
              child: AppText(
                ticketModel.roomNo ?? "",
                fontWeight: FontWeight.w500,
                color: AppColors.greyTextColor,
                fontSize: SizeConfig.getWidthMultiplier() * 1.8,
                fontFamily: globalConfigurationsModel.screenLanguageEnum == LanguageEnum.arabic ? AppStrings.fontNameCairo : AppStrings.fontNamePoppins,
                maxLines: 1,
                textOverflow: TextOverflow.clip,
                textDirection: globalConfigurationsModel.textDirection,
              ),
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    List<TicketDetailsModel> priorityTickets = [];
    List<TicketDetailsModel> otherTickets = [];

    log("tickets : ${tickets.length}");
    log("priorityTickets : ${priorityTickets.length}");
    log("otherTickets : ${otherTickets.length}");

    if (tickets.length > AppConstants.thresholdForListUI) {
      priorityTickets = tickets.sublist(0, AppConstants.thresholdForListUI);
      otherTickets = tickets.sublist(AppConstants.thresholdForListUI, tickets.length);
    } else {
      priorityTickets = tickets;
    }
    final List<Widget> children = [
      Expanded(flex: 5, child: PriorityTickets(globalConfigurationsModel: globalConfigurationsModel, tickets: priorityTickets)),
      // if (screenOrientationEnum == ScreenOrientationEnum.portraitUp || screenOrientationEnum == ScreenOrientationEnum.portraitDown) ...[
      //   SizedBox(width: SizeConfig.getWidthMultiplier() * 2),
      // ],
      // if (screenOrientationEnum == ScreenOrientationEnum.landscapeLeft || screenOrientationEnum == ScreenOrientationEnum.landscapeRight) ...[
      //   SizedBox(height: SizeConfig.getHeightMultiplier() * 0.2),
      // ],
      if (showThankyouMessage) ...[
        showThankyouWidget(),
      ] else ...[
        Expanded(
          flex: !globalConfigurationsModel.isRssFeedReq && (screenOrientationEnum == ScreenOrientationEnum.landscapeLeft || screenOrientationEnum == ScreenOrientationEnum.landscapeRight) ? 6 : 4,
          // flex: !globalConfigurationsModel.isRssFeedReq && (screenOrientationEnum == ScreenOrientationEnum.landscapeLeft || screenOrientationEnum == ScreenOrientationEnum.landscapeRight) ? 6 : 4,
          child: customShadowSmoothContainer(
            child: Column(
              children: [
                Directionality(
                  textDirection: globalConfigurationsModel.textDirection,
                  child: Padding(
                    padding: EdgeInsets.symmetric(
                      vertical: SizeConfig.getHeightMultiplier()! * 0.12,
                      horizontal: SizeConfig.getWidthMultiplier()! * 2,
                    ),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          flex: 3,
                          child: engArabicTextWithSeparatorWidget(
                            englishText: globalConfigurationsModel.queueNoTextEng ?? "",
                            arabicText: globalConfigurationsModel.queueNoTextArb ?? "",
                            fontSize: SizeConfig.getWidthMultiplier()! * 2.2,
                            mainAxisAlignment: MainAxisAlignment.start,
                          ),
                        ),
                        if (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment) ...[
                          Expanded(
                            flex: 5,
                            child: engArabicTextWithSeparatorWidget(
                              mainAxisAlignment: MainAxisAlignment.center,
                              englishText: globalConfigurationsModel.callForTextEng ?? "",
                              arabicText: globalConfigurationsModel.callForTextArb ?? "",
                              fontSize: SizeConfig.getWidthMultiplier()! * 2.2,
                            ),
                          ),
                        ],
                        Expanded(
                          flex: 3,
                          child: Container(
                            child: engArabicTextWithSeparatorWidget(
                              mainAxisAlignment: MainAxisAlignment.end,
                              englishText: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextEng : globalConfigurationsModel.counterTextEng) ?? "",
                              arabicText: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextArb : globalConfigurationsModel.counterTextArb) ?? "",
                              fontSize: SizeConfig.getWidthMultiplier()! * 2.2,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                SizedBox(height: SizeConfig.getHeightMultiplier()! * 0.06),
                Expanded(
                  child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: SizeConfig.getWidthMultiplier() * 3),
                    child: ListView.separated(
                      separatorBuilder: (context, int index) => Padding(
                        padding: EdgeInsets.only(right: SizeConfig.getWidthMultiplier() * 2),
                        child: Divider(
                          color: AppColors.lightBorderColor,
                          thickness: SizeConfig.getHeightMultiplier()! * 0.01,
                          height: SizeConfig.getHeightMultiplier()! * 0.14,
                        ),
                      ),
                      itemCount: otherTickets.length,
                      itemBuilder: (ctx, index) {
                        final ticketModel = otherTickets[index].ticketModel;
                        return ticketListItem(ticketModel: ticketModel);
                      },
                    ),
                  ),
                ),
              ],
            ),
          ),
        )
      ],
    ];

    return (screenOrientationEnum == ScreenOrientationEnum.portraitUp || screenOrientationEnum == ScreenOrientationEnum.portraitDown)
        ? Directionality(
            textDirection: globalConfigurationsModel.textDirection,
            child: Row(children: children),
          )
        : Directionality(
            textDirection: globalConfigurationsModel.textDirection,
            child: Column(children: children),
          );
  }
}
