import 'package:flutter/material.dart';
import 'package:provider/provider.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/shared/tablet_desktop_appbar.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/tenders_submitted.dart';

import '../../../core/constants/tender_feedback.dart';
import '../../shared/tablet_navigation_widget.dart';
import '../../shared/tender_app_bar.dart';
import '../strings/your_tenders_strings.dart';

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

  @override
  State<YourTendersTabletPage> createState() => _YourTendersTabletPageState();
}

class _YourTendersTabletPageState extends State<YourTendersTabletPage>
    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) {
    final GlobalKey<ScaffoldState> key = GlobalKey();
    return Scaffold(
      key: key,
      backgroundColor: AppColors.backgroundColor,
      appBar: tabletAppBar(title: YourTendersStrings.yourTenders, key: key),
      drawer: const TabletNavigationWidget(currentIndex: 1),
      body: SafeArea(
        child: Center(
          child: SizedBox(
            width: 720,
            child: Column(
              children: [
                const SizedBox(height: 64.0),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 8.0.w()),
                  child: const TabletDesktopAppbar(
                    title: YourTendersStrings.yourTenders,
                  ),
                ),
                SizedBox(height: 24.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: false,
                          isMobile: false,
                        );
                      } else if (viewModel.selectedStatus ==
                              YourTendersStrings.dislikeTenders &&
                          viewModel.likedTendersData != null) {
                        content = LikedDislikedTendersList(
                          data: viewModel.likedTendersData!,
                          status: TenderFeedback.disliked.value,
                          isDesktop: false,
                          isMobile: false,
                        );
                      } else if (viewModel.data != null) {
                        content = TendersSubmitted(
                          approvedTenders: viewModel.data!,
                          isDesktop: false,
                          isMobile: false,
                        );
                      } else {
                        content = const Center(
                          child: Text(YourTendersStrings.noTenders),
                        );
                      }
        
                      return Column(
                        children: [
                          Expanded(child: content),
                          SizedBox(height: 10.0.h()),
        
                          _listPagesControll(context, viewModel),
        
                          const SizedBox(height: 30.0),
                        ],
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _listPagesControll(
    BuildContext context,
    YourTendersViewModel viewModel,
  ) {
    return Row(
      children: [
        const Spacer(),
        Text(
          YourTendersStrings.page,
          style: TextStyle(
            fontSize: 13.0,
            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()),
              ],
            ),
          ),
        ),
        const SizedBox(width: 5.0),
        Text(
          YourTendersStrings.page,
          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,
          ),
        ),
      ],
    );
  }
}
