import 'package:doctor_app_flutter/core/model/SOAP/chief_complaint/episode_by_chief_complaint_vidaplus.dart';
import 'package:doctor_app_flutter/core/model/SOAP/chief_complaint/get_chief_complaint_vida_plus.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/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_vida_plus/subjective/chief_complaint/widgets/ComplaintSelection.dart';
import 'package:doctor_app_flutter/screens/patients/profile/soap_update_vida_plus/subjective/chief_complaint/widgets/previous_cheif_complaints.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/loader/gif_loader_dialog_utils.dart';
import 'package:flutter/material.dart';

class AddChiefComplaint extends StatefulWidget {
  final List<GetChiefComplaintVidaPlus> complaints;
  final String selectedType;
  final String? firstField;
  final String? secondField;
  final String? thirdField;
  final PatiantInformtion patientInfo;

  const AddChiefComplaint(
      {super.key,
      required this.complaints,
      required this.selectedType,
      this.firstField,
      this.secondField,
      this.thirdField,
      required this.patientInfo});

  @override
  State<AddChiefComplaint> createState() => _AddChiefComplaintState();
}

class _AddChiefComplaintState extends State<AddChiefComplaint> {
  @override
  Widget build(BuildContext context) {
    return BaseView<SOAPViewModel>(
      onModelReady: (model) {
        episodeByChiefComplaint(model);
      },
      builder: (_, model, w) => AppScaffold(
        isShowAppBar: true,
        appBar: PatientSearchHeader(
            title: TranslationBase.of(context).addChiefComplaint),
        body: Padding(
          padding: const EdgeInsets.all(20.0),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                ComplaintSelection(
                  complaints: widget.complaints,
                  selectedType: widget.selectedType,
                  firstField: widget.firstField,
                  secondField: widget.secondField,
                  thirdField: widget.thirdField,
                  searchData: model.searchChiefComplaintList,
                  onSearch: (String value) {
                    searchChiefComplaint(model, value);
                  },
                  onSave: (String chiefComplaint) {
                    addChiefComplaint(model, chiefComplaint, context);
                  },
                  onCrossClicked: (GetChiefComplaintVidaPlus chiefComplaint) {
                    updateChiefComplaint(model, chiefComplaint, context);
                  },
                ),
                SizedBox(
                  height: 16,
                ),
                PreviousChiefComplaints(
                  model.episodeByChiefComplaintListVidaPlus,
                  (chiefComplains, String) {
                    var listOfChiefComplaint = [chiefComplains];
                    createCCByEpisode(model, listOfChiefComplaint);
                  },
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  addChiefComplaint(
      SOAPViewModel model, String CC, BuildContext context) async {
    GifLoaderDialogUtils.showMyDialog(context);
    await model.saveChiefComplaint(widget.patientInfo, CC);
    await model.getChiefComplaint(widget.patientInfo);
    GifLoaderDialogUtils.hideDialog(context);
  }

  searchChiefComplaint(SOAPViewModel model, String CC) async {
    await model.searchChiefComplaint(widget.patientInfo, CC);
  }

  updateChiefComplaint(SOAPViewModel model, GetChiefComplaintVidaPlus cc,
      BuildContext context) async {
    GifLoaderDialogUtils.showMyDialog(context);
    await model.updateChiefComplaint(widget.patientInfo, cc);
    await model.getChiefComplaint(widget.patientInfo);
    GifLoaderDialogUtils.hideDialog(context);
    setState(() {});
  }

  episodeByChiefComplaint(SOAPViewModel model) async {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await model.episodeByChiefComplaint(widget.patientInfo);
    });
  }

  createCCByEpisode(model, List<PatientPomrs> chiefComplaint) async {
    GifLoaderDialogUtils.showMyDialog(context);
    var result =
        await model.createCCByEpisode(widget.patientInfo, chiefComplaint);
    GifLoaderDialogUtils.hideDialog(context);
    if (result) Navigator.of(context).pop();
  }
}
