import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:maps_launcher/maps_launcher.dart';
import 'package:sfm_app/product/constant/icon/icon_constants.dart';
import 'package:sfm_app/product/extention/context_extention.dart';
import 'package:sfm_app/product/services/language_services.dart';

import '../map_bloc.dart';

showDirections(
  BuildContext context,
  Completer<GoogleMapController> controller,
  List<Marker> markers,
  MapBloc mapBloc,
  String originalName,
  String destinationLocation,
  double devicelat,
  double devicelng,
) {
  TextEditingController originController =
      TextEditingController(text: originalName);
  TextEditingController destinationController =
      TextEditingController(text: destinationLocation);
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
      backgroundColor: Colors.transparent,
      // dismissDirection: DismissDirection.none,
      duration: const Duration(minutes: 5),
      content: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                appLocalization(context).map_show_direction,
                style: context.titleLargeTextStyle,
              ),
              Container(
                alignment: Alignment.centerRight,
                child: IconButton.outlined(
                  onPressed: () async {
                    await mapBloc.updateCameraPosition(
                      controller,
                      devicelat,
                      devicelng,
                      13.0,
                    );
                    List<LatLng> polylineCoordinates = [];
                    mapBloc.sinkPolylines.add(polylineCoordinates);
                    markers.clear();
                    if (context.mounted) {
                      ScaffoldMessenger.of(context).hideCurrentSnackBar();
                    }
                  },
                  icon: IconConstants.instance.getMaterialIcon(Icons.close),
                ),
              )
            ],
          ),
          Row(
            children: [
              Text(
                '${appLocalization(context).map_start}: ',
              ),
              SizedBox(width: context.lowValue),
              Expanded(
                child: TextField(
                  controller: originController,
                  readOnly: true,
                ),
              ),
            ],
          ),
          SizedBox(
            height: context.lowValue,
          ),
          Row(
            children: [
              Text(
                '${appLocalization(context).map_destination}: ',
              ),
              SizedBox(width: context.lowValue),
              Expanded(
                child: TextField(
                  controller: destinationController,
                  readOnly: true,
                ),
              ),
            ],
          ),
          SizedBox(height: context.lowValue),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              ElevatedButton.icon(
                onPressed: () async {
                  ScaffoldMessenger.of(context).hideCurrentSnackBar();
                  List<LatLng> polylineCoordinates = [];
                  mapBloc.sinkPolylines.add(polylineCoordinates);
                  MapsLauncher.launchCoordinates(devicelat, devicelng);
                },
                icon: IconConstants.instance
                    .getMaterialIcon(Icons.near_me_rounded),
                label: Text(
                  appLocalization(context).map_stream,
                ),
                style: ButtonStyle(
                  backgroundColor:
                      MaterialStateProperty.all<Color>(Colors.blue[300]!),
                ),
              ),
            ],
          ),
        ],
      ),
    ),
  );
}
