import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../models/maps/prefecture_tile_layout.dart';
import '../../models/maps/japan_map_data.dart';
import '../../theme/app_colors.dart';
import '../../providers/ui_experiment_provider.dart';

class PrefectureTileMap extends ConsumerWidget {
  final Set<String> visitedPrefectures;
  final Function(String) onPrefectureTap;
  final double tileSize;
  final double gap;

  const PrefectureTileMap({
    super.key,
    required this.visitedPrefectures,
    required this.onPrefectureTap,
    this.tileSize = 46.0,
    this.gap = 4.0,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final isMapColorful = ref.watch(uiExperimentProvider).isMapColorful;

    // 1. Determine Grid Bounds
    int maxCol = 0;
    int maxRow = 0;
    PrefectureTileLayout.getLayout.forEach((_, pos) {
      if (pos.col + pos.width > maxCol) maxCol = pos.col + pos.width;
      if (pos.row + pos.height > maxRow) maxRow = pos.row + pos.height;
    });

    final double totalWidth = maxCol * (tileSize + gap) - gap;
    final double totalHeight = maxRow * (tileSize + gap) - gap;

    return SizedBox(
      width: totalWidth,
      height: totalHeight,
      child: Stack(
        children: PrefectureTileLayout.getLayout.entries.map((entry) {
          final prefName = entry.key;
          final pos = entry.value;
          final isVisited = visitedPrefectures.contains(prefName) || 
                            visitedPrefectures.any((v) => v.startsWith(prefName)); // Robust check

          return Positioned(
            left: pos.col * (tileSize + gap),
            top: pos.row * (tileSize + gap),
            width: pos.width * tileSize + (pos.width - 1) * gap,
            height: pos.height * tileSize + (pos.height - 1) * gap,
            child: _buildTile(context, prefName, isVisited, isMapColorful),
          );
        }).toList(),
      ),
    );
  }

  static const Map<int, Color> regionColors = {
    1: Color(0xFF6A7FF0), // Hokkaido: Blue
    2: Color(0xFF58C2F1), // Tohoku: Cyan
    3: Color(0xFF39D353), // Kanto: Green
    4: Color(0xFF36D3AD), // Chubu: Emerald
    5: Color(0xFFAEDA38), // Kinki: YellowGreen
    6: Color(0xFFE6D33C), // Chugoku: Yellow
    7: Color(0xFFF9A057), // Shikoku: Orange
    8: Color(0xFFFF7B7B), // Kyushu: Pink
  };

  Widget _buildTile(BuildContext context, String prefName, bool isVisited, bool isColorful) {
    final appColors = Theme.of(context).extension<AppColors>()!;

    // Find Region ID for coloring
    int prefId = 0;
    JapanMapData.prefectureNames.forEach((k, v) {
      if (v.contains(prefName)) prefId = k;
    });
    final regionId = JapanMapData.getRegionId(prefId);
    final regionColor = regionColors[regionId] ?? Colors.grey;

    Color baseColor;
    Color textColor;
    Color borderColor;
    BoxBorder? border; // Use BoxBorder specifically

    if (isColorful) {
      // --- Colorful Mode ---
      if (isVisited) {
        baseColor = regionColor;
        textColor = appColors.surfaceSubtle;
        borderColor = Colors.transparent; // No border needed for filled
        border = null;
      } else {
        baseColor = appColors.surfaceSubtle;
        textColor = appColors.textTertiary;
        borderColor = regionColor.withValues(alpha: 0.6);
        border = Border.all(color: borderColor, width: 2);
      }
    } else {
      // --- Classic Mode ---
      baseColor = isVisited ? appColors.brandPrimary : appColors.surfaceSubtle;
      textColor = isVisited ? appColors.surfaceSubtle : appColors.textSecondary;
      borderColor = appColors.divider;
      border = Border.all(color: borderColor, width: 1); // Subtle inner border
    }

    return Material(
      color: baseColor,
      borderRadius: BorderRadius.circular(6), // Rounded corners for "Block" look
      child: InkWell(
        onTap: () => onPrefectureTap(prefName),
        borderRadius: BorderRadius.circular(6),
        child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(6),
            border: border, 
          ),
          child: Text(
            prefName,
            style: Theme.of(context).textTheme.labelSmall?.copyWith(
              color: textColor,
              fontSize: 12, // Small but legible
              fontWeight: FontWeight.bold,
            ),
            textAlign: TextAlign.center,
          ),
        ),
      ),
    );
  }
}
