import 'package:hmg_patient_app/core/viewModels/pharmacyModule/product_detail_view_model.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:map_launcher/map_launcher.dart';
import 'package:url_launcher/url_launcher.dart';

class AvailabilityInfo extends StatelessWidget {
  final ProductDetailViewModel? previousModel;

  final InAppBrowser browser = new InAppBrowser();

  AvailabilityInfo({Key? key, this.previousModel}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return previousModel!.productLocationService.length == 0
        ? Container(
            padding: EdgeInsets.all(15),
            alignment: Alignment.center,
            child: Text(
              TranslationBase.of(context).noLocationAvailable,
            ),
          )
        : Container(
            margin: EdgeInsets.only(bottom: 40),
            child: ListView.builder(
              physics: ScrollPhysics(),
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              itemCount: previousModel!.productLocationService.length,
              itemBuilder: (BuildContext context, int index) {
                return Padding(
                  padding: EdgeInsets.all(8),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Expanded(
                            flex: 1,
                            child: Image.network(previousModel!.productLocationService![index].projectImageUrl!),
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Expanded(
                            flex: 4,
                            child: Text(
                              previousModel!.productLocationService![index].locationDescription! + "\n" + previousModel!.productLocationService[index].cityName.toString(),
                              style: TextStyle(fontSize: 12),
                            ),
                          ),
                          Expanded(
                            flex: 1,
                            child: IconButton(
                              icon: Icon(Icons.location_on),
                              color: Colors.red,
                              onPressed: () async {
                                await MapLauncher.showMarker(
                                  mapType: MapType.google,
                                  coords: Coords(double.parse(previousModel!.productLocationService[index].latitude!), double.parse(previousModel!.productLocationService[index]!.longitude!)),
                                  title: previousModel!.productLocationService![index].locationDescription!,
                                );
                              },
                            ),
                          ),
                          Expanded(
                            flex: 1,
                            child: IconButton(
                              icon: Icon(Icons.phone),
                              color: Colors.red,
                              onPressed: () {
                                launch("tel://" + previousModel!.productLocationService[index]!.phoneNumber!);
                              },
                            ),
                          ),
                        ],
                      ),
                      Divider(height: 1.2, color: Colors.grey)
                    ],
                  ),
                );
              },
            ),
          );
  }

  convertCityName(txt) {
    String stringTxt;
    String newTxt;
    stringTxt = txt.toString();
    newTxt = stringTxt.split('.')[1];

    return newTxt;
  }
}
