import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_fault_description_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/models/fault_description.dart';
import 'package:test_sa/new_views/common_widgets/single_item_drop_down_menu.dart';
import 'package:test_sa/providers/loading_list_notifier.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';

class ServiceReportFaultDescription extends StatefulWidget {
  final String? requestId; // Now nullable
  final Function(FaultDescription?)? onSelect; // Now nullable
  final FaultDescription? initialValue; // Now nullable

  const ServiceReportFaultDescription({Key? key, this.requestId, this.onSelect, this.initialValue}) : super(key: key);

  @override
  State<ServiceReportFaultDescription> createState() => _ServiceReportFaultDescriptionState();
}

class _ServiceReportFaultDescriptionState extends State<ServiceReportFaultDescription> {
  FaultDescription? initialValue;

  @override
  void initState() {
    super.initState();
    initialValue = widget.initialValue;
    if (widget.initialValue == null && widget.requestId != null) {
      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        ServiceRequestFaultDescriptionProvider menuProvider = Provider.of<ServiceRequestFaultDescriptionProvider>(context, listen: false);
        menuProvider.reset();
        menuProvider.getCallRequestForWorkOrder(widget.requestId!); // Non-null assertion since requestId is checked for null
      });
    }
  }

  @override
  void didUpdateWidget(covariant ServiceReportFaultDescription oldWidget) {
    if (widget.initialValue != oldWidget.initialValue) {
      setState(() {
        initialValue = widget.initialValue;
      });
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    final settingProvider = Provider.of<SettingProvider>(context);
    final userProvider = Provider.of<UserProvider>(context);
    final menuProvider = Provider.of<ServiceRequestFaultDescriptionProvider>(context);
    return LoadingManager(
      isLoading: menuProvider.isLoading,
      isFailedLoading: menuProvider.items == null,
      stateCode: menuProvider.stateCode,
      onRefresh: () async {
        if (widget.requestId != null) {
          // Add null check for requestId
          menuProvider.reset();
          await menuProvider.getCallRequestForWorkOrder(widget.requestId!); // Non-null assertion since requestId is checked for null
        }
      },
      child: SingleItemDropDownMenu<FaultDescription, NullableLoadingProvider>(
        context: context,
        title: context.translation.faultDescription,
        onSelect: widget.onSelect,
        // Pass onSelect directly
        initialValue: initialValue,
        staticData: menuProvider.items ?? [],
      ),
    );
  }
}
