import 'package:flutter/material.dart';
import 'package:initial_folder/helper/user_info.dart';
import 'package:initial_folder/models/announcement_model.dart';
import 'package:initial_folder/providers/announcement_provider.dart';
import 'package:initial_folder/widgets/announcement_user.dart';
import '../get_it.dart';
import '../theme.dart';

class AnnouncementUserPage extends StatefulWidget {
  const AnnouncementUserPage({Key? key, required this.idCourse})
      : super(key: key);
  final idCourse;

  @override
  State<AnnouncementUserPage> createState() => _AnnouncementUserPageState();
}

class _AnnouncementUserPageState extends State<AnnouncementUserPage> {
  final provider = announcementGetIt<AnnouncementProvider>();
  int? userId = 0;

  void getUserId() async {
    userId = await UsersInfo().getIdUser();
  }

  @override
  void initState() {
    // TODO: implement initState
    getUserId();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    provider.getAnnouncement(widget.idCourse);
    late Widget build;

    return StreamBuilder<AnnouncementModel>(
      stream: provider.announcementStream,
      builder: (context, AsyncSnapshot<AnnouncementModel> snapshot) {
        if (snapshot.hasError) {
          return Center(
            child: Text(
              'Terjadi Kesalahan',
              style: thirdTextStyle,
            ),
          );
        } else {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              build = Center(
                child: CircularProgressIndicator(
                  color: primaryColor,
                  strokeWidth: 2,
                ),
              );
              break;
            case ConnectionState.none:
              build = Center(
                child: Text(
                  'Tidak ada koneksi',
                  style: thirdTextStyle,
                ),
              );
              break;
            case ConnectionState.active:
              if (snapshot.data!.data[0].isEmpty) {
                build = Center(
                  child: Text(
                    'Tidak Ada Pengumuman',
                    style: thirdTextStyle,
                  ),
                );
              } else {
                build = ListView.builder(
                  itemCount: snapshot.data!.data[0].length,
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  itemBuilder: (context, index) {
                    return AnnouncementUser(
                      divider: Divider(),
                      announcementDataModel: snapshot.data!.data[0][index],
                      id: widget.idCourse,
                      index: index,
                      userId: userId!,
                    );
                  },
                );
              }
              break;
            case ConnectionState.done:
              if (snapshot.data!.data[0].isEmpty) {
                build = Center(
                  child: Text(
                    'Belum ada pengumuman',
                    style: thirdTextStyle,
                  ),
                );
              } else {
                build = ListView.builder(
                  itemCount: snapshot.data!.data[0].length,
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  itemBuilder: (context, index) {
                    return AnnouncementUser(
                      divider: Divider(),
                      announcementDataModel: snapshot.data!.data[0][index],
                      id: widget.idCourse,
                      index: index,
                      userId: userId!,
                    );
                  },
                );
              }
              break;
          }
        }
        return build;
      },
    );
  }
}
