// ignore_for_file: deprecated_member_use_from_same_package

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:go_router/go_router.dart';
import 'package:hoshan/core/gen/assets.gen.dart';
import 'package:hoshan/core/routes/route_generator.dart';
import 'package:hoshan/core/services/api/dio_service.dart';
import 'package:hoshan/data/model/empty_states_enum.dart';
import 'package:hoshan/data/model/personal_assistants_bots.dart';
import 'package:hoshan/ui/screens/main/assistant/bloc/personal_assistants_bloc.dart';
import 'package:hoshan/ui/screens/main/assistant/cubit/personal_assistant_info_cubit.dart';
import 'package:hoshan/ui/theme/colors.dart';
import 'package:hoshan/ui/theme/cubit/theme_mode_cubit.dart';
import 'package:hoshan/ui/theme/responsive.dart';
import 'package:hoshan/ui/theme/text.dart';
import 'package:hoshan/ui/widgets/components/button/loading_button.dart';
import 'package:hoshan/ui/widgets/components/image/network_image.dart';
import 'package:hoshan/ui/widgets/sections/empty/empty_states.dart';
import 'package:hoshan/ui/widgets/sections/loading/default_placeholder.dart';

class PersonalAssistantsScreen extends StatefulWidget {
  const PersonalAssistantsScreen({super.key});

  @override
  State<PersonalAssistantsScreen> createState() =>
      _PersonalAssistantsScreenState();
}

