import 'package:flutter/material.dart';
import 'package:initial_folder/models/qna_model.dart';
import 'package:initial_folder/providers/reply_qna_provider.dart';
import 'package:initial_folder/widgets/reply_qna_user.dart';

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

class ReplyQnaUserPage extends StatefulWidget {
  const ReplyQnaUserPage({
    Key? key,
    required this.idCourse,
    required this.idQna, 
    required this.userId, required this.onReplyDeleted,
  }) : super(key: key);
  final idCourse;
  final String idQna; 
  final int userId;
  final Function(String idRep) onReplyDeleted;
  

  @override
  State<ReplyQnaUserPage> createState() => _ReplyQnaUserPageState();
}

class _ReplyQnaUserPageState extends State<ReplyQnaUserPage> {
  final provider = replyQnaGetIt<ReplyQnaProvider>();

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

    return StreamBuilder<QnaModel>(
        stream: provider.replyQnaStream,
        builder: (context, AsyncSnapshot<QnaModel> 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].comment.length > 0
                //     ? ListView.builder(
                //         itemCount:
                //             snapshot.data!.data[0][widget.index].comment.length,
                //         shrinkWrap: true,
                //         physics: NeverScrollableScrollPhysics(),
                //         itemBuilder: (context, index) {
                //           var comment = snapshot
                //               .data!.data[0][widget.index].comment[index];
                //           var qnauser = snapshot.data!.data[0][widget.index];
                //           return ReplyQnaUser(
                //             divider: Divider(),
                //             comment: comment,
                //             qnaDataModel: qnauser,
                //             userId: widget.userId,
                //           );
                //         })
                //     : Center(
                //         child: Text(
                //           'Belum ada pertanyaan',
                //           style: thirdTextStyle,
                //         ),
                //       );
                // break;
              case ConnectionState.done:
                if (snapshot.hasData && snapshot.data!.data.isNotEmpty) {
                final qnaData = snapshot.data!.data[0];
                return ListView.builder(
                  itemCount: qnaData.length,
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) {
                    var comment = qnaData[index].comment;
                    return Column(
                      children: comment.map((commentItem) {
                        return ReplyQnaUser(
                          divider: Divider(),
                          comment: commentItem,
                          qnaDataModel: qnaData[index],
                          userId: widget.userId,onDeleteReply: widget.onReplyDeleted,
                        );
                      }).toList(),
                    );
                  },
                );
              } else {
                return Center(
                  child: Text(
                    'Belum ada balasan',
                    style: thirdTextStyle,
                  ),
                );
              }
                // break;
            }
          }

          return build;
        });
  }
}
