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/tenders_view_model.dart';
import 'package:tm_app/views/shared/desktop_navigation_widget.dart';
import 'package:tm_app/views/tenders/widgets/main_tenders_slider.dart';

import '../../../core/constants/common_strings.dart';
import '../../../core/utils/app_toast.dart';

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

  @override
  State<DesktopTendersPage> createState() => _DesktopTendersPageState();
}

class _DesktopTendersPageState extends State<DesktopTendersPage> {
  late final TendersViewModel viewModel;
  @override
  void initState() {
    super.initState();
    viewModel = context.read<TendersViewModel>();
    viewModel.addListener(_viewModelListener);
  }

  void _viewModelListener() {
    if (viewModel.feedbackErrorMessage != null) {
      AppToast.error(context, viewModel.feedbackErrorMessage!);
    }
    if (viewModel.tenderApprovalErrorMessage != null) {
      AppToast.error(context, viewModel.tenderApprovalErrorMessage!);
    }
  }

  @override
  void dispose() {
    viewModel.removeListener(_viewModelListener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      body: Column(
        children: [
          const DesktopNavigationWidget(
            currentIndex: 1, // Tenders index
          ),
          Expanded(
            child: Consumer<TendersViewModel>(
              builder: (context, viewModel, child) {
                if (viewModel.isLoading) {
                  return const Expanded(
                    child: Center(
                      child: CircularProgressIndicator(
                        color: AppColors.jellyBean,
                      ),
                    ),
                  );
                }
                if (viewModel.errorMessage != null) {
                  return Center(child: Text(viewModel.errorMessage!));
                }
                if ((viewModel.tendersResponse?.data?.tenders == null ||
                        viewModel.tendersResponse!.data!.tenders!.isEmpty) &&
                    viewModel.isLoading == false) {
                  return const Center(child: Text(CommonStrings.noData));
                }
                return SingleChildScrollView(
                  child: SizedBox(
                    width: 740,
                    height: 800,
                    child: Padding(
                      padding: EdgeInsets.symmetric(vertical: 24.0.h()),
                      child: MainTendersSlider(
                        tenders: viewModel.tendersResponse?.data?.tenders ?? [],
                        isDesktop: true,
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
