import 'package:flutter/material.dart';
import 'package:initial_folder/models/announcement_model.dart';
import 'package:initial_folder/providers/reply_announcement_provider.dart';
import 'package:initial_folder/widgets/reply_announcement_user.dart';

import '../get_it.dart';
import '../theme.dart';

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

  @override
  State<ReplyAnnouncementUserPage> createState() =>
      _ReplyAnnouncementUserPageState();
}

class _ReplyAnnouncementUserPageState extends State<ReplyAnnouncementUserPage> {
  final provider = replyAnnouncementGetIt<ReplyAnnouncementProvider>();

  @override
  Widget build(BuildContext context) {
    provider.getReplyAnnouncement(widget.idCourse, widget.index);
    late Widget build;

    return StreamBuilder<AnnouncementModel>(
      stream: provider.replyAnnouncementStream,
      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:
              build = snapshot.data!.data[0][widget.index].replies.length > 0
                  ? ListView.builder(
                      itemCount:
                          snapshot.data!.data[0][widget.index].replies.length,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemBuilder: (context, index) {
                        var replyModel =
                            snapshot.data!.data[0][widget.index].replies[index];
                        var announcementuser =
                            snapshot.data!.data[0][widget.index];
                        return ReplyAnnouncementUser(
                          divider: Divider(),
                          replyModel: replyModel,
                          announcementDataModel: announcementuser,
                          userId: widget.userId,
                        );
                      })
                  : Center(
                      child: Text(
                        'Belum ada pengumuman',
                        style: thirdTextStyle,
                      ),
                    );
              break;
            case ConnectionState.done:
              build = snapshot.data!.data[0][widget.index].replies.length > 0
                  ? ListView.builder(
                      itemCount:
                          snapshot.data!.data[0][widget.index].replies.length,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemBuilder: (context, index) {
                        var replyModel =
                            snapshot.data!.data[0][widget.index].replies[index];
                        var announcementuser =
                            snapshot.data!.data[0][widget.index];
                        return ReplyAnnouncementUser(
                          divider: Divider(),
                          replyModel: replyModel,
                          announcementDataModel: announcementuser,
                          userId: widget.userId,
                        );
                      })
                  : Center(
                      child: Text(
                        'Belum ada pertanyaan',
                        style: thirdTextStyle,
                      ),
                    );
              break;
          }
        }
        return build;
      },
    );
  }
}
