//request tab page

import 'package:flutter/material.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/all_requests_and_count_model.dart';
import 'package:test_sa/modules/asset_delivery_module/pages/asset_delivery_item_view.dart';
import 'package:test_sa/modules/cleaner_module/widgets/one_time_task_item_view.dart';
import 'package:test_sa/modules/demo_module/demo_request_item_view.dart';
import 'package:test_sa/modules/incident_module/incident_item_view.dart';
import 'package:test_sa/modules/internal_audit_module/pages/equipment_internal_audit/equipment_internal_audit_item_view.dart';
import 'package:test_sa/modules/internal_audit_module/pages/system_internal_audit/system_internal_audit_item_view.dart';
import 'package:test_sa/modules/asset_inventory_module/pages/inventory_session_item_view.dart';
import 'package:test_sa/modules/loan_module/pages/loan_equipment_item_view.dart';
import 'package:test_sa/modules/medical_gas_inspection/medical_gas_inspection_item_view.dart';
import 'package:test_sa/modules/tm_module/tasks/task_request_item_view.dart';
import 'package:test_sa/modules/traf_module/traf_request_item_view.dart';
import 'package:test_sa/new_views/pages/land_page/requests/device_item_view.dart';
import 'package:test_sa/new_views/pages/land_page/requests/gas_refill_item_view.dart';
import 'package:test_sa/new_views/pages/land_page/requests/ppm_item_view.dart';
import 'package:test_sa/new_views/pages/land_page/requests/recurrent_wo_item_view.dart';
import 'package:test_sa/new_views/pages/land_page/requests/service_request_item_view.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';

class RequestItemViewList extends StatefulWidget {
  final List<RequestsDetails> list;
  final bool isLoading;
  final bool disableScroll;

  const RequestItemViewList(this.list, this.isLoading, {Key? key, this.disableScroll = false}) : super(key: key);

  @override
  State<RequestItemViewList> createState() => _RequestItemViewListState();
}

class _RequestItemViewListState extends State<RequestItemViewList> with SingleTickerProviderStateMixin {
  late AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 1200),
      vsync: this,
    );
    _animationController.forward();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return (widget.list.isEmpty && !widget.isLoading)
        ? NoDataFound(message: context.translation.noDataFound).center
        : ListView.separated(
            padding: const EdgeInsets.all(16),
            shrinkWrap: true,
            physics: widget.disableScroll ? const NeverScrollableScrollPhysics() : null,
            itemBuilder: (cxt, index) {
              if (widget.isLoading) return const SizedBox().toRequestShimmer(cxt, widget.isLoading);

              // Calculate staggered animation delay
              final delay = index * 0.08;
              final animation = Tween<double>(begin: 0.0, end: 1.0).animate(
                CurvedAnimation(
                  parent: _animationController,
                  curve: Interval(
                    delay.clamp(0.0, 0.8),
                    (delay + 0.3).clamp(0.0, 1.0),
                    curve: Curves.easeOut,
                  ),
                ),
              );

              final slideAnimation = Tween<Offset>(
                begin: const Offset(0.0, 0.3),
                end: Offset.zero,
              ).animate(
                CurvedAnimation(
                  parent: _animationController,
                  curve: Interval(
                    delay.clamp(0.0, 0.8),
                    (delay + 0.3).clamp(0.0, 1.0),
                    curve: Curves.easeOutCubic,
                  ),
                ),
              );

              Widget itemWidget;
              switch (widget.list[index].transactionType) {
                case 1:
                  itemWidget = ServiceRequestItemView(requestDetails: widget.list[index]);
                  break;
                case 2:
                  itemWidget = GasRefillItemView(requestDetails: widget.list[index]);
                  break;
                case 3:
                  itemWidget = DeviceItemView(requestDetails: widget.list[index]);
                  break;
                case 4:
                  itemWidget = PpmItemView(requestDetails: widget.list[index]);
                  break;
                case 5:
                  itemWidget = RecurrentWoItemView(requestDetails: widget.list[index]);
                  break;
                case 6:
                  itemWidget = TaskRequestItemView(requestDetails: widget.list[index]);
                  break;
                case 7:
                  itemWidget = TaskRequestItemView(requestDetails: widget.list[index]);
                  break;
                case 8:
                  itemWidget = InventorySessionItemView(requestDetails: widget.list[index]);
                  break;
                case 9:
                  itemWidget = TrafRequestItemView(requestDetails: widget.list[index]);
                  break;
                case 10:
                  itemWidget = EquipmentInternalAuditItemView(requestDetails: widget.list[index]);
                  break;
                case 11:
                  itemWidget = SystemInternalAuditItemView(requestDetails: widget.list[index]);
                  break;
                case 12:
                  itemWidget = LoanEquipmentItemView(requestDetails: widget.list[index]);
                  break;
                case 13:
                  itemWidget = AssetDeliveryItemView(requestDetails: widget.list[index]);
                  break;
                case 14:
                  itemWidget = MedicalGasInspectionItemView(requestDetails: widget.list[index]);
                  break;
                case 15:
                  itemWidget = DemoRequestItemView(requestDetails: widget.list[index]);
                  break;
                case 16:
                  itemWidget = IncidentItemView(requestDetails: widget.list[index]);
                  break;
                case 17:
                  itemWidget = OneTimeTaskItemView(requestDetails: widget.list[index]);
                  break;
                default:
                  itemWidget = Container(
                    height: 100,
                    width: double.infinity,
                    color: Colors.grey,
                  );
              }

              return FadeTransition(
                opacity: animation,
                child: SlideTransition(
                  position: slideAnimation,
                  child: itemWidget,
                ),
              );
            },
            separatorBuilder: (cxt, index) => 12.height,
            itemCount: widget.isLoading ? 6 : widget.list.length);
  }
}
