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/models/lookup.dart';

import '../../../new_views/app_style/app_color.dart';

class SingleStatusMenu extends StatefulWidget {
  final List<Lookup>? statuses;// Nullable list
  final Lookup? initialStatus; // Nullable
  final Function(Lookup?)? onSelect; // Nullable function, accepts nullable Lookup
  final bool enabled;
  final String? title; // Nullable

  const SingleStatusMenu({
    Key? key,
    this.enabled = true,
    this.title,
    this.statuses,
    this.onSelect,
    this.initialStatus,
  }) : super(key: key);

  @override
  _SingleStatusMenuState createState() => _SingleStatusMenuState();
}

class _SingleStatusMenuState extends State<SingleStatusMenu> {
  Lookup? _selectedStatus;

  @override
  void setState(VoidCallback fn) {
    if (mounted) super.setState(fn);
  }

  @override
  void didUpdateWidget(covariant SingleStatusMenu oldWidget) {
    _selectedStatus = widget.statuses?.firstWhere(
            (element) => element == widget.initialStatus,
        orElse:  null);

    if (widget.initialStatus != _selectedStatus) {
      widget.onSelect?.call(_selectedStatus); // Use null-aware operator
    }

    super.didUpdateWidget(oldWidget);
  }

  @override
  void initState() {
    print('status lenght is ${widget.statuses?.length}');
    _selectedStatus = widget.statuses?.firstWhere(
            (element) => element == widget.initialStatus,
        orElse: () => Lookup());

    if (widget.initialStatus != _selectedStatus) {
      widget.onSelect?.call(_selectedStatus); // Use null-aware operator
    }

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(height: 60.toScreenHeight,
      padding: EdgeInsets.symmetric(horizontal: 16.toScreenWidth),
      decoration: BoxDecoration(
        color: context.isDark && (!widget.enabled || widget.statuses!.isEmpty)
            ? AppColor.neutral50
            : (!widget.enabled || widget.statuses!.isEmpty)
            ? AppColor.neutral40
            : AppColor.background(context),
        borderRadius: BorderRadius.circular(10),
        boxShadow: [
          BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 10)
        ],
      ),
      child: Stack(
        alignment: Alignment.center,
        children: [
          if (widget.enabled)
            const PositionedDirectional(
                end: 0, child: Icon(Icons.keyboard_arrow_down_rounded)),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              if (widget.title != null)
                Text(
                  widget.title!, // Use null assertion operator
                  style: Theme.of(context).textTheme.bodySmall?.copyWith(
                      color: context.isDark ? null : AppColor.neutral20,
                      fontWeight: FontWeight.w500),
                ),
              DropdownButton<Lookup>(
                value: _selectedStatus,
                iconSize: 24,
                isDense: true,
                icon: const SizedBox.shrink(),
                elevation: 0,
                isExpanded: true,
                hint: Text(
                  context.translation.select,
                  style: Theme.of(context).textTheme.bodyLarge,
                ),
                style: TextStyle(color: Theme.of(context).primaryColor),
                underline: const SizedBox.shrink(),
                onChanged: !widget.enabled
                    ? null
                    : (Lookup? newValue) { // Nullable Lookup
                  setState(() {
                    _selectedStatus = newValue;
                  });
                  widget.onSelect?.call(newValue); // Use null-aware operator
                },
                items: widget.statuses
                    ?.map<DropdownMenuItem<Lookup>>((value) {
                  return DropdownMenuItem<Lookup>(
                    value: value,
                    child: Text(value.name ?? "", // Use null-aware operator
                      style: Theme.of(context).textTheme.bodyLarge,
                    ),
                  );
                })
                    .toList(),
              ),
            ],
          ),
        ],
      ),
    );
  }
}