import 'package:flutter/material.dart';

import '../../../../core/constants/app_colors.dart';
import '../../../../core/constants/app_typography.dart';
import '../../../services/restaurant_form_validator.dart';

class FetchedRestaurantJsonView extends StatelessWidget {
  final bool isDark;
  final TextEditingController nameController;
  final TextEditingController categoryController;
  final TextEditingController subCategoryController;
  final TextEditingController descriptionController;
  final TextEditingController phoneController;
  final TextEditingController roadAddressController;
  final TextEditingController jibunAddressController;
  final TextEditingController latitudeController;
  final TextEditingController longitudeController;
  final TextEditingController naverUrlController;
  final ValueChanged<String> onFieldChanged;

  const FetchedRestaurantJsonView({
    super.key,
    required this.isDark,
    required this.nameController,
    required this.categoryController,
    required this.subCategoryController,
    required this.descriptionController,
    required this.phoneController,
    required this.roadAddressController,
    required this.jibunAddressController,
    required this.latitudeController,
    required this.longitudeController,
    required this.naverUrlController,
    required this.onFieldChanged,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: isDark
            ? AppColors.darkBackground
            : AppColors.lightBackground.withOpacity(0.5),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(
          color: isDark ? AppColors.darkDivider : AppColors.lightDivider,
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              const Icon(Icons.code, size: 18),
              const SizedBox(width: 8),
              Text(
                '가져온 정보',
                style: AppTypography.body1(
                  isDark,
                ).copyWith(fontWeight: FontWeight.w600),
              ),
            ],
          ),
          const SizedBox(height: 12),
          const Text(
            '{',
            style: TextStyle(fontFamily: 'RobotoMono', fontSize: 16),
          ),
          const SizedBox(height: 12),
          _buildJsonField(
            context,
            label: 'name',
            controller: nameController,
            icon: Icons.store,
            validator: (value) =>
                value == null || value.isEmpty ? '가게 이름을 입력해주세요' : null,
          ),
          _buildJsonField(
            context,
            label: 'category',
            controller: categoryController,
            icon: Icons.category,
            validator: RestaurantFormValidator.validateCategory,
          ),
          _buildJsonField(
            context,
            label: 'subCategory',
            controller: subCategoryController,
            icon: Icons.label_outline,
          ),
          _buildJsonField(
            context,
            label: 'description',
            controller: descriptionController,
            icon: Icons.description,
            maxLines: 2,
          ),
          _buildJsonField(
            context,
            label: 'phoneNumber',
            controller: phoneController,
            icon: Icons.phone,
            keyboardType: TextInputType.phone,
            validator: RestaurantFormValidator.validatePhoneNumber,
          ),
          _buildJsonField(
            context,
            label: 'roadAddress',
            controller: roadAddressController,
            icon: Icons.location_on,
            validator: RestaurantFormValidator.validateAddress,
          ),
          _buildJsonField(
            context,
            label: 'jibunAddress',
            controller: jibunAddressController,
            icon: Icons.map,
          ),
          _buildCoordinateFields(context),
          _buildJsonField(
            context,
            label: 'naverUrl',
            controller: naverUrlController,
            icon: Icons.link,
            monospace: true,
          ),
          const SizedBox(height: 12),
          const Text(
            '}',
            style: TextStyle(fontFamily: 'RobotoMono', fontSize: 16),
          ),
        ],
      ),
    );
  }

  Widget _buildCoordinateFields(BuildContext context) {
    final border = OutlineInputBorder(borderRadius: BorderRadius.circular(8));

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: const [
            Icon(Icons.my_location, size: 16),
            SizedBox(width: 8),
            Text('coordinates'),
          ],
        ),
        const SizedBox(height: 6),
        Row(
          children: [
            Expanded(
              child: TextFormField(
                controller: latitudeController,
                keyboardType: const TextInputType.numberWithOptions(
                  decimal: true,
                ),
                decoration: InputDecoration(
                  labelText: 'latitude',
                  border: border,
                  isDense: true,
                ),
                onChanged: onFieldChanged,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '위도를 입력해주세요';
                  }
                  final latitude = double.tryParse(value);
                  if (latitude == null || latitude < -90 || latitude > 90) {
                    return '올바른 위도값을 입력해주세요';
                  }
                  return null;
                },
              ),
            ),
            const SizedBox(width: 12),
            Expanded(
              child: TextFormField(
                controller: longitudeController,
                keyboardType: const TextInputType.numberWithOptions(
                  decimal: true,
                ),
                decoration: InputDecoration(
                  labelText: 'longitude',
                  border: border,
                  isDense: true,
                ),
                onChanged: onFieldChanged,
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '경도를 입력해주세요';
                  }
                  final longitude = double.tryParse(value);
                  if (longitude == null ||
                      longitude < -180 ||
                      longitude > 180) {
                    return '올바른 경도값을 입력해주세요';
                  }
                  return null;
                },
              ),
            ),
          ],
        ),
        const SizedBox(height: 12),
      ],
    );
  }

  Widget _buildJsonField(
    BuildContext context, {
    required String label,
    required TextEditingController controller,
    required IconData icon,
    int maxLines = 1,
    TextInputType? keyboardType,
    bool monospace = false,
    String? Function(String?)? validator,
  }) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Icon(icon, size: 16),
              const SizedBox(width: 8),
              Text('$label:'),
            ],
          ),
          const SizedBox(height: 6),
          TextFormField(
            controller: controller,
            maxLines: maxLines,
            keyboardType: keyboardType,
            onChanged: onFieldChanged,
            validator: validator,
            style: monospace
                ? const TextStyle(fontFamily: 'RobotoMono', fontSize: 13)
                : null,
            decoration: InputDecoration(
              isDense: true,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
