// import 'dart:developer';
//
// import 'package:flutter/material.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/views/common_widgets/app_general_widgets.dart';
// import 'package:hmg_qline/views/main_queue_screen/components/ticket_item.dart';
// import 'package:hmg_qline/views/main_queue_screen/components/ticket_item_calling_card.dart';
// import 'package:hmg_qline/views/main_queue_screen/components/ticket_item_normal_card.dart';
// import 'package:hmg_qline/views/view_helpers/size_config.dart';
// import 'package:zo_animated_border/widget/zo_color_change_border.dart';
//
// class PriorityTickets extends StatelessWidget {
//   final List<TicketDetailsModel> tickets;
//   final GlobalConfigurationsModel globalConfigurationsModel;
//
//   const PriorityTickets({required this.tickets, required this.globalConfigurationsModel, super.key});
//
//   @override
//   Widget build(BuildContext context) {
//     final TicketData firstTicket = tickets[0].ticketModel!;
//     TicketData? secondTicket;
//     List<TicketDetailsModel> otherTickets = [];
//     if (tickets.length > 1) {
//       secondTicket = tickets[1].ticketModel!;
//     }
//
//     if (tickets.length > 3) {
//       otherTickets = tickets.sublist(2, tickets.length);
//     }
//
//     log("tickets: ${tickets.length}");
//
//     return Directionality(
//       textDirection: globalConfigurationsModel.textDirection,
//       child: Column(
//         mainAxisAlignment: MainAxisAlignment.center,
//         crossAxisAlignment: CrossAxisAlignment.center,
//         children: [
//           if (tickets.length == 1) ...[
//             QueueItemCallingCard(
//               isGradientRequired: true,
//               isBorderRequired: true,
//               ticketNo: firstTicket.queueNo ?? '',
//               scale: globalConfigurationsModel.screenTypeEnum == ScreenTypeEnum.roomLevelScreen ? 2 : 1.2,
//               blink: true,
//               roomNo: firstTicket.roomNo ?? '',
//               roomText: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextEng : globalConfigurationsModel.counterTextEng) ?? "",
//               roomTextAr: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextArb : globalConfigurationsModel.counterTextArb) ?? "",
//               isClinicAdded: false,
//               callTypeEnum: firstTicket.callTypeEnum,
//               textDirection: globalConfigurationsModel.textDirection,
//               screenTypeEnum: globalConfigurationsModel.screenTypeEnum,
//               langTypeEnum: globalConfigurationsModel.screenLanguageEnum,
//               globalConfigurationsModel: globalConfigurationsModel,
//             ),
//           ],
//           if (tickets.length == 2) ...[
//             Row(
//               children: [
//                 Expanded(
//                   flex: 2,
//                   child: QueueItemCallingCard(
//                     isGradientRequired: true,
//                     isBorderRequired: true,
//                     ticketNo: firstTicket.queueNo ?? '',
//                     scale: globalConfigurationsModel.screenTypeEnum == ScreenTypeEnum.roomLevelScreen ? 2 : 1.2,
//                     blink: true,
//                     roomNo: firstTicket.roomNo ?? '',
//                     roomText: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextEng : globalConfigurationsModel.counterTextEng) ?? "",
//                     roomTextAr: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextArb : globalConfigurationsModel.counterTextArb) ?? "",
//                     isClinicAdded: false,
//                     callTypeEnum: firstTicket.callTypeEnum,
//                     textDirection: globalConfigurationsModel.textDirection,
//                     screenTypeEnum: globalConfigurationsModel.screenTypeEnum,
//                     langTypeEnum: globalConfigurationsModel.screenLanguageEnum,
//                     globalConfigurationsModel: globalConfigurationsModel,
//                   ),
//                 ),
//                 SizedBox(width: SizeConfig.getWidthMultiplier() * 2),
//                 Expanded(
//                   flex: 1,
//                   child: QueueItemNormalCard(
//                     ticketNo: secondTicket!.queueNo ?? '',
//                     roomNo: secondTicket.roomNo ?? '',
//                     roomText: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextEng : globalConfigurationsModel.counterTextEng) ?? "",
//                     roomTextAr: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextArb : globalConfigurationsModel.counterTextArb) ?? "",
//                     isClinicAdded: false,
//                     callTypeEnum: secondTicket.callTypeEnum,
//                     textDirection: globalConfigurationsModel.textDirection,
//                     screenTypeEnum: globalConfigurationsModel.screenTypeEnum,
//                     langTypeEnum: globalConfigurationsModel.screenLanguageEnum,
//                     globalConfigurationsModel: globalConfigurationsModel,
//                   ),
//                 ),
//               ],
//             ),
//           ],
//           if (tickets.length > 2) ...[
//             Column(
//               children: [
//                 Row(
//                   children: [
//                     Expanded(
//                       flex: 2,
//                       child: QueueItemCallingCard(
//                         isGradientRequired: true,
//                         isBorderRequired: true,
//                         ticketNo: firstTicket.queueNo ?? '',
//                         scale: globalConfigurationsModel.screenTypeEnum == ScreenTypeEnum.roomLevelScreen ? 2 : 1.2,
//                         blink: true,
//                         roomNo: firstTicket.roomNo ?? '',
//                         roomText: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextEng : globalConfigurationsModel.counterTextEng) ?? "",
//                         roomTextAr: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextArb : globalConfigurationsModel.counterTextArb) ?? "",
//                         isClinicAdded: false,
//                         callTypeEnum: firstTicket.callTypeEnum,
//                         textDirection: globalConfigurationsModel.textDirection,
//                         screenTypeEnum: globalConfigurationsModel.screenTypeEnum,
//                         langTypeEnum: globalConfigurationsModel.screenLanguageEnum,
//                         globalConfigurationsModel: globalConfigurationsModel,
//                       ),
//                     ),
//                     SizedBox(width: SizeConfig.getWidthMultiplier() * 2),
//                     Expanded(
//                       flex: 1,
//                       child: QueueItemNormalCard(
//                         ticketNo: secondTicket!.queueNo ?? '',
//                         roomNo: secondTicket.roomNo ?? '',
//                         roomText: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextEng : globalConfigurationsModel.counterTextEng) ?? "",
//                         roomTextAr: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextArb : globalConfigurationsModel.counterTextArb) ?? "",
//                         isClinicAdded: false,
//                         callTypeEnum: secondTicket.callTypeEnum,
//                         textDirection: globalConfigurationsModel.textDirection,
//                         screenTypeEnum: globalConfigurationsModel.screenTypeEnum,
//                         langTypeEnum: globalConfigurationsModel.screenLanguageEnum,
//                         globalConfigurationsModel: globalConfigurationsModel,
//                       ),
//                     ),
//                   ],
//                 ),
//                 Padding(
//                   padding: EdgeInsets.symmetric(vertical: SizeConfig.getHeightMultiplier() * 0.1),
//                   child: engArabicTextWithSeparatorWidget(
//                     englishText: globalConfigurationsModel.currentServeTextEng ?? "",
//                     arabicText: globalConfigurationsModel.currentServeTextArb ?? "",
//                   ),
//                 ),
//                 Row(
//                   children: List.generate(
//                     otherTickets.length,
//                     (index) => Expanded(
//                       child: Padding(
//                         padding: EdgeInsets.only(right: index != otherTickets.length - 1 ? SizeConfig.getWidthMultiplier() * 2 : 0),
//                         child: QueueItemNormalCard(
//                           ticketNo: otherTickets[index].ticketModel!.queueNo ?? '',
//                           roomNo: otherTickets[index].ticketModel!.roomNo ?? '',
//                           roomText: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextEng : globalConfigurationsModel.counterTextEng) ?? "",
//                           roomTextAr: (globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextArb : globalConfigurationsModel.counterTextArb) ?? "",
//                           isClinicAdded: false,
//                           callTypeEnum: otherTickets[index].ticketModel!.callTypeEnum,
//                           textDirection: globalConfigurationsModel.textDirection,
//                           screenTypeEnum: globalConfigurationsModel.screenTypeEnum,
//                           langTypeEnum: globalConfigurationsModel.screenLanguageEnum,
//                           globalConfigurationsModel: globalConfigurationsModel,
//                         ),
//                       ),
//                     ),
//                   ),
//                 ),
//               ],
//             ),
//           ],
//         ],
//       ),
//     );
//   }
// }

