import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_allergy.dart';
import 'package:doctor_app_flutter/screens/patients/In_patient/NoData.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/remark_text.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/remove_button.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/subjective/allergies/reactions_selection.dart';
import 'package:doctor_app_flutter/utils/extenstions_utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/divider_with_spaces_around.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../shared_soap_widgets/SOAP_open_items.dart';
import 'add_allergies.dart';

// ignore: must_be_immutable
class UpdateAllergiesWidget extends StatefulWidget {
  List<MySelectedAllergy>? myAllergiesList =[];

  UpdateAllergiesWidget({Key? key, this.myAllergiesList});

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

class _UpdateAllergiesWidgetState extends State<UpdateAllergiesWidget> {
  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    changeAllState() {
      setState(() {

      });
    }

    return Column(
      children: [
        SOAPOpenItems(
          label: "${TranslationBase.of(context).addAllergies}",
          onTap: () {
            openAllergiesList(context, changeAllState, removeAllergy);
          },
        ),
        SizedBox(
          height: 20,
        ),
        widget.myAllergiesList !=null ? Container(
          margin: EdgeInsets.only(left: 15, right: 15, top: 15),
          child: Column(
              children: widget.myAllergiesList!.map((selectedAllergy) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              AppText(
                                projectViewModel.isArabic ? selectedAllergy.selectedAllergy!.nameAr! : selectedAllergy.selectedAllergy!.nameEn!.toUpperCase(),
                                textDecoration: selectedAllergy.isChecked! ? TextDecoration.none : TextDecoration.lineThrough,
                                bold: true,
                                color: Color(0xFF2B353E),
                                fontSize: SizeConfig.getTextMultiplierBasedOnWidth() * 3.5,
                                fontWeight: FontWeight.w700,
                                letterSpacing: -0.48,
                                // fontHeight:0.18 ,
                              ),
                              AppText(
                                projectViewModel.isArabic ? selectedAllergy.selectedAllergySeverity!.nameAr! : selectedAllergy.selectedAllergySeverity!.nameEn ?? ""!.toUpperCase(),
                                textDecoration: selectedAllergy.isChecked! ? TextDecoration.none : TextDecoration.lineThrough,
                                color: Color(0xFFCC9B14),
                                fontSize: SizeConfig.getTextMultiplierBasedOnWidth() * 3,
                                fontWeight: FontWeight.w700,
                                letterSpacing: -0.48,
                              ),
                            ],
                          ),
                          width: MediaQuery.of(context).size.width * 0.5,
                        ),
                        if (selectedAllergy.isChecked)
                          RemoveButton(
                            onTap: () => removeAllergy(selectedAllergy),
                          )
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Row(
                        children: [
                          RemarkText(
                            remark: selectedAllergy.remark ?? "",
                          ),
                        ],
                      ),
                    ),
                    DividerWithSpacesAround()
                  ],
                ),
                SizedBox(
                  height: 10,
                ),
              ],
            );
          }).toList()),
        ) : SizedBox()
      ],
    );
  }

  removeAllergy(MySelectedAllergy mySelectedAllergy) {
    List<MySelectedAllergy> allergy =
        // ignore: missing_return
        widget.myAllergiesList!
            .where((element) => mySelectedAllergy.selectedAllergySeverity!.id == element.selectedAllergySeverity!.id && mySelectedAllergy.selectedAllergy!.id == element.selectedAllergy!.id)
            .toList();

    if (allergy.length > 0) {
      if (allergy.first.isLocal) {
        setState(() {
          widget.myAllergiesList!.remove(allergy.first);
        });
      }
      setState(() {
        allergy[0].isChecked = false;
      });
    }
  }

  openAllergiesList(BuildContext context, Function changeParentState, removeAllergy) {
    showModalBottomSheet(
        backgroundColor: Colors.white,
        isScrollControlled: true,
        isDismissible: false,
        context: context,
        builder: (context) {
          return AddAllergies(
              myAllergiesList: widget.myAllergiesList,
              addAllergiesFun: (List<MySelectedAllergy> mySelectedAllergy) {
                bool isAllDataFilled = true;
                mySelectedAllergy.forEach((element) {
                  if (element.selectedAllergySeverity == null) {
                    element.hasValidationError = true;
                    isAllDataFilled = false;
                  }
                });
                if (isAllDataFilled) {
                  mySelectedAllergy.forEach((element) {
                    if (
                          widget.myAllergiesList!.singleWhere(
                            (it) => it.selectedAllergy!.id == element.selectedAllergy!.id,
                            orElse: () => MySelectedAllergy(),
                          ).selectedAllergySeverity
                         ==
                        null) {
                      widget.myAllergiesList!.add(element);
                    }
                  });

                  /// remove items.
                  List<MySelectedAllergy> removedList = [];
                  widget.myAllergiesList!.forEach((element) {
                    if (
                          mySelectedAllergy.singleWhere(
                            (it) => it.selectedAllergy!.id == element.selectedAllergy!.id,
                            orElse: () => MySelectedAllergy(),
                          )
                        .selectedAllergySeverity ==
                        null) {
                      removedList.add(element);
                    }
                  });

                  removedList.forEach((element) {
                    removeAllergy(element);
                  });
                  changeParentState();

                  Navigator.of(context).pop();
                } else {
                  Utils.showErrorToast(TranslationBase.of(context).requiredMsg);
                }
              });
        });
  }
}
