import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:initial_folder/main.dart';
import 'package:initial_folder/models/comment_qna_model.dart';
import 'package:initial_folder/models/qna_model.dart';
import 'package:initial_folder/providers/posting_qna_reply_provider.dart';
import 'package:initial_folder/screens/course/component/detail_quest_and_answer.dart';
import 'package:initial_folder/widgets/edit_reply_qna_user.dart';
import 'package:provider/provider.dart';

import '../size_config.dart';
import '../theme.dart';

class ReplyQnaUser extends StatefulWidget {
  const ReplyQnaUser(
      {Key? key,
      required this.qnaDataModel,
      required this.divider,
      required this.comment,
      required this.userId,
      required this.onDeleteReply,})
      : super(key: key);

  final Widget? divider;
  final Comment comment;
  final int userId;
  final QnaDataModel qnaDataModel;
  final Function(String idRep) onDeleteReply;
  @override
  State<ReplyQnaUser> createState() => _ReplyQnaUserState();
}

class _ReplyQnaUserState extends State<ReplyQnaUser> {
  @override
  Widget build(BuildContext context) {
    PostingQnaReplyProvider deleteReplyQnaProvider =
        Provider.of<PostingQnaReplyProvider>(context);

   deleteReplyQna() async {
  if (await deleteReplyQnaProvider.deleteReplyQna(int.parse(widget.comment.idRep!))) {
    // Notify the parent widget about the deletion
    widget.onDeleteReply(widget.comment.idRep!);

    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        duration: Duration(seconds: 2),
        backgroundColor: primaryColor,
        content: Text(
          'Balasan berhasil dihapus',
          style: primaryTextStyle.copyWith(
            color: Colors.white,
          ),
        ),
        behavior: SnackBarBehavior.floating,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10),
        ),
      ),
    );
  } else {
    // Handle the error case
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        duration: Duration(seconds: 2),
        backgroundColor: primaryColor,
        content: Text(
          'Terjadi kesalahan',
          style: primaryTextStyle.copyWith(
            color: backgroundColor,
          ),
        ),
        behavior: SnackBarBehavior.floating,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5),
        ),
      ),
    );
  }
}


    return Container(
      margin: EdgeInsets.symmetric(
        horizontal: getProportionateScreenWidth(16),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: 6,
              ),
              Row(
                children: [
                  CircleAvatar(
                    backgroundColor: primaryColor,
                    backgroundImage: widget.comment.fotoProfile == null
                        ? AssetImage("assets/images/Profile Image.png")
                        : NetworkImage(widget.comment.fotoProfile ?? '')
                            as ImageProvider,
                  ),
                  SizedBox(
                    width: getProportionateScreenWidth(8),
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          InkWell(
                            onTap: () {
                              print('Print ${widget.qnaDataModel.idQna}');
                            },
                            child: Text(
                              widget.comment.username ?? '',
                              style: thirdTextStyle.copyWith(
                                  fontSize: getProportionateScreenWidth(12),
                                  color: Theme.of(context)
                                      .colorScheme
                                      .onBackground),
                            ),
                          ),
                        ],
                      ),
                      Text(
                        widget.comment.createAt ?? '',
                        style: thirdTextStyle.copyWith(
                            fontSize: getProportionateScreenWidth(12),
                            color: Theme.of(context).colorScheme.onBackground),
                      ),
                    ],
                  ),
                  int.parse(widget.comment.sender.toString()) == widget.userId
                      ? Expanded(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              PopupMenuButton(
                                child: Padding(
                                  padding: EdgeInsets.only(right: 10),
                                  child: Icon(
                                    Icons.more_vert,
                                    color: Theme.of(context)
                                        .colorScheme
                                        .onBackground,
                                  ),
                                ),
                                itemBuilder: (context) => [
                                  PopupMenuItem(
                                    child: Container(
                                      child: Text('Edit'),
                                    ),
                                    value: 'edit',
                                  ),
                                  PopupMenuItem(
                                    child: Text('Hapus'),
                                    value: 'hapus',
                                  ),
                                ],
                                onSelected: (value) {
                                  switch (value) {
                                    case 'edit':
                                      Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                          builder: (context) => EditReplyQna(
                                            id_qna: widget.qnaDataModel.idQna,
                                            text_rep: widget.comment.textRep,
                                            id_rep: widget.comment.idRep,
                                          ),
                                        ),
                                      );
                                      break;
                                    case 'hapus':
                                      print(widget.comment.idRep);
                                      deleteReplyQna();
                                      break;
                                  }
                                },
                              ),
                            ],
                          ),
                        )
                      : SizedBox(
                          height: 12,
                        ),
                ],
              ),
              SizedBox(height: getProportionateScreenHeight(8)),
              Html(
                data: widget.comment.textRep ?? '',
                style: {
                  "*": Style(margin: Margins.zero),
                },
              ),
              // Text(
              //   widget.comment.textRep ?? '',
              //   style: thirdTextStyle.copyWith(
              //       color: Theme.of(context).colorScheme.onBackground,
              //       letterSpacing: 1,
              //       fontSize: SizeConfig.blockHorizontal! * 3.4),
              // ),
              SizedBox(height: getProportionateScreenHeight(6)),
            ],
          ),
          Divider(
            thickness: 0.3,
          )
        ],
      ),
    );
  }
}
