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/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/smartwatch_health_data/health_data_transformations.dart' as durations;
import 'package:hmg_patient_app_new/features/smartwatch_health_data/health_provider.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:provider/provider.dart';

import '../../core/utils/date_util.dart' show DateUtil;

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: AppColors.bgScaffoldColor,
        body: CollapsingListView(
          title: "All Health Data".needTranslation,
          child: Column(
            spacing: 16.h,
            children: [
              resultItem(
                      leadingIcon: AppAssets.watchActivity,
                      title: "Activity Calories".needTranslation,
                      description: "Activity rings give you a quick visual reference of how active you are each day. ".needTranslation,
                      trailingIcon: AppAssets.watchActivityTrailing,
                      result: context.read<HealthProvider>().sumOfNonEmptyData(context.read<HealthProvider>().vitals?.activity ?? []),
                      unitsOfMeasure: "Kcal")
                  .onPress(() {
                context.read<HealthProvider>().setDurations(durations.Durations.daily);
                context.read<HealthProvider>().deleteDataIfSectionIsDifferent("activity");
                context.read<HealthProvider>().saveSelectedSection("activity");
                context.read<HealthProvider>().fetchData();
                context.read<HealthProvider>().navigateToDetails("activity", sectionName: "Activity Calories", uom: "Kcal");
              }),
              resultItem(
                      leadingIcon: AppAssets.watchSteps,
                      title: "Steps".needTranslation,
                      description: "Step count is the number of steps you take throughout the day.".needTranslation,
                      trailingIcon: AppAssets.watchStepsTrailing,
                      result: context.read<HealthProvider>().sumOfNonEmptyData(context.read<HealthProvider>().vitals?.step ?? []),
                      unitsOfMeasure: "Steps")
                  .onPress(() {
                // Map<String, List<Vitals>> getVitals() {
                //   return {
                //     "heartRate": heartRate ,
                //     "sleep": sleep,
                //     "steps": step,
                //     "activity": activity,
                //     "bodyOxygen": bodyOxygen,
                //     "bodyTemperature": bodyTemperature,
                //   };
                // }
                context.read<HealthProvider>().setDurations(durations.Durations.daily);
                context.read<HealthProvider>().deleteDataIfSectionIsDifferent("steps");
                context.read<HealthProvider>().saveSelectedSection("steps");
                context.read<HealthProvider>().fetchData();
                context.read<HealthProvider>().navigateToDetails("steps", sectionName: "Steps", uom: "Steps");
              }),
              resultItem(
                      leadingIcon: AppAssets.watchSteps,
                      title: "Distance Covered".needTranslation,
                      description: "Step count is the distance you take throughout the day.".needTranslation,
                      trailingIcon: AppAssets.watchStepsTrailing,
                      result: context.read<HealthProvider>().sumOfNonEmptyData(context.read<HealthProvider>().vitals?.distance ?? []),
                      unitsOfMeasure: "Km")
                  .onPress(() {
                context.read<HealthProvider>().setDurations(durations.Durations.daily);
                context.read<HealthProvider>().deleteDataIfSectionIsDifferent("distance");
                context.read<HealthProvider>().saveSelectedSection("distance");
                context.read<HealthProvider>().fetchData();
                context.read<HealthProvider>().navigateToDetails("distance", sectionName: "Distance Covered", uom: "km");
              }),
              resultItem(
                      leadingIcon: AppAssets.watchSleep,
                      title: "Sleep Score".needTranslation,
                      description: "This will keep track of how much hours you sleep in a day".needTranslation,
                      trailingIcon: AppAssets.watchSleepTrailing,
                      result: DateUtil.millisToHourMin(
                              int.parse(context.read<HealthProvider>().firstNonEmptyValue(context.read<HealthProvider>().vitals?.sleep ?? [])))
                          .split(" ")[0],
                      unitsOfMeasure: "hr",
                      resultSecondValue: DateUtil.millisToHourMin(
                              int.parse(context.read<HealthProvider>().firstNonEmptyValue(context.read<HealthProvider>().vitals?.sleep ?? [])))
                          .split(" ")[2],
                      unitOfSecondMeasure: "min")
                  .onPress(() {
                context.read<HealthProvider>().setDurations(durations.Durations.daily);
                context.read<HealthProvider>().deleteDataIfSectionIsDifferent("sleep");
                context.read<HealthProvider>().saveSelectedSection("sleep");
                context.read<HealthProvider>().fetchData();
                context.read<HealthProvider>().navigateToDetails("sleep", sectionName: "Sleep Score", uom: "");
              }),
              resultItem(
                      leadingIcon: AppAssets.watchWeight,
                      title: "Blood Oxygen".needTranslation,
                      description: "This will calculate your Blood Oxygen to keep track and update history".needTranslation,
                      trailingIcon: AppAssets.watchWeightTrailing,
                      result: context.read<HealthProvider>().firstNonEmptyValue(
                            context.read<HealthProvider>().vitals?.bodyOxygen ?? [],
                          ),
                      unitsOfMeasure: "%")
                  .onPress(() {
                context.read<HealthProvider>().setDurations(durations.Durations.daily);
                context.read<HealthProvider>().deleteDataIfSectionIsDifferent("bodyOxygen");
                context.read<HealthProvider>().saveSelectedSection("bodyOxygen");
                context.read<HealthProvider>().fetchData();
                context.read<HealthProvider>().navigateToDetails("bodyOxygen", uom: "%", sectionName: "Blood Oxygen");
              }),
              resultItem(
                      leadingIcon: AppAssets.watchWeight,
                      title: "Body temperature".needTranslation,
                      description: "This will calculate your Body temprerature to keep track and update history".needTranslation,
                      trailingIcon: AppAssets.watchWeightTrailing,
                      result: context.read<HealthProvider>().firstNonEmptyValue(context.read<HealthProvider>().vitals?.bodyTemperature ?? []),
                      unitsOfMeasure: "C")
                  .onPress(() {
                context.read<HealthProvider>().setDurations(durations.Durations.daily);
                context.read<HealthProvider>().deleteDataIfSectionIsDifferent("bodyTemperature");
                context.read<HealthProvider>().saveSelectedSection("bodyTemperature");
                context.read<HealthProvider>().fetchData();
                context.read<HealthProvider>().navigateToDetails("bodyTemperature", sectionName: "Body temperature".capitalizeFirstofEach, uom: "C");
              }),
            ],
          ).paddingSymmetrical(24.w, 24.h),
        ));
  }

  Widget resultItem({
    required String leadingIcon,
    required String title,
    required String description,
    required String trailingIcon,
    required String result,
    required String unitsOfMeasure,
    String? resultSecondValue,
    String? unitOfSecondMeasure,
  }) {
    return DecoratedBox(
        decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 12.h),
        child: Row(
          spacing: 16.w,
          children: [
            Expanded(
              child: Column(
                spacing: 8.h,
                children: [
                  Row(
                    spacing: 8.w,
                    children: [
                      Utils.buildSvgWithAssets(icon: leadingIcon, height: 16.h, width: 14.w),
                      title.toText16(weight: FontWeight.w600, color: AppColors.textColor),
                    ],
                  ),
                  description.toText12(isBold: true, color: AppColors.greyTextColor),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.baseline,
                    textBaseline: TextBaseline.alphabetic,
                    spacing: 2.h,
                    children: [
                      result.toText21(isBold: true, color: AppColors.textColor),
                      unitsOfMeasure.toText10(isBold: true, color: AppColors.greyTextColor),
                      if (resultSecondValue != null)
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.baseline,
                          textBaseline: TextBaseline.alphabetic,
                          spacing: 2.h,
                          children: [
                            SizedBox(width: 2.w),
                            resultSecondValue.toText21(isBold: true, color: AppColors.textColor),
                            unitOfSecondMeasure!.toText10(isBold: true, color: AppColors.greyTextColor)
                          ],
                        ),
                    ],
                  ),
                ],
              ),
            ),
            Utils.buildSvgWithAssets(icon: trailingIcon, width: 72.w, height: 72.h),
          ],
        ).paddingSymmetrical(16.w, 16.h));
  }
}
