import 'package:flutter/material.dart';

/// A read-only modal shown when viewing "Room Info" in a finished room
/// 종료된 방에서 "방 정보 보기" 버튼을 누르면 표시되는 읽기전용 모달
class RoomSettingFinishDialog extends StatelessWidget {
  final Map<String, dynamic> roomInfo;  
  // 방 정보

  const RoomSettingFinishDialog({
    Key? key,
    required this.roomInfo,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final media = MediaQuery.of(context);

    final String roomTitle   = (roomInfo['room_title']       ?? '') as String;
    // '방 제목'
    final String roomIntro   = (roomInfo['room_intro']       ?? '') as String;
    // '방 소개'
    final String openYn      = (roomInfo['open_yn']          ?? 'Y') as String;
    // '공개 여부 (Y/N)'
    final int    maxPeople   = (roomInfo['number_of_people'] ?? 0) as int;
    // '최대 인원'
    final int    runningTime = (roomInfo['running_time']     ?? 0) as int;
    // '운영 시간'
    final String tempScoreOpen   = (roomInfo['score_open_range'] ?? 'ALL') as String;
    // '점수 공개 범위'

    var scoreOpen = '';
    // convert 'ALL'/'TEAM'/'PRIVATE' into text
    // 'ALL' => '전체 공개', 'TEAM' => '팀 공개', else => '개인 공개'
    if (tempScoreOpen.toUpperCase() == 'ALL') {
      scoreOpen = 'Public to All'; 
      // '전체 공개'
    } else if (tempScoreOpen.toUpperCase() == 'TEAM') {
      scoreOpen = 'Team Only'; 
      // '팀 공개'
    } else {
      scoreOpen = 'Private'; 
      // '개인 공개'
    }

    final String openLabel   = (openYn == 'Y') ? 'Public' 
      /* '공개' */
      : 'Private' 
      /* '비공개' */;

    return Dialog(
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)),
      child: ConstrainedBox(
        constraints: BoxConstraints(
          maxHeight: media.size.height * 0.8,
          minWidth: media.size.width * 0.8,
        ),
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(20),
          child: Column(
            mainAxisSize: MainAxisSize.min, 
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // (A) Top title (상단 타이틀)
              const Text(
                'Room Info', 
                // '방 정보'
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                  color: Colors.black,
                ),
              ),
              const Divider(color: Colors.black54),
              const SizedBox(height: 12),

              // (B) Room title (방 제목)
              _buildLabelValue(
                label: 'Room Title'
                // '방 제목'
                ,
                value: roomTitle.isNotEmpty ? roomTitle : '(None)'
                // '(없음)'
              ),
              const SizedBox(height: 14),

              // (C) Room introduction (방 소개)
              const Text(
                'Room Description'
                // '방 소개'
                ,
                style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.bold,
                  color: Colors.black87,
                ),
              ),
              const SizedBox(height: 6),
              Container(
                width: double.infinity,
                constraints: const BoxConstraints(maxHeight: 100),
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.black26),
                  borderRadius: BorderRadius.circular(8),
                ),
                padding: const EdgeInsets.all(8),
                child: SingleChildScrollView(
                  child: Text(
                    roomIntro.isNotEmpty 
                        ? roomIntro 
                        : 'No introduction provided.',
                    // '소개글 없음'
                    style: const TextStyle(fontSize: 14, color: Colors.black),
                    softWrap: true,
                  ),
                ),
              ),
              const SizedBox(height: 16),

              // (D) open or private (공개 여부)
              _buildLabelValue(
                label: 'Open or Private'
                // '공개 여부'
                , 
                value: openLabel
              ),
              const SizedBox(height: 8),

              // (E) Max participants (최대 인원)
              _buildLabelValue(
                label: 'Max Participants'
                // '최대 인원'
                , 
                value: '$maxPeople people'
                // '$maxPeople 명'
              ),
              const SizedBox(height: 8),

              // (F) Running time (운영 시간)
              _buildLabelValue(
                label: 'Running Time'
                // '운영 시간'
                , 
                value: '$runningTime min'
                // '$runningTime 분'
              ),
              const SizedBox(height: 8),

              // (G) Score open range (점수 공개 범위)
              _buildLabelValue(
                label: 'Score Visibility'
                // '점수 공개 범위'
                , 
                value: scoreOpen
              ),
              const SizedBox(height: 24),

              // (H) Close button at bottom (하단 닫기 버튼, 가운데 정렬)
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: () => Navigator.pop(context),
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.black,
                      foregroundColor: Colors.white,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                      padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
                    ),
                    child: const Text(
                      'Close'
                      // '닫기'
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// A simple widget that vertically displays a label + value
  /// label + value를 세로로 배치한 간단 위젯
  Widget _buildLabelValue({
    required String label,
    required String value,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: const TextStyle(
            fontSize: 15,
            fontWeight: FontWeight.bold,
            color: Colors.black87,
          ),
        ),
        const SizedBox(height: 4),
        Text(
          value,
          style: const TextStyle(fontSize: 14, color: Colors.black),
        ),
      ],
    );
  }
}
