import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:krow_domain/krow_domain.dart';
import 'package:url_launcher/url_launcher.dart';
import 'shift_location_map.dart';

/// A section displaying the shift's location, address, map, and "Get direction" action.
class ShiftLocationSection extends StatelessWidget {
  /// The shift entity containing location data.
  final Shift shift;

  /// Localization string for location section title.
  final String locationLabel;

  /// Localization string for "TBD".
  final String tbdLabel;

  /// Localization string for "Get direction".
  final String getDirectionLabel;

  /// Creates a [ShiftLocationSection].
  const ShiftLocationSection({
    super.key,
    required this.shift,
    required this.locationLabel,
    required this.tbdLabel,
    required this.getDirectionLabel,
  });

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(UiConstants.space5),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        spacing: UiConstants.space4,
        children: [
          Column(
            spacing: UiConstants.space2,
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                locationLabel,
                style: UiTypography.titleUppercase4b.textSecondary,
              ),

              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                spacing: UiConstants.space4,
                children: [
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          shift.location.isEmpty ? tbdLabel : shift.location,
                          style: UiTypography.title1m.textPrimary,
                          overflow: TextOverflow.ellipsis,
                        ),
                        if (shift.locationAddress.isNotEmpty)
                          Text(
                            shift.locationAddress,
                            style: UiTypography.body2r.textSecondary,
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                          ),
                      ],
                    ),
                  ),
                  OutlinedButton.icon(
                    onPressed: () => _openDirections(context),
                    icon: const Icon(
                      UiIcons.navigation,
                      size: UiConstants.iconXs,
                    ),
                    label: Text(getDirectionLabel),
                    style: OutlinedButton.styleFrom(
                      foregroundColor: UiColors.textPrimary,
                      side: const BorderSide(color: UiColors.border),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(
                          UiConstants.radiusBase,
                        ),
                      ),
                      padding: const EdgeInsets.symmetric(
                        horizontal: UiConstants.space3,
                        vertical: 0,
                      ),
                      minimumSize: const Size(0, 32),
                    ),
                  ),
                ],
              ),
            ],
          ),

          ShiftLocationMap(
            shift: shift,
            borderRadius: UiConstants.radiusBase,
          ),
        ],
      ),
    );
  }

  Future<void> _openDirections(BuildContext context) async {
    final destination = (shift.latitude != null && shift.longitude != null)
        ? '${shift.latitude},${shift.longitude}'
        : Uri.encodeComponent(
          shift.locationAddress.isNotEmpty
              ? shift.locationAddress
              : shift.location,
        );

    final String url =
        'https://www.google.com/maps/dir/?api=1&destination=$destination';
    final uri = Uri.parse(url);

    if (await canLaunchUrl(uri)) {
      await launchUrl(uri, mode: LaunchMode.externalApplication);
    } else {
      if (context.mounted) {
        UiSnackbar.show(
          context,
          message: 'Could not open maps',
          type: UiSnackbarType.error,
        );
      }
    }
  }
}