class _PersonalAssistantsScreenState extends State<PersonalAssistantsScreen> {
  final ScrollController scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      backgroundColor: Theme.of(context).colorScheme.surface,
      color: Theme.of(context).colorScheme.primary,
      onRefresh: () async {
        context.read<PersonalAssistantsBloc>().add(GetAll());
        scrollController.jumpTo(0);
      },
      child: SingleChildScrollView(
        controller: scrollController,
        physics: const BouncingScrollPhysics(
            parent: AlwaysScrollableScrollPhysics()),
        child: Responsive(context).maxWidthInDesktop(
          maxWidth: 800,
          child: (contxet, mw) =>
              BlocBuilder<PersonalAssistantsBloc, PersonalAssistantsState>(
            builder: (context, state) {
              if (state is PersonalAssistantsSuccess) {
                return ListView.builder(
                  itemCount: state.personalAssistants.length,
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) {
                    final personalAssistant = state.personalAssistants[index];

                    return Column(
                      children: [
                        GestureDetector(
                            onTap: () {
                              if (personalAssistant.status == 'confirmed') {
                                context.go(Routes.assistant,
                                    extra: personalAssistant.id);
                              }
                            },
                            child:
                                personalAssistantContainer(personalAssistant)),
                        if (state is PersonalAssistantInfoLoading)
                          Container(
                            color: Colors.white.withValues(alpha: 0.5),
                            child: Center(
                              child: SpinKitThreeBounce(
                                size: 32,
                                color: Theme.of(context).colorScheme.primary,
                              ),
                            ),
                          )
                      ],
                    );
                  },
                );
              }
              if (state is PersonalAssistantsEmpty ||
                  state is PersonalAssistantsFail) {
                return EmptyStates.getEmptyState(
                  status: EmptyStatesEnum.assistant,
                  title: 'هنوز دستیاری توسط شما ساخته نشده‌',
                );
              }
              return ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemCount: 10,
                itemBuilder: (context, index) =>
                    personalAssistantContainerPlaceholder(context),
              );
            },
          ),
        ),
      ),
    );
  }

  Widget personalAssistantContainer(PersonalAssistant personalAssistant) {
    return BlocProvider<PersonalAssistantInfoCubit>(
      create: (context) => PersonalAssistantInfoCubit(),
      child:
          BlocConsumer<PersonalAssistantInfoCubit, PersonalAssistantInfoState>(
        listener: (context, state) {
          if (state is PersonalAssistantInfoSucess) {
            context.go(Routes.createAssistant, extra: state.info);
          }
        },
        builder: (context, state) {
          return Directionality(
            textDirection: TextDirection.rtl,
            child: Column(
              children: [
                Container(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 32, vertical: 8),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      ImageNetwork(
                        baseUrl: DioService.baseURL,
                        url: personalAssistant.image,
                        width: 50,
                        height: 50,
                        radius: 360,
                      ),
                      const SizedBox(
                        width: 12,
                      ),
                      Expanded(
                          child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            personalAssistant.name ?? '',
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: AppTextStyles.body4.copyWith(
                                fontWeight: FontWeight.bold,
                                color: Theme.of(context).colorScheme.onSurface),
                          ),
                          const SizedBox(
                            height: 4,
                          ),
                          Text(
                            personalAssistant.description ?? '',
                            maxLines: 3,
                            overflow: TextOverflow.ellipsis,
                            style: AppTextStyles.body4.copyWith(
                                color: AppColors.gray[
                                    context.read<ThemeModeCubit>().isDark()
                                        ? 600
                                        : 900]),
                          ),
                          const SizedBox(
                            height: 8,
                          ),
                          personalAssistant.status == 'confirmed'
                              ? Column(
                                  children: [
                                    Row(
                                      children: [
                                        Expanded(
                                          flex: 4,
                                          child: Row(
                                            children: [
                                              Text(
                                                'تاییدشده',
                                                style: AppTextStyles.body4.copyWith(
                                                    color: AppColors
                                                        .gray[context
                                                            .read<
                                                                ThemeModeCubit>()
                                                            .isDark()
                                                        ? 600
                                                        : 900]),
                                              ),
                                              const SizedBox(
                                                width: 2,
                                              ),
                                              Assets.icon.bold.verify.svg(
                                                  width: 16,
                                                  height: 16,
                                                  color: AppColors
                                                      .green.defaultShade)
                                            ],
                                          ),
                                        ),
                                        const Expanded(
                                            flex: 1, child: SizedBox()),
                                        Expanded(
                                          flex: 4,
                                          child: Row(
                                            textDirection: TextDirection.ltr,
                                            children: [
                                              if (personalAssistant.comments !=
                                                  null)
                                                Row(
                                                  children: [
                                                    Text(
                                                      personalAssistant.comments
                                                          .toString(),
                                                      style: AppTextStyles.body4
                                                          .copyWith(
                                                              color: AppColors
                                                                  .gray[context
                                                                      .read<
                                                                          ThemeModeCubit>()
                                                                      .isDark()
                                                                  ? 600
                                                                  : 900]),
                                                    ),
                                                    const SizedBox(
                                                      width: 4,
                                                    ),
                                                    Assets.icon.outline.messages
                                                        .svg(
                                                            width: 18,
                                                            height: 18,
                                                            color: Theme.of(
                                                                    context)
                                                                .colorScheme
                                                                .primary),
                                                  ],
                                                ),
                                              // Row(
                                              //   children: [
                                              //     Padding(
                                              //       padding:
                                              //           const EdgeInsets.only(top: 2.0),
                                              //       child: Text(
                                              //         '12',
                                              //         style: AppTextStyles.body4.copyWith(
                                              //             color: AppColors.gray[context
                                              // .read<ThemeModeCubit>()
                                              // .isDark()
                                              // ? 600
                                              // :900]),
                                              //       ),
                                              //     ),
                                              //     Icon(
                                              //       Icons.remove_red_eye_outlined,
                                              //       size: 20,
                                              //       color: AppColors
                                              //           .primaryColor.defaultShade,
                                              //     ),
                                              //   ],
                                              // ),
                                              if (personalAssistant.score !=
                                                  null)
                                                Row(
                                                  children: [
                                                    Padding(
                                                      padding:
                                                          const EdgeInsets.only(
                                                              top: 4.0),
                                                      child: Text(
                                                        personalAssistant.score
                                                            .toString(),
                                                        style: AppTextStyles
                                                            .body4
                                                            .copyWith(
                                                                color: AppColors
                                                                    .gray[context
                                                                        .read<
                                                                            ThemeModeCubit>()
                                                                        .isDark()
                                                                    ? 600
                                                                    : 900]),
                                                      ),
                                                    ),
                                                    const SizedBox(
                                                      width: 4,
                                                    ),
                                                    Icon(
                                                      Icons.star_border_rounded,
                                                      size: 24,
                                                      color: Theme.of(context)
                                                          .colorScheme
                                                          .primary,
                                                    ),
                                                    const SizedBox(
                                                      width: 16,
                                                    ),
                                                  ],
                                                ),
                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                    const SizedBox(
                                      height: 8,
                                    ),
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.end,
                                      children: [
                                        LoadingButton(
                                          loading: state
                                              is PersonalAssistantInfoLoading,
                                          onPressed: () {
                                            context
                                                .read<
                                                    PersonalAssistantInfoCubit>()
                                                .getInfo(personalAssistant.id!);
                                          },
                                          isOutlined: true,
                                          color: Theme.of(context)
                                              .colorScheme
                                              .primary,
                                          backgroundColor: Theme.of(context)
                                              .scaffoldBackgroundColor,
                                          child: Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Text(
                                                'ویرایش',
                                                style: AppTextStyles.body4
                                                    .copyWith(
                                                        color: Theme.of(context)
                                                            .colorScheme
                                                            .primary),
                                              ),
                                              Assets.icon.outline.edit2.svg(
                                                color: Theme.of(context)
                                                    .colorScheme
                                                    .primary,
                                                width: 18,
                                                height: 18,
                                              )
                                            ],
                                          ),
                                        ),
                                      ],
                                    )
                                  ],
                                )
                              : personalAssistant.status == 'rejected'
                                  ? Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Row(
                                          children: [
                                            Text(
                                              'تاییدنشده',
                                              style: AppTextStyles.body4.copyWith(
                                                  color: AppColors.gray[context
                                                          .read<
                                                              ThemeModeCubit>()
                                                          .isDark()
                                                      ? 600
                                                      : 900]),
                                            ),
                                            const SizedBox(
                                              width: 2,
                                            ),
                                            Assets.icon.bold.verify.svg(
                                                width: 16,
                                                height: 16,
                                                color:
                                                    AppColors.red.defaultShade)
                                          ],
                                        ),
                                        LoadingButton(
                                            loading: state
                                                is PersonalAssistantInfoLoading,
                                            onPressed: () {
                                              context
                                                  .read<
                                                      PersonalAssistantInfoCubit>()
                                                  .getInfo(
                                                      personalAssistant.id!);
                                            },
                                            color: AppColors.red[50],
                                            child: Text(
                                              'بازبینی',
                                              style: AppTextStyles.body4
                                                  .copyWith(
                                                      color: AppColors
                                                          .red.defaultShade),
                                            ))
                                      ],
                                    )
                                  : personalAssistant.status == 'pending'
                                      ? Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            Row(
                                              children: [
                                                Text(
                                                  'در انتظار تایید',
                                                  style: AppTextStyles.body4
                                                      .copyWith(
                                                          color: AppColors
                                                              .gray[context
                                                                  .read<
                                                                      ThemeModeCubit>()
                                                                  .isDark()
                                                              ? 600
                                                              : 900]),
                                                ),
                                                const SizedBox(
                                                  width: 4,
                                                ),
                                                SpinKitCircle(
                                                  size: 24,
                                                  color: Theme.of(context)
                                                      .colorScheme
                                                      .primary,
                                                )
                                              ],
                                            ),
                                            LoadingButton(
                                              loading: state
                                                  is PersonalAssistantInfoLoading,
                                              onPressed: () {
                                                context
                                                    .read<
                                                        PersonalAssistantInfoCubit>()
                                                    .getInfo(
                                                        personalAssistant.id!);
                                              },
                                              isOutlined: true,
                                              color: Theme.of(context)
                                                  .colorScheme
                                                  .primary,
                                              backgroundColor: Theme.of(context)
                                                  .scaffoldBackgroundColor,
                                              child: Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.center,
                                                children: [
                                                  Text(
                                                    'ویرایش',
                                                    style: AppTextStyles.body4
                                                        .copyWith(
                                                            color: Theme.of(
                                                                    context)
                                                                .colorScheme
                                                                .primary),
                                                  ),
                                                  Assets.icon.outline.edit2.svg(
                                                    color: Theme.of(context)
                                                        .colorScheme
                                                        .primary,
                                                    width: 18,
                                                    height: 18,
                                                  )
                                                ],
                                              ),
                                            ),
                                          ],
                                        )
                                      : const SizedBox.shrink()
                        ],
                      ))
                    ],
                  ),
                ),
                const Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                  child: Divider(),
                )
              ],
            ),
          );
        },
      ),
    );
  }

  Directionality personalAssistantContainerPlaceholder(BuildContext context) {
    return Directionality(
      textDirection: TextDirection.rtl,
      child: Column(
        children: [
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 8),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                DefaultPlaceHolder(
                    child: Container(
                  width: 50,
                  height: 50,
                  decoration: const BoxDecoration(
                      shape: BoxShape.circle, color: Colors.white),
                )),
                const SizedBox(
                  width: 12,
                ),
                Expanded(
                    child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    DefaultPlaceHolder(
                      child: Container(
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(16),
                        ),
                        child: Text(
                          'ساخت پاورپوینت',
                          style: AppTextStyles.body4.copyWith(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ),
                    ),
                    const SizedBox(
                      height: 4,
                    ),
                    DefaultPlaceHolder(
                      child: Container(
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(16),
                        ),
                        child: Text(
                          'با وارد کردن محتوای دلخواهتان، یک پاورپوینت حرفه‌ای و آماده ارائه ایجاد کنید.',
                          style: AppTextStyles.body4.copyWith(
                              color: AppColors.gray[
                                  context.read<ThemeModeCubit>().isDark()
                                      ? 600
                                      : 900]),
                        ),
                      ),
                    ),
                    const SizedBox(
                      height: 8,
                    ),
                    Row(
                      children: [
                        Expanded(
                          flex: 4,
                          child: DefaultPlaceHolder(
                            child: Container(
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(16),
                              ),
                              child: Row(
                                children: [
                                  Text(
                                    'تاییدشده',
                                    style: AppTextStyles.body4.copyWith(
                                        color: AppColors.gray[context
                                                .read<ThemeModeCubit>()
                                                .isDark()
                                            ? 600
                                            : 900]),
                                  ),
                                  const SizedBox(
                                    width: 2,
                                  ),
                                  Assets.icon.bold.verify.svg(
                                      width: 16,
                                      height: 16,
                                      color: AppColors.green.defaultShade)
                                ],
                              ),
                            ),
                          ),
                        ),
                        const Expanded(flex: 1, child: SizedBox()),
                        Expanded(
                          flex: 4,
                          child: DefaultPlaceHolder(
                            child: Container(
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(16),
                              ),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Row(
                                    children: [
                                      Text(
                                        '12',
                                        style: AppTextStyles.body4.copyWith(
                                            color: AppColors.gray[context
                                                    .read<ThemeModeCubit>()
                                                    .isDark()
                                                ? 600
                                                : 900]),
                                      ),
                                      Assets.icon.outline.messages.svg(
                                          width: 18,
                                          height: 18,
                                          color: Theme.of(context)
                                              .colorScheme
                                              .primary)
                                    ],
                                  ),
                                  Row(
                                    children: [
                                      Padding(
                                        padding:
                                            const EdgeInsets.only(top: 2.0),
                                        child: Text(
                                          '12',
                                          style: AppTextStyles.body4.copyWith(
                                              color: AppColors.gray[context
                                                      .read<ThemeModeCubit>()
                                                      .isDark()
                                                  ? 600
                                                  : 900]),
                                        ),
                                      ),
                                      Icon(
                                        Icons.remove_red_eye_outlined,
                                        size: 20,
                                        color: Theme.of(context)
                                            .colorScheme
                                            .primary,
                                      ),
                                    ],
                                  ),
                                  Row(
                                    children: [
                                      Padding(
                                        padding:
                                            const EdgeInsets.only(top: 2.0),
                                        child: Text(
                                          '12',
                                          style: AppTextStyles.body4.copyWith(
                                              color: AppColors.gray[context
                                                      .read<ThemeModeCubit>()
                                                      .isDark()
                                                  ? 600
                                                  : 900]),
                                        ),
                                      ),
                                      Icon(
                                        Icons.star_border_rounded,
                                        size: 20,
                                        color: Theme.of(context)
                                            .colorScheme
                                            .primary,
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          ),
                        )
                      ],
                    ),
                    const SizedBox(
                      height: 8,
                    ),
                    DefaultPlaceHolder(
                        child: Container(
                      width: MediaQuery.sizeOf(context).width,
                      height: 48,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(16),
                      ),
                    ))
                  ],
                ))
              ],
            ),
          ),
          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            child: Divider(),
          )
        ],
      ),
    );
  }
}
