import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/route_extensions.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/water_monitor/water_monitor_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/water_monitor/widgets/cup_bottomsheet_widgets.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:provider/provider.dart';

class WaterActionButtonsWidget extends StatelessWidget {
  const WaterActionButtonsWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer<WaterMonitorViewModel>(builder: (context, vm, _) {
      final cupAmount = vm.selectedCupCapacityMl;
      final isGoalAchieved = vm.progressPercent >= 100 || vm.nextDrinkTime.toLowerCase().contains('goal achieved');
      final isDisabled = vm.isLoading || isGoalAchieved;

      return Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Opacity(
                opacity: vm.isLoading ? 0.4 : 1.0,
                child: InkWell(
                  onTap: vm.isLoading
                      ? null
                      : () async {
                          if (cupAmount > 0) {
                            await vm.undoUserActivity();
                          }
                        },
                  child: Utils.buildSvgWithAssets(
                    icon: AppAssets.minimizeIcon,
                    height: 20.h,
                    width: 20.h,
                    iconColor: AppColors.textColor,
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.symmetric(horizontal: 4.w),
                padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 4.h),
                decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                  color: AppColors.blueColor,
                  borderRadius: 99.r,
                  hasShadow: true,
                ),
                child: (cupAmount > 0 ? "+ $cupAmount ml" : "+ 0ml").toText12(
                  isBold: true,
                  color: AppColors.whiteColor,
                ),
              ),
              Opacity(
                opacity: isDisabled ? 0.4 : 1.0,
                child: InkWell(
                  onTap: isDisabled
                      ? null
                      : () async {
                          if (cupAmount > 0) {
                            await vm.insertUserActivity(quantityIntake: cupAmount);
                          }
                        },
                  child: Utils.buildSvgWithAssets(
                    icon: AppAssets.addIconDark,
                    height: 20.h,
                    width: 20.h,
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 8.h),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _buildActionButton(
                context: context,
                onTap: () => showSwitchCupBottomSheet(context),
                overlayWidget: AppAssets.refreshIcon,
                title: LocaleKeys.switchCup.tr(context: context),
                icon: Utils.buildSvgWithAssets(icon: AppAssets.glassIcon, height: 24.w, width: 24.w),
              ),
              _buildActionButton(
                context: context,
                onTap: () async {},
                title: LocaleKeys.plainWater.tr(context: context),
                icon: Utils.buildSvgWithAssets(icon: AppAssets.glassIcon, height: 24.w, width: 24.w),
              ),
              _buildActionButton(
                context: context,
                onTap: () => context.navigateWithName(AppRoutes.waterMonitorSettingsPage),
                title: LocaleKeys.settings.tr(context: context),
                icon: Icon(
                  Icons.settings,
                  color: AppColors.blueColor,
                  size: 24.w,
                ),
              ),
            ],
          ),
        ],
      );
    });
  }

  Widget _buildActionButton({
    required BuildContext context,
    String? overlayWidget,
    required String title,
    required Widget icon,
    required VoidCallback onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Column(
        children: [
          Stack(
            children: [
              Container(
                height: 46.w,
                width: 46.w,
                decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                  color: AppColors.blueColor.withValues(alpha: 0.14),
                  borderRadius: 12.r,
                  hasShadow: true,
                ),
                child: Center(child: icon),
              ),
              if (overlayWidget != null) ...[
                Positioned(
                  top: 0,
                  right: 0,
                  child: Container(
                    padding: EdgeInsets.all(2.w),
                    height: 16.w,
                    width: 16.w,
                    decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                      color: AppColors.blueColor,
                      borderRadius: 100.r,
                      hasShadow: true,
                    ),
                    child: Center(
                      child: Utils.buildSvgWithAssets(
                        icon: AppAssets.refreshIcon,
                        iconColor: AppColors.whiteColor,
                      ),
                    ),
                  ),
                ),
              ]
            ],
          ),
          SizedBox(height: 4.h),
          title.toText10(),
        ],
      ),
    );
  }
}
