import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_allergy.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/dialogs/master_key_dailog.dart';
import 'package:doctor_app_flutter/widgets/shared/expandable-widget-header-body.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app-textfield-custom.dart';
import 'package:eva_icons_flutter/eva_icons_flutter.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../soap_update/soap_utils.dart';


class AddAllergiesItem extends StatefulWidget {
  final SOAPViewModel? model;
  final Function(MasterKeyModel)? removeAllergy;
  final Function(MySelectedAllergy mySelectedAllergy)? addAllergy;
  final bool Function(MasterKeyModel)? isServiceSelected;
  final MySelectedAllergy Function(MasterKeyModel)? getServiceSelectedAllergy;
  final MasterKeyModel? item;

  const AddAllergiesItem({Key? key, this.model, this.removeAllergy, this.addAllergy, this.isServiceSelected, this.getServiceSelectedAllergy, this.item}) : super(key: key);

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

class _AddAllergiesItemState extends State<AddAllergiesItem> {
  MasterKeyModel _selectedAllergySeverity = MasterKeyModel();
  bool isSubmitted = false;

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);

    bool isSelected = widget.isServiceSelected!(widget.item!);
    MySelectedAllergy mySelectedAllergy = MySelectedAllergy();
    if (isSelected) {
      mySelectedAllergy = widget.getServiceSelectedAllergy!(widget.item!);
    }
    TextEditingController remarkController = TextEditingController(text: isSelected ? mySelectedAllergy.remark : null);
    TextEditingController severityController = TextEditingController(
        text: isSelected
            ? mySelectedAllergy.selectedAllergySeverity != null
                ? projectViewModel.isArabic
                    ? mySelectedAllergy.selectedAllergySeverity!.nameAr
                    : mySelectedAllergy.selectedAllergySeverity!.nameEn
                : null
            : null);
    return HeaderBodyExpandableNotifier(
      headerWidget: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Checkbox(
                  value: widget.isServiceSelected!(widget.item!),
                  activeColor: Colors.red[800],
                  onChanged: (bool? newValue) {
                    onTapItem();
                  }),
              InkWell(
                onTap: onTapItem,
                child: Container(
                  child: AppText(
                    projectViewModel.isArabic
                        ? widget.item!.nameAr != ""
                            ? widget.item!.nameAr!
                            : widget.item!.nameEn!
                        : widget.item!.nameEn!,
                    color: Color(0xFF575757),
                    fontSize: SizeConfig.getTextMultiplierBasedOnWidth() * (SizeConfig.isWidthLarge ? 3 : 3.8),
                    letterSpacing: -0.56,
                  ),
                  width: MediaQuery.of(context).size.width * 0.55,
                ),
              ),
            ],
          ),
          InkWell(
            onTap: () {
              if (mySelectedAllergy != null) {
                setState(() {
                  mySelectedAllergy.isExpanded = mySelectedAllergy.isExpanded == null ? false : true;
                });
              }
            },
            child: Icon(
              (mySelectedAllergy != null ? mySelectedAllergy.isExpanded! : false) ? EvaIcons.arrowIosUpwardOutline : EvaIcons.arrowIosDownwardOutline,
              color: Color(0xFF575757),
              size: 20,
            ),
          )
        ],
      ),
      bodyWidget: Center(
        child: FractionallySizedBox(
          widthFactor: 0.9,
          child: Center(
            child: Column(
              children: [
                AppTextFieldCustom(
                  onClick: widget.model!.allergySeverityList != null
                      ? () {
                          MasterKeyDailog dialog = MasterKeyDailog(
                            list: widget.model!.allergySeverityList,
                            selectedValue: mySelectedAllergy.selectedAllergySeverity!,
                            okText: TranslationBase.of(context).ok,
                            okFunction: (selectedValue) {
                              setState(() {
                                mySelectedAllergy.selectedAllergySeverity = selectedValue;
                              });
                            },
                          );
                          showDialog(
                            barrierDismissible: false,
                            context: context,
                            builder: (BuildContext context) {
                              return dialog;
                            },
                          );
                        }
                      : () {},
                  isTextFieldHasSuffix: true,
                  hintText: TranslationBase.of(context).selectSeverity + "*",
                  enabled: false,
                  maxLines: 1,
                  minLines: 1,
                  height: Utils.getTextFieldHeight(),
                  validationError: mySelectedAllergy != null && mySelectedAllergy.selectedAllergySeverity == null && mySelectedAllergy.hasValidationError!
                      ? TranslationBase.of(context).severityValidationError
                      : "",
                  controller: severityController,
                  onChanged: (value) {},
                  onFieldSubmitted: () {},
                ),
                SizedBox(
                  height: 10,
                ),
                AppTextFieldCustom(
                  hintText: TranslationBase.of(context).remarks,
                  controller: remarkController,
                  maxLines: 25,
                  minLines: 3,
                  hasBorder: true,
                  onChanged: (value) {
                    mySelectedAllergy.remark = value;
                  },
                  inputType: TextInputType.multiline,
                  onClick: () {},
                  onFieldSubmitted: () {},
                ),
                SizedBox(
                  height: 10,
                ),
              ],
            ),
          ),
        ),
      ),
      isExpand: mySelectedAllergy != null ? mySelectedAllergy.isExpanded! : false,
    );
  }

  onTapItem() {
    setState(() {
      if (widget.isServiceSelected!(widget.item!)) {
        widget.removeAllergy!(widget.item!);
      } else {
        MySelectedAllergy mySelectedAllergy = SoapUtils.generateMySelectedAllergy(allergy: widget.item, allergySeverity: _selectedAllergySeverity, remark: null, isChecked: true, isExpanded: true);
        widget.addAllergy!(mySelectedAllergy);
      }
    });
  }
}
