import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:mohem_flutter_app/main.dart';
import 'package:mohem_flutter_app/provider/chat_provider_model.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/ui/chat/chat_detailed_screen.dart';
import 'package:mohem_flutter_app/widgets/circular_avatar.dart';
import 'package:mohem_flutter_app/widgets/shimmer/dashboard_shimmer_widget.dart';
import 'package:provider/provider.dart';

class ChatFavoriteUsersScreen extends StatelessWidget {
  const ChatFavoriteUsersScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColors.white,
      body: Consumer<ChatProviderModel>(
        builder: (BuildContext context, ChatProviderModel m, Widget? child) {
          if (m.isLoading) {
            return ChatHomeShimmer(
              isDetailedScreen: false,
            );
          } else {
            return m.favUsersList != null && m.favUsersList.isNotEmpty
                ? ListView.separated(
                    itemCount: m.favUsersList!.length,
                    shrinkWrap: true,
                    physics: const ClampingScrollPhysics(),
                    padding: const EdgeInsets.only(bottom: 80.0),
                    itemBuilder: (BuildContext context, int index) {
                      return SizedBox(
                        height: 55,
                        child: Row(
                          children: [
                            Stack(
                              children: <Widget>[
                                if (m.favUsersList![index].isImageLoading!)
                                  const SizedBox(
                                    height: 48,
                                    width: 48,
                                  ).toShimmer().circle(30),
                                if (!m.favUsersList![index].isImageLoading! && m.favUsersList![index].userLocalDownlaodedImage == null)
                                  SvgPicture.asset(
                                    "assets/images/user.svg",
                                    height: 48,
                                    width: 48,
                                  ),
                                if (!m.favUsersList![index].isImageLoading! && m.favUsersList![index].userLocalDownlaodedImage != null)
                                  Container(
                                    width: 48.0,
                                    height: 48.0,
                                    decoration: BoxDecoration(
                                      shape: BoxShape.circle,
                                      image: DecorationImage(
                                        fit: BoxFit.cover,
                                        image: FileImage(m.favUsersList![index].userLocalDownlaodedImage!),
                                      ),
                                    ),
                                  ),
                                Positioned(
                                  right: 5,
                                  bottom: 1,
                                  child: Container(
                                    width: 10,
                                    height: 10,
                                    decoration: BoxDecoration(
                                      color: m.favUsersList![index].userStatus == 1 ? MyColors.green2DColor : Colors.red,
                                    ),
                                  ).circle(10),
                                )
                              ],
                            ),
                            Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                (m.favUsersList![index].userName!.replaceFirst(".", " ").capitalizeFirstofEach ?? "").toText14(color: MyColors.darkTextColor).paddingOnly(left: 11, top: 13),
                              ],
                            ).expanded,
                            SizedBox(
                              width: 60,
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                mainAxisAlignment: MainAxisAlignment.end,
                                mainAxisSize: MainAxisSize.max,
                                children: <Widget>[
                                  Icon(
                                    m.favUsersList![index].isFav! ? Icons.star : Icons.star_border,
                                    color: m.favUsersList![index].isFav! ? MyColors.yellowColor : MyColors.grey35Color,
                                  ).onPress(() {
                                    if (m.favUsersList![index].isFav!) {
                                      m.unFavoriteUser(
                                        userID: AppState().chatDetails!.response!.id!,
                                        targetUserID: m.favUsersList![index].id!,
                                      );
                                    }
                                  }).center,
                                ],
                              ),
                            ),
                          ],
                        ),
                      ).onPress(() {
                        Navigator.pushNamed(
                          context,
                          AppRoutes.chatDetailed,
                          arguments: ChatDetailedScreenParams(m.favUsersList![index], true),
                        ).then(
                          (Object? value) {
                            m.clearSelections();
                          },
                        );
                      });
                    },
                    separatorBuilder: (BuildContext context, int index) => const Divider(color: MyColors.lightGreyE5Color).paddingOnly(left: 70),
                  ).paddingAll(21)
                : Column(
                    children: <Widget>[
                      Utils.getNoDataWidget(context).expanded,
                    ],
                  );
          }
        },
      ),
    );
  }
}
