import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:initial_folder/models/announcement_model.dart';
import 'package:initial_folder/providers/announcement_provider.dart';
import 'package:initial_folder/providers/like_announcement.dart';
import 'package:initial_folder/screens/course/component/inside_announcement.dart';
import 'package:provider/provider.dart';

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

class AnnouncementUser extends StatefulWidget {
  const AnnouncementUser({
    Key? key,
    required this.id,
    this.divider,
    required this.announcementDataModel,
    required this.index,
    required this.userId,
  }) : super(key: key);

  final Widget? divider;
  final id;
  final AnnouncementDataModel announcementDataModel;
  final int index;
  final int userId;

  @override
  State<AnnouncementUser> createState() => _AnnouncementUserState();
}

class _AnnouncementUserState extends State<AnnouncementUser> {
  double value = 0;
  final provider = announcementGetIt<AnnouncementProvider>();

  @override
  Widget build(BuildContext context) {
    LikeOrAnnouncementProvider _likeOrAnnouncementProvider =
        Provider.of<LikeOrAnnouncementProvider>(context);

    likeOrAnnouncement(String tokenAnnouncement) async {
      final provider = announcementGetIt<AnnouncementProvider>();
      if (await _likeOrAnnouncementProvider
          .likeOrAnnouncement(tokenAnnouncement)) {
        provider.getAnnouncement(widget.id);
      }
    }

    return Container(
      margin: EdgeInsets.symmetric(
        horizontal: getProportionateScreenWidth(16),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          InkWell(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => InsideAnnouncement(
                    announcementDataModel: widget.announcementDataModel,
                    id: widget.id,
                    index: widget.index,
                    userId: widget.userId,
                  ),
                ),
              ).then((value) => provider.getAnnouncement(widget.id));
            },
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    CircleAvatar(
                      backgroundColor: primaryColor,
                      backgroundImage:
                          widget.announcementDataModel.fotoProfile == null
                              ? AssetImage("assets/images/Profile Image.png")
                              : NetworkImage(
                                  widget.announcementDataModel.fotoProfile ??
                                      '') as ImageProvider,
                    ),
                    SizedBox(
                      width: getProportionateScreenWidth(8),
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Text(
                              widget.announcementDataModel.instructorName ?? '',
                              style: thirdTextStyle.copyWith(
                                fontSize: getProportionateScreenWidth(12),
                                color:
                                    Theme.of(context).colorScheme.onBackground,
                              ),
                            ),
                          ],
                        ),
                        Text(
                          widget.announcementDataModel.date ?? '',
                          style: thirdTextStyle.copyWith(
                            fontSize: getProportionateScreenWidth(12),
                            color: Theme.of(context).colorScheme.onBackground,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
                SizedBox(height: getProportionateScreenHeight(9)),
                Text(
                  widget.announcementDataModel.bodyContent ?? '',
                  style: thirdTextStyle.copyWith(
                    color: Theme.of(context).colorScheme.onBackground,
                    letterSpacing: 1,
                    fontSize: SizeConfig.blockHorizontal! * 3.4,
                  ),
                ),
                SizedBox(height: getProportionateScreenHeight(5)),
              ],
            ),
          ),
          Row(
            children: [
              kLike(
                  widget.announcementDataModel.isLike == 1
                      ? Icons.favorite
                      : Icons.favorite_border_rounded,
                  widget.announcementDataModel.isLike == 1
                      ? Colors.red
                      : secondaryColor, () {
                likeOrAnnouncement(
                    widget.announcementDataModel.tokenAnnouncement!);
              }),
              SizedBox(
                width: getProportionateScreenWidth(3),
              ),
              Text(
                "${widget.announcementDataModel.countLike ?? 0}",
                style: secondaryTextStyle.copyWith(
                  fontSize: getProportionateScreenWidth(10),
                  letterSpacing: 0.3,
                ),
              ),
              SizedBox(width: getProportionateScreenWidth(13)),
              kComment(widget.announcementDataModel.replies.length),
            ],
          ),
          SizedBox(
            height: getProportionateScreenWidth(13),
          ),
          SizedBox(
            child: widget.divider,
          )
        ],
      ),
    );
  }
}

Widget kLike(IconData icon, Color color, Function onTap) {
  return GestureDetector(
    onTap: () => onTap(),
    child: Row(
      children: [
        Icon(
          icon,
          color: color,
          size: 12,
        ),
      ],
    ),
  );
}

Widget kComment(int value) {
  return Row(
    children: [
      Icon(
        FontAwesomeIcons.comment,
        color: secondaryColor,
        size: 12,
      ),
      SizedBox(
        width: getProportionateScreenWidth(3),
      ),
      Text(
        "$value",
        style: secondaryTextStyle.copyWith(
            fontSize: getProportionateScreenWidth(10), letterSpacing: 0.3),
      )
    ],
  );
}