import 'dart:developer';

import 'package:flutter/material.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/views/common_widgets/app_general_widgets.dart';
import 'package:hmg_qline/views/main_queue_screen/components/ticket_item_calling_card.dart';
import 'package:hmg_qline/views/main_queue_screen/components/ticket_item_normal_card.dart';
import 'package:hmg_qline/views/view_helpers/size_config.dart';

class PriorityTickets extends StatelessWidget {
  final List<TicketDetailsModel> tickets;
  final GlobalConfigurationsModel globalConfigurationsModel;

  const PriorityTickets({
    required this.tickets,
    required this.globalConfigurationsModel,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    log("tickets: ${tickets.length}");
    return Directionality(
      textDirection: globalConfigurationsModel.textDirection,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: _buildTicketLayout(context),
      ),
    );
  }

  Widget _buildAnimatedTicket({required Widget child, required int delay, String? key}) {
    return TweenAnimationBuilder<double>(
      key: key != null ? ValueKey(key) : null,
      duration: Duration(milliseconds: 400 + delay),
      tween: Tween(begin: 0.0, end: 1.0),
      curve: Curves.easeOutCubic,
      // Changed from elasticOut to prevent overshoot
      builder: (context, value, child) {
        final clampedValue = value.clamp(0.0, 1.0);

        return Transform.scale(
          scale: 0.8 + (clampedValue * 0.2), // Scale from 0.8 to 1.0 instead of 0 to 1
          child: Opacity(
            opacity: clampedValue,
            child: child,
          ),
        );
      },
      child: child,
    );
  }

