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

import 'package:diplomaticquarterapp/config/size_config.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacies_view_model.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/others/rounded_container.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:maps_launcher/maps_launcher.dart';
import 'package:url_launcher/url_launcher.dart';

import '../base/base_view.dart';

class PharmaciesList extends StatelessWidget {
  final int medicineID;
  final String imageURL;
  final String medicineDescription;

  PharmaciesList({this.medicineID, this.imageURL, this.medicineDescription});

  @override
  Widget build(BuildContext context) {
    return BaseView<PharmacyViewModel>(
      allowAny: true,
      onModelReady: (model) => model.getPharmacies(id: medicineID),
      builder: (BuildContext context, PharmacyViewModel model, Widget child) =>
          AppScaffold(
        appBarTitle: TranslationBase.of(context).pharmaciesList,
        baseViewModel: model,
        isShowAppBar: true,
        isShowDecPage: false,
        body: Container(
          height: SizeConfig.screenHeight,
          child: ListView(
            shrinkWrap: true,
            scrollDirection: Axis.vertical,
            physics: AlwaysScrollableScrollPhysics(),
            children: <Widget>[
              RoundedContainer(
                child: Row(
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: ClipRRect(
                        borderRadius: BorderRadius.all(
                          Radius.circular(7),
                        ),
                        child: Image.memory(
                          dataFromBase64String(imageURL),
                          height: SizeConfig.imageSizeMultiplier * 19,
                          width: SizeConfig.imageSizeMultiplier * 18,
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                    Expanded(
                      flex: 3,
                      child: Padding(
                        padding: EdgeInsets.symmetric(
                            horizontal: 6.8, vertical: 3.5),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: <Widget>[
                            Texts(
                              TranslationBase.of(context).description,
                            ),
                            Texts(
                              medicineDescription,
                            ),
                            Texts(
                              TranslationBase.of(context).price,
                              marginRight: 10,
                              marginLeft: 10,
                              marginBottom: 2.0,
                            ),
                            if (model.pharmacyList.length > 0)
                              Texts(
                                model.pharmacyList[0].price.toString(),
                              ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(
                  top: SizeConfig.widthMultiplier * 2.0,
                  bottom: SizeConfig.widthMultiplier * 3.0,
                  right: SizeConfig.widthMultiplier * 4.0,
                  left: SizeConfig.widthMultiplier * 2.5,
                ),
                child: Align(
                  alignment: Alignment.center,
                  child: Texts(
                    TranslationBase.of(context).youCanFindItIn,
                  ),
                ),
                alignment: Alignment.topRight,
              ),
              Container(
                width: SizeConfig.screenWidth * 1.0,
                child: ListView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemCount: model.pharmacyList == null
                      ? 0
                      : model.pharmacyList.length,
                  itemBuilder: (BuildContext context, int index) {
                    return RoundedContainer(
                      child: Row(
                        children: <Widget>[
                          Expanded(
                            child: ClipRRect(
                              borderRadius:
                                  BorderRadius.all(Radius.circular(7)),
                              child: Image.network(
                                model.pharmacyList[index].imageLocation,
                                height: SizeConfig.imageSizeMultiplier * 13.0,
                                width: SizeConfig.imageSizeMultiplier * 11.0,
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                          Expanded(
                            flex: 4,
                            child: Padding(
                              padding: EdgeInsets.all(9.0),
                              child: Texts(
                                model.pharmacyList[index].desLocation,
                              ),
                            ),
                          ),
                          Expanded(
                            flex: 2,
                            child: Wrap(
                              direction: Axis.horizontal,
                              alignment: WrapAlignment.end,
                              crossAxisAlignment: WrapCrossAlignment.end,
                              children: <Widget>[
                                Padding(
                                  padding: EdgeInsets.all(6.0),
                                  child: InkWell(
                                    child: Icon(
                                      Icons.phone,
                                      color: Theme.of(context).primaryColor,
                                    ),
                                    onTap: () => launch("tel://" +
                                        model.pharmacyList[index].phoneNumber),
                                  ),
                                ),
                                Padding(
                                  padding: EdgeInsets.all(6.0),
                                  child: InkWell(
                                    child: Icon(
                                      Icons.local_pharmacy,
                                      color: Theme.of(context).primaryColor,
                                    ),
                                    onTap: () {
                                      MapsLauncher.launchCoordinates(
                                        double.parse(
                                            model.pharmacyList[index].latitude),
                                        double.parse(model
                                            .pharmacyList[index].longitude),
                                      );
                                    },
                                  ),
                                ),
                              ],
                            ),
                          )
                        ],
                      ),
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Uint8List dataFromBase64String(String base64String) {
    return base64Decode(base64String);
  }
}
