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

import '../../../models/new_models/assistant_employee.dart';
import '../../../new_views/app_style/app_color.dart';

class NurseEmployeeMenu extends StatefulWidget {
  final List<Employee> list;
  final Employee initialValue;
  final Function(Employee) onSelect;
  final String title;
  final bool enable;

  const NurseEmployeeMenu({Key key, this.list, this.title, this.onSelect, this.initialValue, this.enable = true}) : super(key: key);

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

class _SingleNurseEmployeeMenuState extends State<NurseEmployeeMenu> {
  Employee _selectedStatus;

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

  @override
  void didUpdateWidget(covariant NurseEmployeeMenu oldWidget) {
    if (widget.initialValue != null) {
      final result = widget.list?.where((element) {
        return element?.id == widget.initialValue?.id;
      });
      if (result.isNotEmpty) {
        _selectedStatus = result.first;
      } else {
        _selectedStatus = null;
      }
      if ((widget.initialValue?.id ?? "") != (_selectedStatus?.id ?? "")) {
        widget.onSelect(_selectedStatus);
      }
    } else {
      _selectedStatus = null;
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void initState() {
    if (widget.initialValue != null) {
      final result = widget.list?.where((element) {
        return element?.id == widget.initialValue?.id;
      });
      if (result.isNotEmpty) _selectedStatus = result.first;
      if (widget.initialValue?.id != _selectedStatus?.id) {
        widget.onSelect(_selectedStatus);
      }
    }

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 60.toScreenHeight,
      padding: EdgeInsets.symmetric(horizontal: 16.toScreenWidth),
      decoration: BoxDecoration(
        color: context.isDark ? AppColor.neutral50 : Colors.white,
        borderRadius: BorderRadius.circular(10),
        boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 10)],
      ),
      child: Stack(
        alignment: Alignment.center,
        children: [
          PositionedDirectional(
              end: 0,
              child: Icon(
                Icons.keyboard_arrow_down_rounded,
                color: widget.enable ? null : Colors.grey,
              )),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              if (widget.title != null)
                Text(
                  widget.title,
                  style: Theme.of(context).textTheme.bodySmall?.copyWith(color: context.isDark ? null : AppColor.neutral20, fontWeight: FontWeight.w500),
                ),
              DropdownButton<Employee>(
                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.enable
                    ? (Employee newValue) {
                        setState(() {
                          _selectedStatus = newValue;
                        });
                        widget.onSelect(newValue);
                      }
                    : null,
                items: widget.list.map<DropdownMenuItem<Employee>>((Employee value) {
                  return DropdownMenuItem<Employee>(
                    value: value,
                    child: Text(
                      value.name ?? "NULL",
                      style: Theme.of(context).textTheme.bodyLarge,
                    ),
                  );
                }).toList(),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
