import 'package:didvan/config/design_config.dart';
import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/models/ai/ai_chat_args.dart';
import 'package:didvan/models/ai/bot_assistants_model.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/routes/routes.dart';
import 'package:didvan/views/ai/bot_assistants_state.dart';
import 'package:didvan/views/widgets/didvan/button.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:didvan/views/widgets/hoshan_app_bar.dart';
import 'package:didvan/views/widgets/shimmer_placeholder.dart';
import 'package:didvan/views/widgets/skeleton_image.dart';
import 'package:didvan/views/widgets/state_handlers/empty_list.dart';
import 'package:didvan/views/widgets/state_handlers/sliver_state_handler.dart';
import 'package:flutter/material.dart';
import 'package:persian_number_utility/persian_number_utility.dart';
import 'package:provider/provider.dart';

class BotAssistantsPage extends StatefulWidget {
  const BotAssistantsPage({Key? key}) : super(key: key);

  @override
  State<BotAssistantsPage> createState() => _BotAssistantsPageState();
}

class _BotAssistantsPageState extends State<BotAssistantsPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HoshanAppBar(
        onBack: () => Navigator.pop(context),
        withActions: false,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
      floatingActionButton: context.watch<BotAssistantsState>().isMyAssistants
          ? FloatingActionButton.small(
              shape: const CircleBorder(),
              backgroundColor: Theme.of(context).colorScheme.primary,
              onPressed: () {
                Navigator.pushNamed(context, Routes.createBotAssistants);
              },
              child: const Icon(
                Icons.add,
                color: Colors.white,
              ),
            )
          : null,
      body: Consumer<BotAssistantsState>(
        builder:
            (BuildContext context, BotAssistantsState state, Widget? child) =>
                CustomScrollView(
          slivers: [
            // SliverToBoxAdapter(
            //   child: Center(
            //     child: Padding(
            //       padding: const EdgeInsets.only(top: 32, bottom: 24),
            //       child: DidvanText(
            //         'انتخاب بات‌ها',
            //         fontSize: 20,
            //         fontWeight: FontWeight.bold,
            //         color: Theme.of(context).colorScheme.checkFav,
            //       ),
            //     ),
            //   ),
            // ),
            const SliverToBoxAdapter(
              child: SizedBox(
                height: 32,
              ),
            ),
            if (state.appState != AppState.failed)
              SliverToBoxAdapter(
                child: Padding(
                  padding: const EdgeInsets.only(
                    bottom: 12.0,
                  ),
                  child: switchAssistants(context, state),
                ),
              ),
            SliverStateHandler(
                childCount: state.isMyAssistants
                    ? state.myAssistants.length
                    : state.globalAssistants.length,
                state: state,
                emptyState: const EmptyList(),
                builder: (context, state, index) {
                  final assistants = state.isMyAssistants
                      ? state.myAssistants[index]
                      : state.globalAssistants[index];
                  return assistantsContainer(state, context, assistants);
                },
                placeholderCount: 10,
                placeholder: assistantsContainerPlaceholder(state, context),
                onRetry: state.isMyAssistants
                    ? state.getMyAssissmant
                    : state.getGlobalAssissmant),
            SliverPadding(
                padding:
                    EdgeInsets.only(bottom: state.isMyAssistants ? 100 : 0))
          ],
          physics: const BouncingScrollPhysics(),
        ),
      ),
    );
  }

  Container assistantsContainerPlaceholder(
      BotAssistantsState state, BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(12),
      margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 32),
      decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.surface,
          borderRadius: DesignConfig.lowBorderRadius),
      child: Column(
        children: [
          if (state.isMyAssistants)
            const Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                ShimmerPlaceholder(
                  width: 60,
                  height: 24,
                  borderRadius: DesignConfig.lowBorderRadius,
                ),
              ],
            ),
          const Row(
            children: [
              ShimmerPlaceholder(
                width: 80,
                height: 80,
                borderRadius: DesignConfig.lowBorderRadius,
              ),
              SizedBox(
                width: 8,
              ),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    ShimmerPlaceholder(
                      width: 120,
                      height: 24,
                      borderRadius: DesignConfig.lowBorderRadius,
                    ),
                    SizedBox(
                      height: 8,
                    ),
                    ShimmerPlaceholder(
                      width: 240,
                      height: 46,
                      borderRadius: DesignConfig.lowBorderRadius,
                    ),
                    SizedBox(
                      height: 18,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          child: ShimmerPlaceholder(
                            height: 18,
                            borderRadius: DesignConfig.lowBorderRadius,
                          ),
                        ),
                        Expanded(child: SizedBox()),
                        Expanded(
                          child: ShimmerPlaceholder(
                            height: 18,
                            borderRadius: DesignConfig.lowBorderRadius,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              )
            ],
          ),
          const SizedBox(
            height: 12,
          ),
          ShimmerPlaceholder(
            width: MediaQuery.sizeOf(context).width,
            height: 46,
            borderRadius: DesignConfig.lowBorderRadius,
          ),
        ],
      ),
    );
  }

  Container assistantsContainer(BotAssistantsState state, BuildContext context,
      BotAssistants assistants) {
    return Container(
      padding: const EdgeInsets.all(12),
      margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 32),
      decoration: BoxDecoration(
          border: Border.all(color: Theme.of(context).colorScheme.border),
          color: Theme.of(context).colorScheme.surface,
          borderRadius: DesignConfig.lowBorderRadius),
      child: Column(
        children: [
          if (state.isMyAssistants)
            Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Container(
                  padding: const EdgeInsets.symmetric(horizontal: 12),
                  decoration: BoxDecoration(
                      color: Theme.of(context).colorScheme.disabledBackground,
                      borderRadius: DesignConfig.lowBorderRadius),
                  child: DidvanText(assistants.private! ? 'خصوصی' : 'عمومی'),
                ),
              ],
            ),
          Row(
            children: [
              SkeletonImage(
                imageUrl: assistants.image ?? assistants.bot!.image ?? '',
                width: 80,
                height: 80,
              ),
              const SizedBox(
                width: 8,
              ),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    DidvanText(
                      assistants.name ?? '',
                      fontSize: 16,
                      fontWeight: FontWeight.bold,
                      color: Theme.of(context).colorScheme.checkFav,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(
                      height: 8,
                    ),
                    if (assistants.description != null)
                      DidvanText(
                        assistants.description!,
                        fontSize: 12,
                        color: Theme.of(context).colorScheme.text,
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                    const SizedBox(
                      height: 18,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          child: Row(
                            children: [
                              Icon(
                                DidvanIcons.calendar_day_light,
                                size: 18,
                                color: Theme.of(context).colorScheme.caption,
                              ),
                              const SizedBox(
                                width: 4,
                              ),
                              DidvanText(
                                DateTime.parse(assistants.createdAt!)
                                    .toPersianDateStr(),
                                fontSize: 12,
                                color: Theme.of(context).colorScheme.caption,
                              ),
                            ],
                          ),
                        ),
                        Expanded(
                          child: state.isMyAssistants
                              ? InkWell(
                                  onTap: () async {
                                    Navigator.pushNamed(
                                        context, Routes.createBotAssistants,
                                        arguments: assistants.id);
                                  },
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.end,
                                    children: [
                                      Icon(
                                        DidvanIcons.user_edit_light,
                                        size: 18,
                                        color: Theme.of(context)
                                            .colorScheme
                                            .caption,
                                      ),
                                      const SizedBox(
                                        width: 4,
                                      ),
                                      DidvanText(
                                        'ویرایش',
                                        fontSize: 12,
                                        color: Theme.of(context)
                                            .colorScheme
                                            .caption,
                                      ),
                                    ],
                                  ),
                                )
                              : Row(
                                  children: [
                                    SkeletonImage(
                                      imageUrl: assistants.user!.photo ?? '',
                                      width: 24,
                                      height: 24,
                                      borderRadius: BorderRadius.circular(360),
                                    ),
                                    const SizedBox(
                                      width: 4,
                                    ),
                                    Expanded(
                                      child: DidvanText(
                                        assistants.user!.fullName ?? '',
                                        fontSize: 12,
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                        color: Theme.of(context)
                                            .colorScheme
                                            .caption,
                                      ),
                                    ),
                                  ],
                                ),
                        )
                      ],
                    ),
                  ],
                ),
              )
            ],
          ),
          const SizedBox(
            height: 12,
          ),
          DidvanButton(
            title: 'استفاده از دستیار',
            onPressed: () => Navigator.pushNamed(context, Routes.aiChat,
                arguments: AiChatArgs(
                    bot: assistants.bot!.copyWith(
                        id: assistants.id,
                        image: assistants.image,
                        description: assistants.description),
                    assistantsName: assistants.name)),
          )
        ],
      ),
    );
  }

  Container switchAssistants(BuildContext context, BotAssistantsState state) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 32),
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
          color: Theme.of(context).colorScheme.surface,
          borderRadius: DesignConfig.lowBorderRadius),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(
            child: InkWell(
              onTap: () {
                if (state.appState == AppState.busy) return;
                state.isMyAssistants = true;
                state.getMyAssissmant();
                state.update();
              },
              child: Column(
                children: [
                  Icon(
                    DidvanIcons.profile_solid,
                    color: state.isMyAssistants
                        ? Theme.of(context).colorScheme.primary
                        : Theme.of(context).colorScheme.disabledText,
                  ),
                  Container(
                    width: 100,
                    height: 1,
                    margin: const EdgeInsets.symmetric(vertical: 4),
                    decoration: BoxDecoration(
                      color: state.isMyAssistants
                          ? Theme.of(context).colorScheme.primary
                          : Theme.of(context).colorScheme.disabledText,
                    ),
                  ),
                  DidvanText(
                    'دستیارهای من',
                    color: state.isMyAssistants
                        ? Theme.of(context).colorScheme.primary
                        : Theme.of(context).colorScheme.disabledText,
                  )
                ],
              ),
            ),
          ),
          Container(
            height: 34,
            width: 1,
            margin: const EdgeInsets.symmetric(horizontal: 12),
            decoration:
                BoxDecoration(color: Theme.of(context).colorScheme.primary),
          ),
          Expanded(
            child: InkWell(
              onTap: () {
                if (state.appState == AppState.busy) return;
                state.isMyAssistants = false;
                state.getGlobalAssissmant();
                state.update();
              },
              child: Column(
                children: [
                  Icon(
                    DidvanIcons.profile_solid,
                    color: !state.isMyAssistants
                        ? Theme.of(context).colorScheme.primary
                        : Theme.of(context).colorScheme.disabledText,
                  ),
                  Container(
                    width: 100,
                    height: 1,
                    margin: const EdgeInsets.symmetric(vertical: 4),
                    decoration: BoxDecoration(
                      color: !state.isMyAssistants
                          ? Theme.of(context).colorScheme.primary
                          : Theme.of(context).colorScheme.disabledText,
                    ),
                  ),
                  DidvanText(
                    'دستیارهای دیگران',
                    color: !state.isMyAssistants
                        ? Theme.of(context).colorScheme.primary
                        : Theme.of(context).colorScheme.disabledText,
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
