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

import '../../../core/constants/common_strings.dart';
import '../../shared/tender_app_bar.dart';
import '../strings/tenders_strings.dart';

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

  @override
  State<TabletTendersPage> createState() => _TabletTendersPageState();
}

class _TabletTendersPageState extends State<TabletTendersPage> {
  late final TendersViewModel viewModel;

  @override
  void initState() {
    super.initState();
    viewModel = context.read<TendersViewModel>();
  }

  @override
  Widget build(BuildContext context) {
    final GlobalKey<ScaffoldState> key = GlobalKey();
    return Scaffold(
      key: key,
      backgroundColor: AppColors.backgroundColor,
      appBar: tabletAppBar(title: TendersStrings.tendersTitle, key: key),
      drawer: const TabletNavigationWidget(currentIndex: 1),
      body: Consumer<TendersViewModel>(
        builder: (context, viewModel, child) {
          if (viewModel.isLoading) {
            return const 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 Expanded(
            child: SingleChildScrollView(
              child: SafeArea(
                child: Padding(
                  padding: EdgeInsetsDirectional.only(
                    end: 24.0.w(),
                    top: 128.0.h(),
                  ),
                  child: MainTendersSlider(
                    tenders: viewModel.tendersResponse?.data?.tenders ?? [],
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
