import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:jema_app/addclassroom.dart';
import 'package:jema_app/childrenProfile.dart';
import 'package:jema_app/classroomview.dart';
import 'package:jema_app/games.dart';
import 'package:jema_app/teacherchat.dart';

import 'ChatScreen.dart';
import 'TeacherProfile.dart';

class ParentHomePage extends StatelessWidget {
  late User? user;
  late final Stream<QuerySnapshot> _usersStream;
  late String phoneNo;
  late String role;

  ParentHomePage(param0, role) {
    this.role = role;
    this.phoneNo = param0;
    user = FirebaseAuth.instance.currentUser;
    initializeStreams();
  }

  void initializeStreams() {
    _usersStream = FirebaseFirestore.instance
        .collection('students')
        .where('Parentphone', isEqualTo: phoneNo)
        .snapshots();
  }

  final TextStyle poppinsTextStyle = TextStyle(
    fontFamily: 'Poppins',
  );

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        return false;
      },
      child: Scaffold(
        appBar: AppBar(
          actions: [
            IconButton(
              icon: Icon(Icons.notifications),
              onPressed: () {},
            ),
          ],
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(height: 20),
              Text(
                'Explore, learn, and play in our magical world of wonder!',
                style: poppinsTextStyle.copyWith(
                  fontSize: 13,
                ),
              ),
              SizedBox(height: 10),
              Expanded(
                child: Image.asset(
                  'lib/assets/teacherhome.png',
                  fit: BoxFit.contain,
                ),
              ),
              SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  TextButton(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => AddClassroomPage()),
                      );
                    },
                    child: Row(
                      children: [],
                    ),
                  ),
                ],
              ),
              Divider(),
              SizedBox(height: 10),
              Text(
                'All Kids',
                style: poppinsTextStyle.copyWith(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(height: 12),
              Expanded(
                child: StreamBuilder<QuerySnapshot>(
                  stream: _usersStream,
                  builder: (context, snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return CircularProgressIndicator();
                    }

                    if (snapshot.hasError) {
                      return Text('Error: ${snapshot.error}');
                    }

                    return ListView.builder(
                      itemCount: snapshot.data!.docs.length,
                      itemBuilder: (context, index) {
                        var document = snapshot.data!.docs[index];
                        var className = document['className'];
                        var name = document['Firstname'];

                        return Padding(
                          padding: const EdgeInsets.only(bottom: 8.0),
                          child: ListTile(
                            leading: CircleAvatar(
                              radius: 27.0,
                              backgroundImage:
                                  AssetImage('lib/assets/classlogo.png'),
                            ),
                            title: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    Text(
                                      '$name',
                                      style: poppinsTextStyle.copyWith(
                                          fontWeight: FontWeight.bold),
                                    ),
                                  ],
                                ),
                                Row(
                                  children: [
                                    Text(
                                      '$className',
                                      style: poppinsTextStyle.copyWith(
                                          fontSize: 12.0),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                            trailing: Container(
                              height: 35.0,
                              width: 80.0,
                              decoration: BoxDecoration(
                                color: Color.fromARGB(203, 164, 88, 177),
                                borderRadius: BorderRadius.circular(4.0),
                              ),
                              child: TextButton(
                                onPressed: () {
                                  Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                      builder: (context) => ChildrenProfilePage(
                                        document,
                                      ),
                                    ),
                                  );
                                },
                                child: Text(
                                  'View',
                                  style: poppinsTextStyle.copyWith(
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        );
                      },
                    );
                  },
                ),
              ),
            ],
          ),
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: const [
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: 'Home',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.chat),
              label: 'Chat',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.videogame_asset),
              label: 'Games',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.person),
              label: 'Profile',
            ),
          ],
          selectedItemColor: Color(0xFF7A1FA0),
          unselectedItemColor: Color(0xFFA9ABAD),
          selectedLabelStyle: TextStyle(color: Color(0xFF7A1FA0)),
          unselectedLabelStyle:
              TextStyle(color: Color.fromARGB(0, 197, 16, 16)),
          currentIndex: 0,
          onTap: (index) {
            switch (index) {
              case 0:
                break;
              case 1:
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => ChatScreen()),
                );
                break;
              case 2:
                // Handle Game
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => GamesPage(),
                  ),
                );
                break;
              case 3:
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => TeacherProfilePage(
                      uid: user?.uid,
                    ),
                  ),
                );
                break;
            }
          },
        ),
      ),
    );
  }
}
