import 'package:flutter/material.dart';
import 'room_search_list_page.dart';

// Config (설정)
import '../../config/config.dart';

// Ads (광고)
import '../../plugins/admob.dart';

class RoomSearchHomePage extends StatefulWidget {
  const RoomSearchHomePage({Key? key}) : super(key: key);

  @override
  State<RoomSearchHomePage> createState() => _RoomSearchHomePageState();
}

class _RoomSearchHomePageState extends State<RoomSearchHomePage> {
  // Adjust font size by screen width (화면 크기에 따라 폰트 크기 조절)
  double scaleFactor = 1.0;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _updateScaleFactor();
  }

  // Adjust font size by screen width (화면 크기에 따라 폰트 크기 조절)
  void _updateScaleFactor() {
    final screenWidth = MediaQuery.of(context).size.width;
    const baseWidth = 450.0;
    setState(() {
      scaleFactor = (screenWidth / baseWidth).clamp(0.8, 1.2);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,

      appBar: AppBar(
        backgroundColor: Colors.black,
        elevation: 0,
        title: const Text(
          'Room Search', // '방 검색'
          style: TextStyle(color: Colors.white),
        ),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
          onPressed: () => Navigator.pop(context),
        ),
      ),

      // Bottom ad banner (화면 하단 광고 영역)
      bottomNavigationBar: AdBannerWidget(),

      // Main body: 3 buttons in the center (대기중 / 진행중 / 종료)
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            _buildSearchStatusButton(context, label: 'Waiting', status: 'WAIT'), 
            // '대기중'
            const SizedBox(width: 16),
            _buildSearchStatusButton(context, label: 'Running', status: 'RUNNING'), 
            // '진행중'
            const SizedBox(width: 16),
            _buildSearchStatusButton(context, label: 'Finished', status: 'FINISH'), 
            // '종료'
          ],
        ),
      ),
    );
  }

  /// Build a button with same size (동일한 크기로 버튼)
  Widget _buildSearchStatusButton(
    BuildContext context, {
    required String label,
    required String status,
  }) {
    return SizedBox(
      width: 100 * scaleFactor,
      height: 100 * scaleFactor,
      child: ElevatedButton(
        onPressed: () {
          // Navigate to RoomSearchListPage with roomStatus (RoomSearchListPage로 이동하며 roomStatus 전달)
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (_) => RoomSearchListPage(roomStatus: status),
            ),
          );
        },
        style: ElevatedButton.styleFrom(
          backgroundColor: Colors.white,
          foregroundColor: Colors.black,
          side: const BorderSide(color: Colors.black, width: 1),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
          padding: EdgeInsets.zero,
        ),
        child: Text(
          label,
          style: const TextStyle(color: Colors.black),
        ),
      ),
    );
  }
}
