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

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.userName}) : super(key: key);

  final String userName;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<void> _signOut() async {
    await _auth.signOut();
    Navigator.pushReplacementNamed(context, '/login');
  }

  Future<void> _confirmSignOut() async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Confirm Sign Out'),
          content: const Text('Are you sure you want to sign out?'),
          actions: <Widget>[
            TextButton(
              child: const Text('Cancel'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: const Text('Yes'),
              onPressed: () {
                Navigator.of(context).pop();
                _signOut();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.brown,
        title: Text(
          'Welcome, ${widget.userName}',
          style: TextStyle(
            fontFamily: 'Quicksand',
          ),
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.logout),
            onPressed: _confirmSignOut,
          ),
        ],
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton.icon(
              onPressed: () {
                Navigator.pushReplacementNamed(context, '/monitoringSensor');
              },
              icon: const Icon(Icons.monitor),
              label: const Text('Monitoring Sensor'),
              style: ElevatedButton.styleFrom(
                minimumSize: const Size(260, 70), // lebar: 260, tinggi: 70
                backgroundColor: Colors.brown[100],
                elevation: 8, // Tambahkan properti elevation untuk bayangan
                shadowColor: Colors.brown, // Warna bayangan
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton.icon(
              onPressed: () {
                Navigator.pushReplacementNamed(context, '/historiData');
              },
              icon: const Icon(Icons.history),
              label: const Text('Histori Data'),
              style: ElevatedButton.styleFrom(
                minimumSize: const Size(260, 70), // lebar: 260, tinggi: 70
                backgroundColor: Colors.brown[100],
                elevation: 8, // Tambahkan properti elevation untuk bayangan
                shadowColor: Colors.brown, // Warna bayangan
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton.icon(
              onPressed: () {
                Navigator.pushReplacementNamed(context, '/modeKontrol');
              },
              icon: const Icon(Icons.settings),
              label: const Text('Mode Kontrol'),
              style: ElevatedButton.styleFrom(
                minimumSize: const Size(260, 70), // lebar: 260, tinggi: 70
                backgroundColor: Colors.brown[100],
                elevation: 8, // Tambahkan properti elevation untuk bayangan
                shadowColor: Colors.brown, // Warna bayangan
              ),
            ),
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        selectedItemColor:
            Colors.brown, // Warna yang akan digunakan untuk ikon yang dipilih
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              'assets/images/logo.png', // Ganti dengan path gambar yang sesuai
              height: 40, // Sesuaikan tinggi gambar
            ),
            label: '', // Biarkan label kosong agar gambar berada di tengah
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.article),
            label: 'Artikel',
          ),
        ],
        onTap: (int index) {
          switch (index) {
            case 0:
              // Navigasi ke halaman beranda
              Navigator.pushReplacementNamed(context, '/home');
              break;
            case 1:
              // Tidak ada tindakan khusus karena gambar di tengah tidak mempunyai fungsi navigasi
              break;
            case 2:
              // Navigasi ke halaman artikel
              Navigator.pushReplacementNamed(context, '/artikel');
              break;
          }
        },
      ),
    );
  }
}
