import 'package:doctor_app_flutter/core/enum/master_lookup_key.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/SOAP/physical_exam/GeneralSpeciality.dart';
import 'package:doctor_app_flutter/core/model/SOAP/selected_items/my_selected_examination.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/SOAP_view_model.dart';
import 'package:doctor_app_flutter/routes.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/patient_search/patient_search_header.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update/shared_soap_widgets/expandable_SOAP_widget.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/objective/widget/examination_items.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/subjective/chief_complaint/widgets/add_soap_item.dart';
import 'package:doctor_app_flutter/utils/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.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/buttons/app_buttons_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../../core/model/SOAP/physical_exam/Category.dart';
import '../../../../../core/viewModel/project_view_model.dart';

class AddDetailsToExaminationVidaPlus extends StatefulWidget {
  final List<GeneralSpeciality>? mySelectedExamination;
  final PatiantInformtion patientInfo;

  const AddDetailsToExaminationVidaPlus(
      {super.key, this.mySelectedExamination, required this.patientInfo});

  @override
  State<AddDetailsToExaminationVidaPlus> createState() =>
      _AddDetailsToExaminationVidaPlusState();
}

class _AddDetailsToExaminationVidaPlusState
    extends State<AddDetailsToExaminationVidaPlus> {
  String currentlyExpanded = '';

  @override
  Widget build(BuildContext context) {
    print('the widget is build');
    ProjectViewModel projectViewModel = Provider.of(context);
    return BaseView<SOAPViewModel>(
        onModelReady: (model) async {
          WidgetsBinding.instance.addPostFrameCallback((_) async {
            widget.mySelectedExamination?.forEach((value) =>
                model.getSpecialityDetails(
                    value.name ?? '', value.id, widget.patientInfo));
          });
        },
        builder: (_, model, w) => AppScaffold(
              isShowAppBar: true,
              isLoading: model.state == ViewState.BusyLocal,
              appBar: PatientSearchHeader(
                  title: TranslationBase.of(context).examinationPart),
              backgroundColor: Colors.white,
              body: Padding(
                padding: const EdgeInsets.all(20.0),
                child: Column(
                  children: [
                    Expanded(
                      child: ListView.separated(
                          itemBuilder: (context, index) {
                            var title =
                                model.specialityDetails.keys.elementAt(index);
                            return ExpandableSOAPWidget(
                              headerTitle: title,
                              onTap: () {
                                setState(() {
                                  if (currentlyExpanded == title) {
                                    currentlyExpanded = '';
                                  } else
                                    currentlyExpanded = title;
                                });
                              },
                              child: ExaminationItems(
                                  examination: model.specialityDetails[title]),
                              isExpanded: currentlyExpanded == title,
                            );
                          },
                          separatorBuilder: (context, index) {
                            return SizedBox(
                              height: 8,
                            );
                          },
                          itemCount: model.specialityDetails.length ?? 0),
                    ),
                  ],
                ),
              ),
              bottomNavigationBar: Material(
                  color: Colors.white,
                  child: Padding(
                      padding: const EdgeInsets.all(16.0),
                      child: Row(
                        children: [
                          Expanded(
                            child: AppButton(
                              title: TranslationBase.of(context).cancelSmall,
                              color: Color(0xffEAEAEA),
                              fontColor: Colors.black,
                              fontWeight: FontWeight.w600,
                              onPressed: () async {
                                Navigator.pop(context);
                              },
                            ),
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Expanded(
                            child: AppButton(
                              title: TranslationBase.of(context).saveSmall,
                              color: Color(0xff359846),
                              fontColor: Colors.white,
                              fontWeight: FontWeight.w600,
                              onPressed: () async {
                                var listOfSelectedCategory =
                                    model.getListOfSelectedCategory();


                                if (listOfSelectedCategory.isEmpty) {
                                  DrAppToastMsg.showErrorToast(
                                      TranslationBase.of(context)
                                          .kindlySelectAllMandatoryField);
                                  return;
                                }
                                /*    if (listOfSelectedCategory.any((value) {
                                 return  value.remarksController.text.isEmpty && value.conditionsList?.firstWhere((item){
                                   return item.conditionName == value.selectedCondition;
                                 }).conditionCode?.toLowerCase() == TranslationBase.of(context).normal.toLowerCase();
                                })) {
                                  DrAppToastMsg.showErrorToast(
                                      TranslationBase.of(context)
                                          .remarksCanNotBeEmpty);
                                  return;
                                }*/
                                if(listOfSelectedCategory.any((value) {
                                  print('the value is ${value.selectedConditionName}');
                                return value.remarksController.text.isEmpty == true && value.selectedConditionName.isNotEmpty &&value.selectedConditionName != TranslationBase.of(context).normal.toLowerCase(); })) {
                                  DrAppToastMsg.showErrorToast(TranslationBase.of(context).remarksCanNotBeEmpty);
                                  return;
                                }
                                var result =
                                    await model.postPhysicalExamination(
                                        widget.patientInfo,
                                        listOfSelectedCategory);
                                if (result) {
                                  model.getPhysicalExamination(
                                      widget.patientInfo);
                                  Navigator.popUntil(context, ((route) {
                                    if (route.settings.name ==
                                        UPDATE_EPISODE_VIDA_PLUS) {
                                      return true;
                                    } else {
                                      return false;
                                    }
                                  }));
                                }
                              },
                            ),
                          ),
                        ],
                      ))),
            ));
  }
}

/* Container(
                            padding: EdgeInsets.symmetric(horizontal: 12),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Container(
                                  margin: EdgeInsets.only(bottom: 8),
                                  child: AppText(
                                    TranslationBase.of(context).status,
                                    fontWeight: FontWeight.bold,
                                    fontFamily: 'Poppins',
                                    fontSize: SizeConfig.textMultiplier! * 1.6,
                                  ),
                                ),
                                Row(
                                  children: [
                                    Expanded(
                                        child: InkWell(
                                      onTap: () {
                                        setState(() {
                                          status = 1;
                                        });
                                        examination.isNormal = true;
                                        examination.isAbnormal = false;
                                        examination.notExamined = false;
                                      },
                                      child: Row(
                                        children: [
                                          Container(
                                            padding: EdgeInsets.all(2.0),
                                            margin: EdgeInsets.symmetric(horizontal: 6),
                                            width: 20,
                                            height: 20,
                                            decoration: BoxDecoration(
                                              color: Colors.white,
                                              shape: BoxShape.circle,
                                              border: Border.all(color: Colors.grey, width: 1),
                                            ),
                                            child: Container(
                                              decoration: BoxDecoration(
                                                color: status == 1 ? HexColor("#D02127") : Colors.white,
                                                shape: BoxShape.circle,
                                              ),
                                            ),
                                          ),
                                          AppText(
                                            TranslationBase.of(context).normal,
                                            fontWeight: FontWeight.normal,
                                            fontFamily: 'Poppins',
                                            fontSize: SizeConfig.textMultiplier! * 1.6,
                                          ),
                                        ],
                                      ),
                                    )),
                                    Expanded(
                                        child: InkWell(
                                      onTap: () {
                                        setState(() {
                                          status = 2;
                                        });
                                        examination.isNormal = false;
                                        examination.isAbnormal = true;
                                        examination.notExamined = false;
                                      },
                                      child: Row(
                                        children: [
                                          Container(
                                            padding: EdgeInsets.all(2.0),
                                            margin: EdgeInsets.symmetric(horizontal: 6),
                                            width: 20,
                                            height: 20,
                                            decoration: BoxDecoration(
                                              color: Colors.white,
                                              shape: BoxShape.circle,
                                              border: Border.all(color: Colors.grey, width: 1),
                                            ),
                                            child: Container(
                                              decoration: BoxDecoration(
                                                color: status == 2 ? HexColor("#D02127") : Colors.white,
                                                shape: BoxShape.circle,
                                              ),
                                            ),
                                          ),
                                          AppText(
                                            TranslationBase.of(context).abnormal,
                                            fontWeight: FontWeight.normal,
                                            fontFamily: 'Poppins',
                                            fontSize: SizeConfig.textMultiplier! * 1.6,
                                          ),
                                        ],
                                      ),
                                    )),
                                    if (!examination.isLocal)
                                      Expanded(
                                          child: InkWell(
                                        onTap: () {
                                          setState(() {
                                            status = 3;
                                          });
                                          examination.isNormal = false;
                                          examination.isAbnormal = false;
                                          examination.notExamined = true;
                                        },
                                        child: Row(
                                          children: [
                                            Container(
                                              padding: EdgeInsets.all(2.0),
                                              margin: EdgeInsets.symmetric(horizontal: 6),
                                              width: 20,
                                              height: 20,
                                              decoration: BoxDecoration(
                                                color: Colors.white,
                                                shape: BoxShape.circle,
                                                border: Border.all(color: Colors.grey, width: 1),
                                              ),
                                              child: Container(
                                                decoration: BoxDecoration(
                                                  color: status == 3 ? HexColor("#D02127") : Colors.white,
                                                  shape: BoxShape.circle,
                                                ),
                                              ),
                                            ),
                                            Expanded(
                                              child: AppText(
                                                TranslationBase.of(context).notExamined,
                                                fontWeight: FontWeight.normal,
                                                fontFamily: 'Poppins',
                                                fontSize: SizeConfig.textMultiplier! * 1.6,
                                              ),
                                            ),
                                          ],
                                        ),
                                      )),
                                  ],
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 8),
                                  child: AppTextFieldCustom(
                                    hintText: TranslationBase.of(context).remarks,
                                    controller: remarksController,
                                    minLines: 2,
                                    maxLines: 4,
                                    inputType: TextInputType.multiline,
                                    onChanged: (value) {
                                      examination.remark = value;
                                    },
                                    onClick: () {},
                                    onFieldSubmitted: () {},
                                  ),
                                ),
                              ],
                            ),
                          );*/
