import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:terepi_seged/pages/map_survey/presentations/controllers/map_survey_controller.dart';

import 'map_info_card.dart';
import 'quality_mini_value.dart';

class GnssQualityCard extends StatelessWidget {
  final MapSurveyController controller;

  const GnssQualityCard({
    super.key,
    required this.controller,
  });

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      final hError = controller.horizontalAccuracy;
      final vError = controller.gpsAltitudeError.value;

      return MapInfoCard(
        onClose: controller.toggleShowGnssQualityCard,
        title: Row(
          children: [
            const Icon(Icons.speed, size: 18),
            const SizedBox(width: 6),
            Text(
              'Minőség',
              style: Theme.of(context).textTheme.titleSmall?.copyWith(
                    fontWeight: FontWeight.w700,
                  ),
            ),
          ],
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(
              children: [
                Expanded(
                  child: QualityMiniValue(
                    label: 'PDOP',
                    value: controller.formatDop(controller.pdop.value),
                    color: qualityColorForDop(
                      context,
                      controller.pdop.value,
                    ),
                  ),
                ),
                Expanded(
                  child: QualityMiniValue(
                    label: 'HDOP',
                    value: controller.formatDop(controller.hdop.value),
                    color: qualityColorForDop(
                      context,
                      controller.hdop.value,
                    ),
                  ),
                ),
                Expanded(
                  child: QualityMiniValue(
                    label: 'VDOP',
                    value: controller.formatDop(controller.vdop.value),
                    color: qualityColorForDop(
                      context,
                      controller.vdop.value,
                    ),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 3),
            Row(
              children: [
                Expanded(
                  child: QualityMiniValue(
                    label: 'Latσ',
                    value: controller.formatMeterCompact(
                      controller.gpsLatitudeError.value,
                    ),
                    color: qualityColorForHorizontalError(
                      context,
                      controller.gpsLatitudeError.value,
                    ),
                  ),
                ),
                Expanded(
                  child: QualityMiniValue(
                    label: 'Lonσ',
                    value: controller.formatMeterCompact(
                      controller.gpsLongitudeError.value,
                    ),
                    color: qualityColorForHorizontalError(
                      context,
                      controller.gpsLongitudeError.value,
                    ),
                  ),
                ),
                Expanded(
                  child: QualityMiniValue(
                    label: 'H',
                    value: controller.formatMeterCompact(hError),
                    color: qualityColorForHorizontalError(
                      context,
                      hError,
                    ),
                    emphasized: true,
                  ),
                ),
                Expanded(
                  child: QualityMiniValue(
                    label: 'V',
                    value: controller.formatMeterCompact(vError),
                    color: qualityColorForVerticalError(
                      context,
                      vError,
                    ),
                    emphasized: true,
                  ),
                ),
              ],
            ),
          ],
        ),
      );
    });
  }

  Color qualityColorForDop(
    BuildContext context,
    double? value,
  ) {
    final colorScheme = Theme.of(context).colorScheme;

    if (value == null || value.isNaN || value.isInfinite) {
      return colorScheme.onSurface.withOpacity(0.45);
    }

    if (value <= 1.0) {
      return Colors.green.shade700;
    }

    if (value <= 2.0) {
      return Colors.lightGreen.shade700;
    }

    if (value <= 5.0) {
      return Colors.orange.shade800;
    }

    return Colors.red.shade700;
  }

  Color qualityColorForHorizontalError(
    BuildContext context,
    double? value,
  ) {
    final colorScheme = Theme.of(context).colorScheme;

    if (value == null || value.isNaN || value.isInfinite) {
      return colorScheme.onSurface.withOpacity(0.45);
    }

    if (value <= 0.03) {
      return Colors.green.shade700;
    }

    if (value <= 0.10) {
      return Colors.lightGreen.shade700;
    }

    if (value <= 0.50) {
      return Colors.orange.shade800;
    }

    return Colors.red.shade700;
  }

  Color qualityColorForVerticalError(
    BuildContext context,
    double? value,
  ) {
    final colorScheme = Theme.of(context).colorScheme;

    if (value == null || value.isNaN || value.isInfinite) {
      return colorScheme.onSurface.withOpacity(0.45);
    }

    if (value <= 0.05) {
      return Colors.green.shade700;
    }

    if (value <= 0.15) {
      return Colors.lightGreen.shade700;
    }

    if (value <= 0.75) {
      return Colors.orange.shade800;
    }

    return Colors.red.shade700;
  }
}
