import 'package:flutter/material.dart';
import 'package:initial_folder/helper/user_info.dart';
import 'package:initial_folder/models/qna_model.dart';
import 'package:initial_folder/providers/qna_provider.dart';
import 'package:initial_folder/widgets/qna_user.dart';
import '../get_it.dart';

import '../theme.dart';

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

  @override
  State<QnaUserPage> createState() => _QnaUserPageState();
}

class _QnaUserPageState extends State<QnaUserPage> {
  final provider = qnaGetIt<QnaProvider>();
  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.getQna(widget.idCourse);
    late Widget build;

    return StreamBuilder<QnaModel>(
      stream: provider.qnaStream,
      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:
              if (snapshot.data!.data[0].isEmpty) {
                build = Center(
                  child: Text(
                    'Tidak Ada Pertanyaan',
                    style: thirdTextStyle,
                  ),
                );
              } else {
                print('masuk siniiiiiiiiiii active');
                // build = Text(snapshot.data!.status.toString());
                build = ListView.builder(
                  itemCount: snapshot.data!.data[0].length,
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  itemBuilder: (context, index) {
                    return QnaUser(
                      divider: Divider(),
                      qnaDataModel: 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 pertanyaan',
                    style: thirdTextStyle,
                  ),
                );
              } else {
                print('masuk siniiiiiiiiiii done');

                build = ListView.builder(
                  itemCount: snapshot.data!.data[0].length,
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  itemBuilder: (context, index) {
                    return QnaUser(
                      divider: Divider(
                        thickness: 3.0,
                      ),
                      qnaDataModel: snapshot.data!.data[0][index],
                      id: widget.idCourse,
                      index: index,
                      userId: userId!,
                    );
                  },
                );
              }
              break;
          }
        }

        return build;
      },
    );
  }
}