  Widget _buildAnimatedOtherTicketsRow() {
    final otherTickets = tickets.length > 2 ? tickets.sublist(2) : <TicketDetailsModel>[];
    if (otherTickets.isEmpty) return const SizedBox.shrink();

    return Row(
      children: List.generate(
        otherTickets.length,
        (index) => Expanded(
          child: _buildAnimatedTicket(
            key: 'other_ticket_${otherTickets[index].ticketModel?.queueNo ?? index}',
            child: Padding(
              padding: EdgeInsets.only(
                right: (index != otherTickets.length - 1 && globalConfigurationsModel.textDirection == TextDirection.ltr) ? SizeConfig.getWidthMultiplier() * 2 : 0,
                left: (index != otherTickets.length - 1 && globalConfigurationsModel.textDirection == TextDirection.rtl) ? SizeConfig.getWidthMultiplier() * 2 : 0,
              ),
              child: _buildSecondaryTicket(otherTickets[index]),
            ),
            delay: 200 + (index * 100),
          ),
        ),
      ),
    );
  }

  List<Widget> _buildTicketLayout(BuildContext context) {
    if (tickets.isEmpty) return [];
    log("tickets.length: ${tickets.length}");
    switch (tickets.length) {
      case 1:
        return [
          _buildAnimatedTicket(
            child: _buildPrimaryTicket(context, tickets[0], isFullWidth: true),
            delay: 0,
          ),
        ];

      case 2:
        return [
          Row(
            children: [
              Expanded(
                flex: 3,
                child: _buildAnimatedTicket(
                  child: _buildPrimaryTicket(context, tickets[0]),
                  delay: 0,
                ),
              ),
              _buildSpacing(),
              Expanded(
                flex: 2,
                child: _buildAnimatedTicket(
                  child: _buildSecondaryTicket(tickets[1]),
                  delay: 150,
                ),
              ),
            ],
          ),
        ];

      default:
        return [
          Row(
            children: [
              Expanded(
                flex: 2,
                child: _buildAnimatedTicket(
                  child: _buildPrimaryTicket(context, tickets[0]),
                  delay: 0,
                ),
              ),
              _buildSpacing(),
              Expanded(
                flex: 1,
                child: _buildAnimatedTicket(
                  child: _buildSecondaryTicket(tickets[1]),
                  delay: 100,
                ),
              ),
            ],
          ),
          _buildCurrentServeText(),
          _buildAnimatedOtherTicketsRow(),
        ];
    }
  }

