import 'dart:convert';
import 'dart:typed_data';

import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/viewModel/medicine_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/icons_app/doctor_app_icons.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/utils/dr_app_shared_pref.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/widgets/medicine/medicine_item_widget.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/card_with_bg_widget.dart';
import 'package:flutter/material.dart';
import 'package:maps_launcher/maps_launcher.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher_string.dart';

DrAppSharedPreferances sharedPref = DrAppSharedPreferances();

class PharmaciesListScreen extends StatefulWidget {
  final int itemID;
  final String? url;

  PharmaciesListScreen({Key? key, required this.itemID, this.url}) : super(key: key);

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

class _PharmaciesListState extends State<PharmaciesListScreen> {
  Utils helpers = new Utils();
  late ProjectViewModel projectsProvider;

  @override
  Widget build(BuildContext context) {
    projectsProvider = Provider.of(context);
    return BaseView<MedicineViewModel>(
      onModelReady: (model) => model.getPharmaciesList(widget.itemID),
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        appBarTitle: TranslationBase.of(context).pharmaciesList,
        body: Center(
          child: FractionallySizedBox(
            widthFactor: 0.95,
            child: Container(
              height: SizeConfig.screenHeight,
              child: ListView(shrinkWrap: true, scrollDirection: Axis.vertical, physics: const AlwaysScrollableScrollPhysics(), children: <Widget>[
                model.pharmaciesList.length > 0
                    ? MedicineItemWidget(
                        label: model.pharmaciesList[0]["ItemDescription"],
                        url: widget.url!,
                        showArrow: false,
                        showPrice: true,
                        price: model.pharmaciesList[0]["SellingPrice"].toString(),
                      )
                    : Container(),
                Container(
                  margin: EdgeInsets.only(
                    top: SizeConfig.widthMultiplier! * 2,
                    bottom: SizeConfig.widthMultiplier! * 2,
                    right: SizeConfig.widthMultiplier! * 4,
                    left: SizeConfig.widthMultiplier! * 4,
                  ),
                  child: Align(
                    alignment: Alignment.centerLeft,
                    child: AppText(
                      TranslationBase.of(context).youCanFindItIn,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  alignment: projectsProvider.isArabic ? Alignment.topRight : Alignment.topLeft,
                ),
                Container(
                  width: SizeConfig.screenWidth! * 0.99,
                  margin: EdgeInsets.only(left: 10, right: 10),
                  child: ListView.builder(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemCount: model.pharmaciesList == null ? 0 : model.pharmaciesList.length,
                      itemBuilder: (BuildContext context, int index) {
                        return CardWithBgWidget(
                          bgColor: Colors.transparent,
                          hasBorder: false,
                          widget: Row(
                            children: <Widget>[
                              Expanded(
                                flex: 1,
                                child: ClipRRect(
                                  borderRadius: BorderRadius.all(Radius.circular(7)),
                                  child: Image.network(
                                    model.pharmaciesList[index]["ProjectImageURL"],
                                    height: SizeConfig.imageSizeMultiplier! * 15,
                                    width: SizeConfig.imageSizeMultiplier! * 15,
                                    fit: BoxFit.cover,
                                  ),
                                ),
                              ),
                              Expanded(
                                flex: 4,
                                child: AppText(
                                  Utils.convertToTitleCase(model.pharmaciesList[index]["LocationDescription"] ?? ''),
                                  margin: 10,
                                  fontHeight: 1.4,
                                  color: AppGlobal.appTextColor,
                                  textAlign: TextAlign.start,
                                  fontWeight: FontWeight.w600,
                                  letterSpacing: -0.33,
                                ),
                              ),

                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                  InkWell(
                                    onTap: () async {
                                      MapsLauncher.launchCoordinates(double.parse(model.pharmaciesList[index]["Latitude"]), double.parse(model.pharmaciesList[index]["Longitude"]),
                                          model.pharmaciesList[index]["LocationDescription"]);
                                    },
                                    child: Container(
                                      decoration: BoxDecoration(
                                        color: Colors.grey,
                                        borderRadius: BorderRadius.circular(10),
                                      ),
                                      // color:Colors.red[600],

                                      child: Row(
                                        children: [
                                          Icon(
                                            Icons.pin_drop,
                                            size: 15,
                                            color: Colors.white,
                                          ),
                                          SizedBox(
                                            width: 2,
                                          ),
                                          AppText(
                                            "location",
                                            fontSize: 8,
                                            color: Colors.white,
                                          ),
                                        ],
                                      ),
                                      padding: EdgeInsets.all(6),
                                    ),
                                  ),
                                  SizedBox(
                                    height: 10,
                                  ),
                                  InkWell(
                                    onTap: () {
                                      launchUrlString("tel://" + model.pharmaciesList[index]["PhoneNumber"]);
                                    },
                                    child: Container(
                                      decoration: BoxDecoration(
                                        color: Colors.grey,
                                        borderRadius: BorderRadius.circular(10),
                                      ),
                                      // color:Colors.red[600],

                                      child: Row(
                                        children: [
                                          Icon(
                                            DoctorApp.call,
                                            size: 12,
                                            color: Colors.white,
                                          ),
                                          SizedBox(
                                            width: 2,
                                          ),
                                          AppText(
                                            "Call Now",
                                            fontSize: 8,
                                            color: Colors.white,
                                          ),
                                        ],
                                      ),
                                      padding: EdgeInsets.all(6),
                                    ),
                                  ),
                                ],
                              ),
                              // Expanded(
                              //   flex: 2,
                              //   child: Wrap(
                              //     direction: Axis.horizontal,
                              //     alignment: WrapAlignment.end,
                              //     crossAxisAlignment: WrapCrossAlignment.end,
                              //     children: <Widget>[
                              //       Padding(
                              //         padding: EdgeInsets.all(5),
                              //         child: InkWell(
                              //           child: Icon(
                              //             Icons.call,
                              //             color: Colors.red,
                              //           ),
                              //           onTap: () => launch("tel://" +
                              //               model.pharmaciesList[index]
                              //                   ["PhoneNumber"]),
                              //         ),
                              //       ),
                              //       Padding(
                              //         padding: EdgeInsets.all(5),
                              //         child: InkWell(
                              //           child: Icon(
                              //             Icons.pin_drop,
                              //             color: Colors.red,
                              //           ),
                              //           onTap: () {
                              //             MapsLauncher.launchCoordinates(
                              //                 double.parse(
                              //                     model.pharmaciesList[index]
                              //                         ["Latitude"]),
                              //                 double.parse(
                              //                     model.pharmaciesList[index]
                              //                         ["Longitude"]),
                              //                 model.pharmaciesList[index]
                              //                     ["LocationDescription"]);
                              //           },
                              //         ),
                              //       ),
                              //     ],
                              //   ),
                              // ),
                            ],
                          ),
                        );
                      }),
                )
              ]),
            ),
          ),
        ),
      ),
    );
  }

  Image imageFromBase64String(String base64String) {
    return Image.memory(base64Decode(base64String));
  }

  Uint8List dataFromBase64String(String? base64String) {
    if (base64String != null) {
      return base64Decode(base64String);
    } else {
      return Uint8List(0);
    }
  }

  String base64String(Uint8List data) {
    return base64Encode(data);
  }
}
