import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/providers/medicine_provider.dart';
import 'package:doctor_app_flutter/screens/medicine/pharmacies_list_screen.dart';
import 'package:doctor_app_flutter/util/dr_app_shared_pref.dart';
import 'package:doctor_app_flutter/util/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:doctor_app_flutter/widgets/medicine/medicine_item_widget.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_text_form_field.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/dr_app_circular_progress_Indeicator.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../util/extenstions.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';

DrAppSharedPreferances sharedPref = DrAppSharedPreferances();

class MedicineSearchScreen extends StatefulWidget with DrAppToastMsg {
  MedicineSearchScreen({this.changeLoadingStata});
  final Function changeLoadingStata;

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

class _MedicineSearchState extends State<MedicineSearchScreen> {
  var data;
  final myController = TextEditingController();
  Helpers helpers = new Helpers();

  MedicineProvider _medicineProvider;

  bool _isInit = true;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (_isInit) {
      _medicineProvider = Provider.of<MedicineProvider>(context);
    }
    _isInit = false;
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
        appBarTitle: TranslationBase.of(context).searchMedicine,
        body: Column(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(
                  bottom: SizeConfig.heightMultiplier * 1,
                  right: SizeConfig.heightMultiplier * 2,
                  left: SizeConfig.heightMultiplier * 2,
                  top: SizeConfig.heightMultiplier * 3),
              child: AppTextFormField(
                  hintText: TranslationBase.of(context).searchMedicineNameHere,
                  controller: myController,
                  onSaved: (value) {},
                  onFieldSubmitted: (value){
                     searchMedicine(context);
                  },
                  textInputAction: TextInputAction.search,
                  inputFormatter: ONLY_LETTERS),
            ),
            Container(
              margin: EdgeInsets.only(
                  bottom: SizeConfig.heightMultiplier * 1,
                  right: SizeConfig.heightMultiplier * 2,
                  left: SizeConfig.heightMultiplier * 2,
                  top: SizeConfig.heightMultiplier * 1),
              child: Wrap(
                alignment: WrapAlignment.center,
                children: <Widget>[
                  AppButton(
                    title: TranslationBase.of(context).search,
                    color: Color(PRIMARY_COLOR),
                    onPressed: () {
                      searchMedicine(context);
                    },
                  ),
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: SizeConfig.heightMultiplier * 2),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  AppText(
                    TranslationBase.of(context).youCanFind +
                        (_medicineProvider.pharmacyItemsList == null ? "0" : _medicineProvider.pharmacyItemsList.length.toString()) +
                        TranslationBase.of(context).itemsInSearch,
                    fontWeight: FontWeight.bold,
                    margin: 5,
                  ),
                ],
              ),
            ),
            Expanded(
              child: Container(
                width: SizeConfig.screenWidth * 0.90,
                child: !_medicineProvider.isFinished
                    ? DrAppCircularProgressIndeicator()
                    : _medicineProvider.hasError
                        ? Center(
                            child: Text(
                              _medicineProvider.errorMsg,
                              style: TextStyle(
                                  color: Theme.of(context).errorColor),
                            ),
                          )
                        : ListView.builder(
                            scrollDirection: Axis.vertical,
                            shrinkWrap: true,
                            itemCount: _medicineProvider.pharmacyItemsList == null ? 0 : _medicineProvider.pharmacyItemsList.length,
                            itemBuilder: (BuildContext context, int index) {
                              return InkWell(
                                child: MedicineItemWidget(
                                  label: _medicineProvider.pharmacyItemsList[index]["ItemDescription"],
                                  url: _medicineProvider.pharmacyItemsList[index]["ProductImageBase64"],
                                ),
                                onTap: () {
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder: (context) =>
                                          PharmaciesListScreen(
                                              itemID: _medicineProvider.pharmacyItemsList[index]["ItemID"],
                                              url: _medicineProvider.pharmacyItemsList[index]
                                              ["ProductImageBase64"]),
                                    ),
                                  );
                                },
                              );
                            },
                          ),
              ),
            ),
          ],
        ));
  }

  searchMedicine(context) {
    FocusScope.of(context).unfocus();
    if (myController.text.isNullOrEmpty()) {
      _medicineProvider.clearPharmacyItemsList();
      helpers.showErrorToast("Type Medicine Name");
      return;
    }
    _medicineProvider.getMedicineItem(myController.text);
  }
}
