import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/core/constants/tender_feedback.dart';
import 'package:tm_app/core/theme/colors.dart';
import 'package:tm_app/core/utils/size_config.dart';
import 'package:tm_app/view_models/your_tenders_view_model.dart';
import 'package:tm_app/views/your_tenders/strings/your_tenders_strings.dart';
import 'package:tm_app/views/your_tenders/widgets/filter_button.dart';
import 'package:tm_app/views/your_tenders/widgets/liked_disliked_tenders_list.dart';
import 'package:tm_app/views/your_tenders/widgets/tablet_desktop_appbar.dart';
import 'package:tm_app/views/your_tenders/widgets/tenders_submitted.dart';

import '../../shared/desktop_navigation_widget.dart';

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

  @override
  State<YourTendersDesktopPage> createState() => _YourTendersDesktopPageState();
}

class _YourTendersDesktopPageState extends State<YourTendersDesktopPage>
    with SingleTickerProviderStateMixin {
  late TabController controller;

  @override
  void initState() {
    super.initState();
    controller = TabController(length: 2, vsync: this);
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      body: Column(
        children: [
          const DesktopNavigationWidget(currentIndex: 1), // Tenders index
          Expanded(
            child: SizedBox(
              width: 740,
              child: Column(
                children: [
                  SizedBox(height: 64.0.h()),
                  const TabletDesktopAppbar(),
                  SizedBox(height: 20.0.h()),
                  const FilterButton(platformType: PlatformType.tabletDesktop),

                  Expanded(
                    child: Consumer<YourTendersViewModel>(
                      builder: (context, viewModel, child) {
                        if (viewModel.isLoading &&
                            (viewModel.data == null &&
                                viewModel.likedTendersData == null)) {
                          return const Center(
                            child: CircularProgressIndicator(
                              color: AppColors.secondary50,
                            ),
                          );
                        }

                        if (viewModel.errorMessage != null) {
                          return Center(child: Text(viewModel.errorMessage!));
                        }

                        Widget content;

                        if (viewModel.selectedStatus ==
                                YourTendersStrings.likeTenders &&
                            viewModel.likedTendersData != null) {
                          content = LikedDislikedTendersList(
                            data: viewModel.likedTendersData!,
                            status: TenderFeedback.liked.value,
                            isDesktop: true,
                            isMobile: false,
                          );
                        } else if (viewModel.selectedStatus ==
                                YourTendersStrings.dislikeTenders &&
                            viewModel.likedTendersData != null) {
                          content = LikedDislikedTendersList(
                            data: viewModel.likedTendersData!,
                            status: TenderFeedback.disliked.value,
                            isDesktop: true,
                            isMobile: false,
                          );
                        } else if (viewModel.data != null) {
                          content = TendersSubmitted(
                            approvedTenders: viewModel.data!,
                            isDesktop: true,
                            isMobile: false,
                          );
                        } else {
                          content = const Center(
                            child: Text(YourTendersStrings.noTenders),
                          );
                        }

                        return Column(
                          children: [
                            Expanded(child: content),
                            SizedBox(height: 10.0.h()),

                            Row(
                              children: [
                                const Spacer(),
                                Text(
                                  YourTendersStrings.page,
                                  style: TextStyle(
                                    fontSize: 13.0.sp(),
                                    fontWeight: FontWeight.w300,
                                    color: AppColors.grey60,
                                  ),
                                ),
                                SizedBox(width: 10.0.w()),
                                InkWell(
                                  onTap: () async {
                                    final selectedPage = await showDialog<int>(
                                      context: context,
                                      builder: (context) {
                                        return AlertDialog(
                                          backgroundColor:
                                              AppColors.backgroundColor,
                                          title: const Text(
                                            YourTendersStrings.selectPage,
                                          ),
                                          content: SizedBox(
                                            width: 200.0.w(),
                                            height: 300.0.h(),
                                            child: ListView.builder(
                                              itemCount: viewModel.totalPages,
                                              itemBuilder: (context, index) {
                                                final pageNumber = index + 1;
                                                return ListTile(
                                                  title: Text(
                                                    '${YourTendersStrings.page} $pageNumber',
                                                  ),
                                                  onTap:
                                                      () => Navigator.pop(
                                                        context,
                                                        pageNumber,
                                                      ),
                                                );
                                              },
                                            ),
                                          ),
                                        );
                                      },
                                    );

                                    if (selectedPage != null) {
                                      await viewModel.jumpToPage(selectedPage);
                                    }
                                  },
                                  child: Container(
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                        color: AppColors.grey30,
                                        width: 1,
                                      ),
                                      borderRadius: BorderRadius.circular(4),
                                    ),
                                    child: Row(
                                      children: [
                                        SizedBox(width: 5.0.w()),
                                        Text(
                                          '${viewModel.currentPage}',
                                          style: TextStyle(
                                            fontSize: 14.0.sp(),
                                            fontWeight: FontWeight.w500,
                                            color: AppColors.grey70,
                                          ),
                                        ),
                                        SizedBox(width: 5.0.w()),
                                        const Icon(Icons.arrow_drop_down),
                                        SizedBox(width: 5.0.w()),
                                      ],
                                    ),
                                  ),
                                ),
                                SizedBox(width: 5.0.w()),
                                Text(
                                  YourTendersStrings.of,
                                  style: TextStyle(
                                    fontSize: 14.0.sp(),
                                    fontWeight: FontWeight.w400,
                                    color: AppColors.grey60,
                                  ),
                                ),
                                SizedBox(width: 5.0.w()),
                                Text(
                                  '${viewModel.totalPages}',
                                  style: TextStyle(
                                    fontSize: 13.0.sp(),
                                    fontWeight: FontWeight.w400,
                                    color: AppColors.grey60,
                                  ),
                                ),
                              ],
                            ),

                            SizedBox(height: 30.0.h()),
                          ],
                        );
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
