import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

import '../../../config/config.dart';
import '../../../core/model/search_drug/get_medication_response_model.dart';
import '../../../core/viewModel/medicine_view_model.dart';
import '../../../core/viewModel/prescription/prescription_view_model.dart';
import '../../../utils/translations_delegate_base_utils.dart';
import '../../../utils/utils.dart';
import '../../../widgets/medicine/medicine_item_widget.dart';
import '../../../widgets/shared/buttons/app_buttons_widget.dart';
import '../../../widgets/shared/loader/gif_loader_dialog_utils.dart';
import '../../../widgets/shared/network_base_view.dart';
import '../../../widgets/shared/text_fields/app_text_form_field.dart';
import '../../base/base_view.dart';


class SearchPrescriptionWidget extends StatefulWidget {
  final double? spaceBetweenTextFields;
  final MedicineViewModel? medicineViewModel;
  final PrescriptionViewModel? prescriptionViewModel;
  final Function(GetMedicationResponseModel)? onItemSelected;

  SearchPrescriptionWidget({Key? key, this.spaceBetweenTextFields, this.medicineViewModel, this.prescriptionViewModel, this.onItemSelected}) : super(key: key);

  @override
  State<SearchPrescriptionWidget> createState() => _SearchPrescriptionWidgetState();
}

class _SearchPrescriptionWidgetState extends State<SearchPrescriptionWidget> {
  final myController = TextEditingController();

  @override
  void initState() {

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<MedicineViewModel>(
      onModelReady: (model) async {},
      builder: (
        BuildContext context,
        MedicineViewModel model,
        Widget? child,
      ) =>
          NetworkBaseView(
        baseViewModel: model,
        child: Column(
          children: [
            FractionallySizedBox(
              widthFactor: 0.9,
              child: Container(
                decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(6.0)), border: Border.all(width: 1.0, color: HexColor("#CCCCCC"))),
                padding: EdgeInsets.all(6),
                child: AppTextFormField(
                  borderColor: Colors.transparent,
                  hintText: TranslationBase.of(context).searchMedicineNameHere,
                  controller: myController,
                  onSaved: (value) {},
                  onFieldSubmitted: (value) {
                    searchMedicine(context, model);
                  },
                  inputFormatter: ONLY_LETTERS,
                ),
              ),
            ),
            SizedBox(
              height: 15.0,
            ),
            Container(
              child: Column(
                children: [
                  FractionallySizedBox(
                    widthFactor: 0.8,
                    child: Container(
                      child: Wrap(
                        alignment: WrapAlignment.center,
                        children: <Widget>[
                          AppButton(
                            title: TranslationBase.of(context).search,
                            onPressed: () async {
                              await searchMedicine(
                                context,
                                model,
                              );
                            },
                          ),
                        ],
                      ),
                    ),
                  ),
                  if (myController.text != '')
                    Container(
                      height: MediaQuery.of(context).size.height * 0.5,
                      child: ListView.builder(
                        padding: const EdgeInsets.only(top: 20),
                        scrollDirection: Axis.vertical,
                        itemCount: model.allMedicationList == null ? 0 : model.allMedicationList.length,
                        itemBuilder: (BuildContext context, int index) {
                          return InkWell(
                            child: MedicineItemWidget(label: model.allMedicationList[index].description!),
                            onTap: () async {
                              GifLoaderDialogUtils.showMyDialog(context);
                              await model.getItem(itemID: model.allMedicationList[index].itemId!, isLocalBusy: true);
                              GifLoaderDialogUtils.hideDialog(context);
                              widget.onItemSelected!(model.allMedicationList[index]);
                              // uom = _selectedMedication.uom;
                            },
                          );
                        },
                      ),
                    ),
                ],
              ),
            ),
            SizedBox(
              height: widget.spaceBetweenTextFields,
            ),
          ],
        ),
      ),
    );
  }

  searchMedicine(context, MedicineViewModel model) async {
    FocusScope.of(context).unfocus();
    if (myController.text.length < 3) {
      Utils.showErrorToast(TranslationBase.of(context).moreThan3Letter);
      return;
    }
    GifLoaderDialogUtils.showMyDialog(context);
    await model.getMedicationList(drug: myController.text, isLocalBusy: true);
    GifLoaderDialogUtils.hideDialog(context);
  }
}