  Widget _buildPrimaryTicket(BuildContext context, TicketDetailsModel ticket, {bool isFullWidth = false}) {
    Widget primaryCallingCard = QueueItemCallingCard(
      isGradientRequired: true,
      isBorderRequired: true,
      isSingleTicket: isFullWidth,
      ticketNo: ticket.ticketModel?.queueNo ?? '',
      scale: _getTicketScale(),
      blink: true,
      roomNo: ticket.ticketModel?.roomNo ?? '',
      roomText: _getRoomText(),
      roomTextAr: _getRoomTextAr(),
      isClinicAdded: false,
      callTypeEnum: ticket.ticketModel?.callTypeEnum ?? CallTypeEnum.vitalSign,
      textDirection: globalConfigurationsModel.textDirection,
      screenTypeEnum: globalConfigurationsModel.screenTypeEnum,
      langTypeEnum: globalConfigurationsModel.screenLanguageEnum,
      globalConfigurationsModel: globalConfigurationsModel,
    );

    if (isFullWidth) {
      return Center(
        child: ConstrainedBox(
          constraints: BoxConstraints(
            maxWidth: MediaQuery.of(context).size.width * 0.6, // 60% of screen width
            minHeight: SizeConfig.getHeightMultiplier() * 2.5, // Minimum height
          ),
          child: Transform.scale(
            scale: _getTicketScale() + 0.2,
            child: primaryCallingCard,
          ),
        ),
      );
    }

    return primaryCallingCard;
  }

  Widget _buildSecondaryTicket(TicketDetailsModel ticket) {
    return QueueItemNormalCard(
      ticketNo: ticket.ticketModel?.queueNo ?? '',
      roomNo: ticket.ticketModel?.roomNo ?? '',
      roomText: _getRoomText(),
      roomTextAr: _getRoomTextAr(),
      isClinicAdded: false,
      callTypeEnum: ticket.ticketModel?.callTypeEnum ?? CallTypeEnum.vitalSign,
      textDirection: globalConfigurationsModel.textDirection,
      screenTypeEnum: globalConfigurationsModel.screenTypeEnum,
      langTypeEnum: globalConfigurationsModel.screenLanguageEnum,
      globalConfigurationsModel: globalConfigurationsModel,
    );
  }

  Widget _buildOtherTicketsRow(TextDirection textDirection) {
    final otherTickets = tickets.length > 2 ? tickets.sublist(2) : <TicketDetailsModel>[];
    if (otherTickets.isEmpty) return const SizedBox.shrink();

    return Row(
      children: List.generate(
        otherTickets.length,
        (index) => Expanded(
          child: Padding(
            padding: EdgeInsets.only(
              right: (index != otherTickets.length - 1 && textDirection == TextDirection.ltr) ? SizeConfig.getWidthMultiplier() * 2 : 0,
              left: (index != otherTickets.length - 1 && textDirection == TextDirection.rtl) ? SizeConfig.getWidthMultiplier() * 2 : 0,
            ),
            child: _buildSecondaryTicket(otherTickets[index]),
          ),
        ),
      ),
    );
  }

  Widget _buildCurrentServeText() {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: SizeConfig.getHeightMultiplier() * 0.13),
      child: engArabicTextWithSeparatorWidget(
        englishText: globalConfigurationsModel.currentServeTextEng ?? "",
        arabicText: globalConfigurationsModel.currentServeTextArb ?? "",
      ),
    );
  }

  Widget _buildSpacing() {
    return SizedBox(width: SizeConfig.getWidthMultiplier() * 2);
  }

  // Helper methods to reduce repetition
  double _getTicketScale() {
    return globalConfigurationsModel.screenTypeEnum == ScreenTypeEnum.roomLevelScreen ? 2.0 : 1.2;
  }

  String _getRoomText() {
    return globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextEng ?? "" : globalConfigurationsModel.counterTextEng ?? "";
  }

  String _getRoomTextAr() {
    return globalConfigurationsModel.qTypeEnum == QTypeEnum.appointment ? globalConfigurationsModel.roomTextArb ?? "" : globalConfigurationsModel.counterTextArb ?? "";
  }
}
