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/strings/tender_details_strings.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/tender_app_bar.dart';

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

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

  @override
  State<TenderDetailMobilePage> createState() => _TenderDetailMobilePageState();
}

class _TenderDetailMobilePageState extends State<TenderDetailMobilePage> {
  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,
      appBar: appBar(
        context: context,
        title: TenderDetailsStrings.tenderDetailTitle,
      ),
      body: SafeArea(
        child: 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 SingleChildScrollView(
              child: Padding(
                padding: EdgeInsets.symmetric(
                  horizontal: 16.0.w(),
                  vertical: 16.0.h(),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    TenderDetailHeader(isScreenBig: false, detail: detail),
                    SizedBox(height: 24.0.h()),
                    TenderDetailCard(detail: detail),
                    SizedBox(height: 24.0.h()),
                    TenderDetailActions(isScreenBig: false, detail: detail),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}
