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 '../../../product/constant/icon/icon_constants.dart';
import '../../../product/extension/context_extension.dart';
import '../../../product/services/language_services.dart';

import '../../../bloc/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: Theme.of(context).colorScheme.surface,
      // 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.responsiveBodyLargeWithBold,
              ),
              Container(
                alignment: Alignment.centerRight,
                child: IconButton.outlined(
                  onPressed: () async {
                    mapBloc.sinkPolylines.add([]);
                    markers.clear();
                    await mapBloc.updateCameraPosition(
                      controller,
                      deviceLat,
                      deviceLng,
                      13.0,
                    );
                    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,
                ),
              ),
            ],
          ),
        ],
      ),
    ),
  );
}
