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_feedback.dart';
import '../../../core/theme/colors.dart';
import '../../../core/utils/size_config.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: isDesktop ? 16.0.h() : 32.0.w()),
            // Like button
            _likeButton(viewModel),
          ],
        );
      },
    );
  }

  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,
            ),
          ),
      ],
    );
  }
}
