import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/data/services/model/tender_data/tender_data.dart';
import 'package:tm_app/view_models/tenders_view_model.dart';

import '../../../core/constants/assets.dart';
import '../../../core/constants/tender_approval_status.dart';
import '../../../core/constants/tender_feedback.dart';
import '../../../core/constants/tender_submision_mode.dart';
import '../../../core/theme/colors.dart';
import '../../../core/utils/size_config.dart';
import '../../shared/select_submission_bottom_sheet.dart';
import '../../shared/select_submission_dialog.dart';
import '../strings/tenders_strings.dart';
import '../widgets/animated_feedback_button.dart';

class TenderActionButtonsRow extends StatelessWidget {
  final TenderData tender;
  final bool isDesktop;

  const TenderActionButtonsRow({
    required this.tender,
    this.isDesktop = false,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Consumer<TendersViewModel>(
      builder: (context, viewModel, child) {
        return Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(width: 16.0.w()),

            // Dislike button
            _dislikeButton(viewModel),
            SizedBox(width: 32.0.w()),
            //reject button
            _rejectButton(viewModel),
            SizedBox(width: 32.0.w()),
            //submit button
            _submitButton(viewModel, context),
            SizedBox(width: 32.0.w()),
            // Like button
            _likeButton(viewModel),
          ],
        );
      },
    );
  }

  Widget _rejectButton(TendersViewModel viewModel) {
    // Find approval for this specific tender
    if (tender.id == null) {
      return const SizedBox.shrink();
    }

    final approval = viewModel.getTenderApprovalForTender(tender.id!);
    final isRejected = approval?.status == TenderApprovalStatus.rejected.value;

    return Tooltip(
      message: TendersStrings.reject,
      child: InkWell(
        splashColor: Colors.transparent,
        onTap: () {
          viewModel.toggleApprovals(
            tenderId: tender.id!,
            submissionMode: TenderSubmissionMode.selfApply.value,
            status: TenderApprovalStatus.rejected.value,
          );
        },
        child: Column(
          children: [
            Container(
              width: 24.0.w(),
              height: 24.0.w(),
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                border: Border.all(
                  color: isRejected ? AppColors.errorColor : AppColors.grey60,
                  width: 1.5.w(),
                ),
              ),
              alignment: Alignment.center,
              child: Icon(
                Icons.close,
                color: isRejected ? AppColors.errorColor : AppColors.grey60,
                size: 16,
              ),
            ),
            SizedBox(height: 8.0.h()),
            if (!isDesktop)
              Text(
                TendersStrings.reject,
                style: TextStyle(
                  fontSize: 12.0.sp(),
                  fontWeight: FontWeight.w400,
                  color: isRejected ? AppColors.errorColor : AppColors.grey60,
                ),
              ),
          ],
        ),
      ),
    );
  }

  Widget _submitButton(TendersViewModel viewModel, BuildContext context) {
    // Find approval for this specific tender
    if (tender.id == null) {
      return const SizedBox.shrink();
    }

    final approval = viewModel.getTenderApprovalForTender(tender.id!);
    final isApproved = approval?.status == TenderApprovalStatus.submitted.value;

    return Tooltip(
      message: TendersStrings.submit,
      child: InkWell(
        splashColor: Colors.transparent,
        onTap: () {
          if (isApproved) {
            viewModel.toggleApprovals(
              tenderId: tender.id!,
              submissionMode:
                  (approval?.submissionMode?.isNotEmpty == true)
                      ? approval!.submissionMode!
                      : TenderSubmissionMode.selfApply.value,
              status: TenderApprovalStatus.submitted.value,
            );
          } else {
            final isMobile = MediaQuery.sizeOf(context).width < 600;
            if (isMobile) {
              showModalBottomSheet(
                isDismissible: true,
                context: context,
                builder: (context) {
                  return SelectSubmissionBottomSheet(
                    onConfirm: (value) {
                      viewModel.toggleApprovals(
                        tenderId: tender.id!,
                        submissionMode: value,
                        status: TenderApprovalStatus.submitted.value,
                      );
                    },
                  );
                },
              );
            } else {
              showDialog(
                context: context,
                builder: (context) {
                  return SelectSubmissionDialog(
                    onConfirm: (String value) {
                      viewModel.toggleApprovals(
                        tenderId: tender.id!,
                        submissionMode: value,
                        status: TenderApprovalStatus.submitted.value,
                      );
                    },
                  );
                },
              );
            }
          }
        },
        child: Column(
          children: [
            Container(
              width: 24.0.w(),
              height: 24.0.w(),
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                border: Border.all(
                  color: isApproved ? AppColors.successColor : AppColors.grey60,
                  width: 1.5.w(),
                ),
              ),
              alignment: Alignment.center,
              child: Icon(
                Icons.check,
                color: isApproved ? AppColors.successColor : AppColors.grey60,
                size: 16,
              ),
            ),
            SizedBox(height: 8.0.h()),
            if (!isDesktop)
              Text(
                TendersStrings.submit,
                style: TextStyle(
                  fontSize: 12.0.sp(),
                  fontWeight: FontWeight.w400,
                  color: isApproved ? AppColors.successColor : AppColors.grey60,
                ),
              ),
          ],
        ),
      ),
    );
  }

  Widget _dislikeButton(TendersViewModel viewModel) {
    if (tender.id == null) {
      return const SizedBox.shrink();
    }

    final feedback = viewModel.getFeedbackForTender(tender.id!);
    final isDisliked = feedback?.feedbackType == TenderFeedback.disliked.value;

    return Column(
      children: [
        AnimatedFeedbackButton(
          iconPath: AssetsManager.dislike,
          activeColor: AppColors.errorColor,
          inactiveColor: AppColors.grey50,
          isActive: isDisliked,
          tooltip: TendersStrings.dislike,
          onTap: () {
            viewModel.toogleFeedback(
              tenderId: tender.id!,
              feedbackType: TenderFeedback.disliked.value,
            );
          },
        ),
        SizedBox(height: 8.0.h()),
        if (!isDesktop)
          Text(
            TendersStrings.dislike,
            style: TextStyle(
              fontSize: 12.0.sp(),
              fontWeight: FontWeight.w400,
              color: isDisliked ? AppColors.errorColor : AppColors.grey50,
            ),
          ),
      ],
    );
  }

  Widget _likeButton(TendersViewModel viewModel) {
    if (tender.id == null) {
      return const SizedBox.shrink();
    }

    final feedback = viewModel.getFeedbackForTender(tender.id!);
    final isLiked = feedback?.feedbackType == TenderFeedback.liked.value;

    return Column(
      children: [
        AnimatedFeedbackButton(
          iconPath: AssetsManager.like,
          activeColor: AppColors.successColor,
          inactiveColor: AppColors.grey50,
          isActive: isLiked,
          tooltip: TendersStrings.like,
          onTap: () {
            viewModel.toogleFeedback(
              tenderId: tender.id!,
              feedbackType: TenderFeedback.liked.value,
            );
          },
        ),
        SizedBox(height: 8.0.h()),
        if (!isDesktop)
          Text(
            TendersStrings.like,
            style: TextStyle(
              fontSize: 12.0.sp(),
              fontWeight: FontWeight.w400,
              color: isLiked ? AppColors.successColor : AppColors.grey50,
            ),
          ),
      ],
    );
  }
}
