import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/models/SOAP/my_selected_allergy.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.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:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:hexcolor/hexcolor.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);
        },),
        SizedBox(
          height: 20,
        ),
        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
                                      ? null
                                      : TextDecoration.lineThrough,
                                  bold: true,
                                  color: Color(0xFF2B353E)),
                              AppText(
                                  projectViewModel.isArabic
                                      ? selectedAllergy.selectedAllergySeverity
                                      .nameAr
                                      : selectedAllergy.selectedAllergySeverity
                                      .nameEn
                                      .toUpperCase(),
                                  textDecoration: selectedAllergy.isChecked
                                      ? null
                                      : TextDecoration.lineThrough,
                                  color: Color(0xFFCC9B14)),
                            ],
                          ),
                          width: MediaQuery
                              .of(context)
                              .size
                              .width * 0.5,
                        ),

                        if (selectedAllergy.isChecked)
                          InkWell(
                            child: Row(
                              children: [Container(
                                child: AppText(
                                  TranslationBase
                                      .of(context)
                                      .remove,
                                  fontSize: 15,
                                  variant: "bodyText",
                                  color: HexColor("#B8382C"),),
                              ),
                                Icon(
                                  FontAwesomeIcons.times,
                                  color: HexColor("#B8382C"),
                                  size: 20,
                                ),
                              ],
                            ),
                            onTap: () => removeAllergy(selectedAllergy),
                          )
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Row(
                        children: [
                          Container(
                            width: MediaQuery.of(context).size.width * 0.55,
                            child: AppText(
                              selectedAllergy.remark ?? '',
                              fontSize: 10,
                              color: Colors.grey,
                            ),
                          ),
                        ],
                      ),
                    ),
                    DividerWithSpacesAround()
                  ],
                ),
                SizedBox(
                  height: 10,
                ),
              ],
            );
          }).toList()),
        )
      ],
    );
  }

  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) {
      setState(() {
        allergy[0].isChecked = false;
      });
    }


    print(allergy);
  }

  openAllergiesList(BuildContext context, Function changeParentState) {
    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) {
                    isAllDataFilled = false;
                  }
                });
                if (isAllDataFilled) {
                  mySelectedAllergy.forEach((element) {
                    if (!widget.myAllergiesList.contains(element.selectedAllergySeverity.id)) {
                      widget.myAllergiesList.add(element);
                    }
                  });
                  changeParentState();
                  Navigator.of(context).pop();
                } else {
                  Helpers.showErrorToast(TranslationBase
                      .of(context)
                      .requiredMsg);
                }
              });
        });
  }

}






