import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/enum/master_lookup_key.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/models/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/models/SOAP/my_selected_allergy.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/shared/Text.dart';
import 'package:doctor_app_flutter/widgets/shared/TextFields.dart';
import 'package:doctor_app_flutter/widgets/shared/app_buttons_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.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:eva_icons_flutter/eva_icons_flutter.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

class AddAllergiesWidget extends StatefulWidget {
  final List<MySelectedAllergy> myAllergiesList;

  AddAllergiesWidget({Key key, this.myAllergiesList});

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

class _AddAllergiesWidgetState extends State<AddAllergiesWidget> {

  TextEditingController remarkController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;

    return Column(
      children: [
        Container(
          margin: EdgeInsets.only(left: 10, right: 10, top: 15),
          child: TextFields(
              hintText: "Add Allergies",
              fontSize: 13.5,
              onTapTextFields: () {
                openAllergiesList(context);
              },
              readOnly: true,
              // hintColor: Colors.black,
              suffixIcon: EvaIcons.plusCircleOutline,
              suffixIconColor: AppGlobal.appPrimaryColor,
              fontWeight: FontWeight.w600,
              // controller: messageController,
              validator: (value) {
                if (value == null)
                  return TranslationBase
                      .of(context)
                      .emptyMessage;
                else
                  return null;
              }),
        ),
        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,
                  children: [
                    Row(
                      mainAxisAlignment:
                      MainAxisAlignment.spaceBetween,
                      children: [
                    Texts(selectedAllergy.selectedAllergy.nameEn.toUpperCase(),
                        variant: "bodyText", bold: true, color: Colors.black),
                    Texts(
                        selectedAllergy.selectedAllergySeverity.nameEn
                            .toUpperCase(),
                        variant: "bodyText",
                        bold: true,
                        color: AppGlobal.appPrimaryColor),
                    InkWell(
                      child: Icon(
                        FontAwesomeIcons.trash,
                        color: Colors.grey,
                        size: 20,
                      ),
                      onTap: () => removeAllergy(selectedAllergy),
                    )
                  ],
                ),
                SizedBox(
                  height: 10,
                ),
              ],
            );
          }).toList()),
        )
      ],
    );
  }

  removeAllergy(MySelectedAllergy mySelectedAllergy) {
    Iterable<MySelectedAllergy> allergy =
        widget.myAllergiesList.where((element) => mySelectedAllergy == element);

    if (allergy.length > 0)
      setState(() {
        widget.myAllergiesList.remove(allergy.first);
      });
  }

  openAllergiesList(BuildContext context) {
    showModalBottomSheet(
        backgroundColor: Colors.white,
        isScrollControlled: true,
        context: context,
        builder: (context) {
          return AddAllergies(
            addAllergiesFun: (MySelectedAllergy mySelectedAllergy) {
              setState(() {
                widget.myAllergiesList.add(mySelectedAllergy);
                Navigator.of(context).pop();
              });
            },);
        });
  }

}

class AddAllergies extends StatefulWidget {
  final Function addAllergiesFun;

  const AddAllergies({Key key, this.addAllergiesFun}) : super(key: key);

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

class _AddAllergiesState extends State<AddAllergies> {
  List<MasterKeyModel> allergiesList;
  List<MasterKeyModel> allergySeverityList;
  MasterKeyModel _selectedAllergySeverity;
  MasterKeyModel _selectedAllergy;
  TextEditingController remarkController = TextEditingController();


