import 'package:flutter/material.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/new_models/dashboard_detail.dart';
import 'package:test_sa/modules/asset_delivery_module/pages/asset_delivery_item_view.dart';
import 'package:test_sa/modules/asset_inventory_module/pages/inventory_session_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/internal_audit_module/pages/equipment_internal_audit/equipment_internal_audit_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/app_style/app_color.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';

class RequestCategoryList extends StatefulWidget {
  final List<Data> list;
  final int totalCount;
  final bool isLoading;

  const RequestCategoryList(this.list, this.isLoading, this.totalCount, {Key? key}) : super(key: key);

  @override
  State<RequestCategoryList> createState() => _RequestCategoryListState();
}

class _RequestCategoryListState extends State<RequestCategoryList> 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 ListView.separated(
      itemCount: widget.list.length < widget.totalCount ? widget.list.length + 1 : widget.list.length,
      shrinkWrap: true,
      padding: const EdgeInsets.all(16),
      physics: const NeverScrollableScrollPhysics(),
      separatorBuilder: (cxt, index) => 12.height,
      itemBuilder: (context, index) {
        if (index == widget.list.length) {
          return CircularProgressIndicator(color: AppColor.loadingColor(context), strokeWidth: 1).center;
        }

        // 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.7),
              (delay + 0.4).clamp(0.0, 1.0),
              curve: Curves.easeOut,
            ),
          ),
        );

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

        return FadeTransition(
          opacity: animation,
          child: SlideTransition(
            position: slideAnimation,
            child: _buildRequestItem(widget.list[index]),
          ),
        );
      },
    );
  }

  Widget _buildRequestItem(Data request) {
    // log('here with no ${request.transactionNo}');
    switch (request.transactionNo) {
      case 1:
        return ServiceRequestItemView(requestData: request);
      case 2:
        return GasRefillItemView(requestData: request);
      case 3:
        return DeviceItemView(requestData: request);
      case 4:
        return PpmItemView(requestData: request);
      case 5:
        return RecurrentWoItemView(requestData: request);
      case 6:
        return TaskRequestItemView(requestData: request);
      case 7:
        return TaskRequestItemView(requestData: request);
      case 8:
        return InventorySessionItemView(requestData: request);
      case 9:
        return TrafRequestItemView(requestData: request);
      case 10:
        return EquipmentInternalAuditItemView(requestData: request);
      case 11:
        return EquipmentInternalAuditItemView(requestData: request);
      case 12:
        return LoanEquipmentItemView(requestData: request);
      case 13:
        return AssetDeliveryItemView(requestData: request);
      case 14:
        return MedicalGasInspectionItemView(requestData: request);
      case 15:
        return DemoRequestItemView(requestData: request);
        case 17:
        return OneTimeTaskItemView(requestData: request);
      default:
        return Container(
          height: 100,
          width: double.infinity,
          color: Colors.grey,
        );
    }
  }
}
