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/tender_detail_view_model.dart';
import 'package:tm_app/views/detail/widgets/tender_detail_action.dart';
import 'package:tm_app/views/detail/widgets/tender_detail_card.dart';
import 'package:tm_app/views/detail/widgets/tender_detail_header.dart';
import 'package:tm_app/views/shared/tablet_navigation_widget.dart';

import '../../../core/constants/tender_approval_status.dart';
import '../../../core/utils/app_toast.dart';
import '../../shared/tablet_desktop_appbar.dart';
import '../../shared/tender_app_bar.dart';
import '../strings/tender_details_strings.dart';

class TenderDetailTabletPage extends StatefulWidget {
  final String tenderId;
  const TenderDetailTabletPage({required this.tenderId, super.key});

  @override
  State<TenderDetailTabletPage> createState() => _TenderDetailTabletPageState();
}

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

  void _viewModelListener() {
    if (viewModel.status.isNotEmpty &&
        viewModel.status == TenderApprovalStatus.submitted.value &&
        viewModel.approvalStatus == true) {
      AppToast.success(
        context,
        TenderDetailsStrings.tenderSubmittedSuccessfully,
      );
    }
    if (viewModel.status.isNotEmpty &&
        viewModel.status == TenderApprovalStatus.unsubmitted.value &&
        viewModel.approvalStatus == true) {
      AppToast.success(
        context,
        TenderDetailsStrings.tenderUnsubmittedSuccessfully,
      );
    }
    if (viewModel.status.isNotEmpty &&
        viewModel.status == TenderApprovalStatus.rejected.value &&
        viewModel.approvalStatus == true) {
      AppToast.success(
        context,
        TenderDetailsStrings.tenderRejectedSuccessfully,
      );
    }
    if (viewModel.status.isNotEmpty &&
        viewModel.status == TenderApprovalStatus.unrejected.value &&
        viewModel.approvalStatus == true) {
      AppToast.success(
        context,
        TenderDetailsStrings.tenderUnrejectedSuccessfully,
      );
    }
    if (viewModel.approvalStatus != false && viewModel.errorMessage != null) {
      AppToast.error(context, viewModel.errorMessage!);
    }
  }

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

  @override
  Widget build(BuildContext context) {
    final GlobalKey<ScaffoldState> key = GlobalKey();
    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      appBar: tabletAppBar(
        title: TenderDetailsStrings.tenderDetailTitle,
        key: key,
      ),

      drawer: const TabletNavigationWidget(
        currentIndex: 1, // Home is index 0
      ),
      body: Consumer<TenderDetailViewModel>(
        builder: (context, tenderViewModel, child) {
          if (tenderViewModel.isLoading) {
            return const Center(
              child: CircularProgressIndicator(color: AppColors.secondary50),
            );
          }

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

          final detail = tenderViewModel.tenderDetail;
          if (detail == null) {
            return const Center(child: Text(TenderDetailsStrings.tenderNoData));
          }
          return SafeArea(
            child: SingleChildScrollView(
              child: Center(
                child: SizedBox(
                  width: 768,
            
                  child: Padding(
                    padding: const EdgeInsets.all(24.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const TabletDesktopAppbar(
                          title: TenderDetailsStrings.tenderDetailTitle,
                        ),
                        SizedBox(height: 28.0.h()),
                        TenderDetailHeader(isScreenBig: true, detail: detail),
                        SizedBox(height: 28.0.h()),
                        TenderDetailCard(detail: detail),
                        SizedBox(height: 24.0.h()),
                        TenderDetailActions(isScreenBig: true, detail: detail),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
