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 '../../../core/constants/tender_approval_status.dart';
import '../../../core/utils/app_toast.dart';
import '../../shared/desktop_navigation_widget.dart';
import '../../shared/tablet_desktop_appbar.dart';
import '../strings/tender_details_strings.dart';

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

  @override
  State<TenderDetailDesktopPage> createState() =>
      _TenderDetailDesktopPageState();
}

class _TenderDetailDesktopPageState extends State<TenderDetailDesktopPage> {
  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) {
    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      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: Column(
              children: [
                const DesktopNavigationWidget(
                  currentIndex: 1, // Home index
                ),
                Expanded(
                  child: SingleChildScrollView(
                    child: Center(
                      child: SizedBox(
                        width: 740,
                        child: Padding(
                          padding: const EdgeInsets.all(32.0),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              const TabletDesktopAppbar(
                                title: TenderDetailsStrings.tenderDetailTitle,
                              ),
                              SizedBox(height: 32.0.h()),
                              TenderDetailHeader(
                                isScreenBig: true,
                                detail: detail,
                              ),
                              SizedBox(height: 32.0.h()),
                              TenderDetailCard(detail: detail),
                              SizedBox(height: 24.0.h()),
                              TenderDetailActions(
                                isScreenBig: true,
                                detail: detail,
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
