import 'package:diplomaticquarterapp/config/size_config.dart';
import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/buttons/button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:diplomaticquarterapp/core/model/pharmacies/pharmacies_model.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacies_view_model.dart';
import '../base/base_view.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/input/text_field.dart';
import 'package:diplomaticquarterapp/widgets/pharmacy/drug_item.dart';
import 'package:diplomaticquarterapp/core/service/pharmacies_service.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/pharmacies_list_screen.dart';

class MedicineSearch extends StatelessWidget {
  final textController = TextEditingController();
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return BaseView<PharmacyViewModel>(
      builder: (BuildContext context, PharmacyViewModel model, Widget child) =>
          AppScaffold(
              baseViewModel: model,
              appBarTitle: TranslationBase.of(context).searchMedicine,
              body: Container(
                height: SizeConfig.screenHeight,
                child: Column(
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.only(
                        bottom: SizeConfig.heightMultiplier * 1,
                        right: SizeConfig.heightMultiplier * 2.5,
                        top: SizeConfig.heightMultiplier * 4,
                        left: SizeConfig.heightMultiplier * 2.5,
                      ),
                      child: Form(
                        key: _formKey,
                        child: TextFields(
                          hintText:
                              TranslationBase.of(context).searchMedicineHere,
                          inputAction: TextInputAction.search,
                          onSaved: (value) {},
                          onSubmit: (value) {
                            searchMedicine(model, context);
                          },
                          controller: textController,
                          validator: (value) {
                            if (value.isEmpty) {
                              return TranslationBase.of(context)
                                  .pleaseEnterMedicineName;
                            }
                            return null;
                          },
                        ),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(
                        bottom: SizeConfig.heightMultiplier * 0.0,
                        right: SizeConfig.heightMultiplier * 5,
                        top: SizeConfig.heightMultiplier * 1,
                        left: SizeConfig.heightMultiplier * 5,
                      ),
                      child: Wrap(
                        children: <Widget>[
                          Container(
                            child: Button(
                                loading: model.state == ViewState.BusyLocal,
                                label: TranslationBase.of(context).search,
                                onTap: () {
                                  searchMedicine(model, context);
                                }),
                            width: 350.0,
                          ),
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(
                          left: SizeConfig.heightMultiplier * 2),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Texts(
                            'You Can Find  ' +
                                '${model.pharmacy == null ? '0' : model.pharmacy.length.toString()}'
                                    ' Item In Search ',
                          ),
                        ],
                      ),
                    ),
                    if (model.state == ViewState.ErrorLocal)
                      Container(
                        child: Texts(model.error),
                      ),
                    Expanded(
                      child: Container(
                        width: SizeConfig.screenWidth * 0.85,
                        //child: _getHospitals(model.pharmacy),
                        child: ListView.builder(
                            //physics: NeverScrollableScrollPhysics(),
                            scrollDirection: Axis.vertical,
                            shrinkWrap: true,
                            itemCount: model.pharmacy == null
                                ? 0
                                : model.pharmacy.length,
                            itemBuilder: (BuildContext context, int index) {
                              return InkWell(
                                child: MedicineItemWidget(
                                    label: model.pharmacy[index].itemDes,
                                    url: model.pharmacy[index].productImage),
                                onTap: () {
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder: (context) => PharmaciesList(
                                          medicineID:
                                              model.pharmacy[index].itemID,
                                          imageURL: model
                                              .pharmacy[index].productImage,
                                          medicineDescription:
                                              model.pharmacy[index].itemDes),
                                    ),
                                  );
                                },
                              );
                            }),
                      ),
                    ),
                  ],
                ),
              )),
    );
  }

  searchMedicine(PharmacyViewModel model, BuildContext context) {
    Utils.hideKeyboard(context);
    if (_formKey.currentState.validate())
      model.getMedicine(name: textController.text);
  }
}