  InputDecoration textFieldSelectorDecoration(String hintText,
      String selectedText, bool isDropDown) {
    return InputDecoration(
      focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Color(0xFFCCCCCC), width: 2.0),
        borderRadius: BorderRadius.circular(8),
      ),
      enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Color(0xFFCCCCCC), width: 2.0),
        borderRadius: BorderRadius.circular(8),
      ),
      disabledBorder: OutlineInputBorder(
        borderSide: BorderSide(color: Color(0xFFCCCCCC), width: 2.0),
        borderRadius: BorderRadius.circular(8),
      ),
      hintText: selectedText != null ? selectedText : hintText,
      suffixIcon: isDropDown ? Icon(Icons.arrow_drop_down) : null,
      hintStyle: TextStyle(
        fontSize: 14,
        color: Colors.grey.shade600,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery
        .of(context)
        .size;
    return FractionallySizedBox(
      heightFactor: 0.7,
      child: BaseView<SOAPViewModel>(
        onModelReady: (model) async {
          if (model.listOfAllergies.length == 0) {
            await model.getMasterLookup(MasterKeysService.Allergies);
          }
          if (model.allergySeverityList.length == 0) {
            await model.getMasterLookup(MasterKeysService.AllergySeverity);
          }
        },
        builder: (_, model, w) =>
            AppScaffold(
                baseViewModel: model,
                isShowAppBar: false,
                body: SingleChildScrollView(
                  child: Center(
                    child: FractionallySizedBox(
                      widthFactor: 0.9,
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [

                            SizedBox(
                              height: 16,
                            ),
                            AppText(
                              "Add Allergy",
                              fontWeight: FontWeight.bold,
                              fontSize: 16,
                            ),
                            SizedBox(
                              height: 16,
                            ),
                            Container(
                              height: screenSize.height * 0.070,
                              child: InkWell(
                                onTap: model.listOfAllergies != null
                                    ? () {
                                  MasterKeyDailog dialog = MasterKeyDailog(
                                    list: model.listOfAllergies,
                                    okText: TranslationBase
                                        .of(context)
                                        .ok,
                                    okFunction: (MasterKeyModel selectedValue) {
                                      setState(() {
                                        _selectedAllergy = selectedValue;
                                      });
                                    },
                                  );
                                  showDialog(
                                    barrierDismissible: false,
                                    context: context,
                                    builder: (BuildContext context) {
                                      return dialog;
                                    },
                                  );
                                }
                                    : null,
                                child: TextField(
                                  decoration: textFieldSelectorDecoration(
                                      "Select Allergy",
                                      _selectedAllergy != null
                                          ? _selectedAllergy.nameEn
                                          : null,
                                      true),
                                  enabled: false,
                                ),
                              ),
                            ),
                            SizedBox(
                              height: 10,
                            ), Container(
                              height: screenSize.height * 0.070,
                              child: InkWell(
                                onTap: model.allergySeverityList != null
                                    ? () {
                                  MasterKeyDailog dialog = MasterKeyDailog(
                                    list: model.allergySeverityList,
                                    okText: TranslationBase
                                        .of(context)
                                        .ok,
                                    okFunction: (selectedValue) {
                                      setState(() {
                                        _selectedAllergySeverity =
                                            selectedValue;
                                        // model.getDoctorBranch().then((value) {
                                        //   _selectedBranch = value;
                                        //   if (_referTo['id'] == 1) {
                                        //     model.getClinics(
                                        //         _selectedBranch['ID']);
                                        //   }
                                        // });
                                      });
                                    },
                                  );
                                  showDialog(
                                    barrierDismissible: false,
                                    context: context,
                                    builder: (BuildContext context) {
                                      return dialog;
                                    },
                                  );
                                }
                                    : null,
                                child: TextField(
                                  decoration: textFieldSelectorDecoration(
                                      "Select Severity",
                                      _selectedAllergySeverity != null
                                          ? _selectedAllergySeverity.nameEn
                                          : null,
                                      true),
                                  enabled: false,
                                ),
                              ),
                            ),
                            SizedBox(
                              height: 10,
                            ),
                            Container(
                              margin: EdgeInsets.only(
                                  left: 0, right: 0, top: 15),
                              child: TextFields(
                                  hintText: "Remarks",
                                  fontSize: 13.5,
                                  // hintColor: Colors.black,
                                  fontWeight: FontWeight.w600,
                                  maxLines: 25,
                                  minLines: 13,
                                  controller: remarkController,
                                  validator: (value) {
                                    if (value == null)
                                      return TranslationBase
                                          .of(context)
                                          .emptyMessage;
                                    else
                                      return null;
                                  }),
                            ), SizedBox(
                              height: 10,
                            ),
                            AppButton(
                              title: "Add".toUpperCase(),
                              onPressed: () {
                                MySelectedAllergy mySelectedAllergy = new MySelectedAllergy(
                                    remark: remarkController.text,
                                    selectedAllergy: _selectedAllergy,
                                    selectedAllergySeverity: _selectedAllergySeverity);
                                widget.addAllergiesFun(mySelectedAllergy);
                              },
                            ),
                          ]

                      ),
                    ),
                  ),
                )),
      ),
    );
  }
}





