import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:screen_brightness/screen_brightness.dart';

import 'package:mini_tias/providers/camera_provider.dart';
import 'package:mini_tias/providers/gallery_provider.dart';
import 'package:mini_tias/screens/capture_screen.dart';
import 'package:mini_tias/screens/gallery_screen.dart';

/// BottomNavigationBar で撮影画面と一覧画面を切り替えるホーム画面．
///
/// アタッチメントで隠れる下部領域を避け，上部の見える領域に UI を配置する．
/// ※ 180° 回転は app.dart で一括適用しているため，ここでは不要．
class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _currentIndex = 0;
  double _brightness = 0.8;

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

  @override
  void dispose() {
    ScreenBrightness.instance.resetApplicationScreenBrightness();
    super.dispose();
  }

  Future<void> _setBrightness(double value) async {
    await ScreenBrightness.instance.setApplicationScreenBrightness(value);
  }

  @override
  Widget build(BuildContext context) {
    final screenHeight = MediaQuery.of(context).size.height;
    final attachmentPadding = screenHeight / 3;
    final cameraProvider = context.watch<CameraProvider>();

    return Scaffold(
      backgroundColor: Colors.black,
      body: Column(
        children: [
          // ナビゲーションバー（保存中はタップ無効）
          AbsorbPointer(
            absorbing: cameraProvider.isSaving,
            child: BottomNavigationBar(
              currentIndex: _currentIndex,
              onTap: (index) {
                setState(() => _currentIndex = index);
                if (index == 1) {
                  context.read<GalleryProvider>().loadImages();
                }
              },
              backgroundColor: Colors.black87,
              selectedItemColor: Colors.white,
              unselectedItemColor: Colors.white60,
              items: const [
                BottomNavigationBarItem(
                  icon: Icon(Icons.camera_alt),
                  label: '撮影',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.photo_library),
                  label: '一覧',
                ),
              ],
            ),
          ),

          // 明るさスライダー
          Container(
            color: Colors.black54,
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: Row(
              children: [
                const Icon(Icons.brightness_low, size: 20, color: Colors.white),
                Expanded(
                  child: Slider(
                    value: _brightness,
                    onChanged: (value) {
                      setState(() => _brightness = value);
                      _setBrightness(value);
                    },
                  ),
                ),
                const Icon(
                  Icons.brightness_high,
                  size: 20,
                  color: Colors.white,
                ),
              ],
            ),
          ),

          // メインコンテンツ（カメラプレビュー or 一覧）
          Expanded(
            child: _currentIndex == 0
                ? const CaptureScreen()
                : const GalleryScreen(),
          ),

          // アタッチメントで隠れる領域（空白）
          SizedBox(height: attachmentPadding),
        ],
      ),
    );
  }
}
