import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_export.dart';
import 'package:hmg_patient_app_new/extensions/string_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/water_action_buttons_widget.dart';
import 'package:hmg_patient_app_new/presentation/water_monitor/widgets/water_bottle_widget.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:provider/provider.dart';
import 'package:shimmer/shimmer.dart';

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

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(24.w),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [
            AppColors.blueGradientColorOne,
            AppColors.blueGradientColorTwo,
          ],
        ),
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Expanded(
                flex: isTablet ? 2 : 3,
                child: Consumer<WaterMonitorViewModel>(builder: (context, vm, _) {
                  if (vm.isLoading) {
                    return _buildLoadingShimmer();
                  }

                  final goalMl = vm.dailyGoalMl;
                  final consumed = vm.totalConsumedMl;
                  final remaining = (goalMl - consumed) > 0 ? (goalMl - consumed) : 0;
                  final completedPercent = "${(vm.progress * 100).clamp(0.0, 100.0).toStringAsFixed(0)}%";

                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // Don't show label if goal is achieved
                      if (!vm.nextDrinkTime.toLowerCase().contains('goal achieved'))
                        // Show "Tomorrow" if nextDrinkTime contains "tomorrow", otherwise "Next Drink Time"
                        (vm.nextDrinkTime.toLowerCase().contains('tomorrow') ? "Tomorrow" : "Next Drink Time")
                            .toText18(isBold: true, color: AppColors.textColor),

                      // Extract only time if "tomorrow" is present, otherwise show as is
                      (vm.nextDrinkTime.toLowerCase().contains('tomorrow')
                              ? vm.nextDrinkTime.replaceAll(RegExp(r'tomorrow', caseSensitive: false), '').trim()
                              : vm.nextDrinkTime)
                          .toText32(isBold: true, color: AppColors.blueColor),

                      SizedBox(height: 12.h),
                      Row(
                        children: [
                          _buildStatusColumn(title: LocaleKeys.yourGoal.tr(context: context), subTitle: "${goalMl}ml"),
                          SizedBox(width: 16.w),
                          _buildStatusColumn(title: LocaleKeys.remaining.tr(context: context), subTitle: "${remaining}ml"),
                          SizedBox(width: 16.w),
                          _buildStatusColumn(title: LocaleKeys.completed.tr(context: context), subTitle: completedPercent, subTitleColor: AppColors.successColor),
                        ],
                      ),
                      _buildStatusColumn(
                        title: LocaleKeys.hydrationStatus.tr(context: context),
                        subTitle: vm.hydrationStatus,
                        subTitleColor: vm.hydrationStatusColor,
                      ),
                    ],
                  );
                }),
              ),
              SizedBox(width: isTablet ? 32 : 16.w),
              Expanded(
                flex: isTablet ? 1 : 2,
                child: const WaterBottleWidget(),
              ),
            ],
          ),
          const WaterActionButtonsWidget(),
        ],
      ),
    );
  }

  Widget _buildStatusColumn({required String title, required String subTitle, Color? subTitleColor}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        "$title: ".toText16(isBold: true, color: AppColors.textColor),
        subTitle.toText12(
          isBold: true,
          color: subTitleColor ?? AppColors.greyTextColor,
        ),
      ],
    );
  }

  Widget _buildLoadingShimmer() {
    return ListView.separated(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      padding: EdgeInsets.all(0.w),
      itemCount: 4,
      separatorBuilder: (_, __) => SizedBox(height: 12.h),
      itemBuilder: (context, index) {
        return Shimmer.fromColors(
          baseColor: AppColors.shimmerBaseColor,
          highlightColor: AppColors.shimmerHighlightColor,
          child: Container(
            height: 40.h,
            decoration: BoxDecoration(
              color: AppColors.whiteColor,
              borderRadius: BorderRadius.circular(10.r),
            ),
          ),
        );
      },
    );
  }
}
