import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/all_requests_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/ppm/recurrent_wo.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_text_form_field.dart';
import 'package:test_sa/service_request_latest/utilities/service_request_utils.dart';
import 'package:test_sa/views/widgets/requests/request_status.dart';
import 'package:test_sa/views/widgets/timer/app_timer.dart';

class RecurrentTaskInfoWidget extends StatelessWidget {
  final RecurrentWoData? model;

  RecurrentTaskInfoWidget({super.key, required this.model});

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (model?.status != null)
              StatusLabel(
                label: model?.status?.name,
                textColor: AppColor.getRequestStatusTextColorByName(context, model?.status?.name),
                backgroundColor: AppColor.getRequestStatusColorByName(context, model?.status?.name),
              ),
            8.height,
            model!.title!.bodyText(context).custom(color: AppColor.black10),
            2.height,
            '${context.translation.taskNo}:  ${model!.taskNo!}'.bodyText2(context).custom(color: AppColor.neutral120),
            '${context.translation.site}:  ${model!.site!.siteName!}'.bodyText2(context).custom(color: AppColor.neutral120),
            '${context.translation.assignedEmployee}:  ${model!.engineer!.userName ?? ""}'.bodyText2(context).custom(color: AppColor.neutral120),
            '${context.translation.scheduledDate}:  ${model!.scheduleDate!.toMonthYearFormat}'.bodyText2(context).custom(color: AppColor.neutral120),
          ],
        ).toShadowContainer(context),
        12.height,
        model!.planRecurrentMedicalTaskRooms!.isEmpty
            ? Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  buildingInfoWidget(label: context.translation.department, value: model!.department?.name?.cleanupWhitespace.capitalizeFirstOfEach, context: context),
                  8.height,
                  buildingInfoWidget(label: context.translation.floor, value: model!.floor?.name?.cleanupWhitespace.capitalizeFirstOfEach, context: context),
                  8.height,
                  buildingInfoWidget(label: context.translation.room, value: model!.room?.name?.cleanupWhitespace.capitalizeFirstOfEach, context: context),
                  8.height,
                  _timerWidget(context, model!.totalWorkingHours!),
                  8.height,
                  _commentWidget(context: context, model: model)
                ],
                // ],
              ).toShadowContainer(context, padding: 12)
            : Column(
                children: [_timerWidget(context, model!.totalWorkingHours!), 8.height, _commentWidget(context: context, model: model)],
              ).toShadowContainer(context, padding: 12),
      ],
    );
  }

  Widget _timerWidget(BuildContext context, double totalWorkingHours) {
    return Consumer<AllRequestsProvider>(builder: (context, snapshot, child) {
      return Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          AppTimer(
            label: context.translation.timer,
            timer: snapshot.recurrentWoData?.recurrentWoTimerModel,
            width: double.infinity,
            enabled: snapshot.recurrentWoData?.status?.value != 1,
            decoration: BoxDecoration(
              color: AppColor.neutral100,
              borderRadius: BorderRadius.circular(10),
            ),
            timerProgress: (isRunning) {},
            onChange: (timer) async {
              snapshot.updateRecurrentWoTimer(timer: timer);
              return true;
            },
          ),
          if (totalWorkingHours > 0.0) ...[
            12.height,
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                'Total Working Time:'.bodyText2(context).custom(color: AppColor.neutral120),
                8.width,
                Text(
                  ServiceRequestUtils.formatTimerDuration(totalWorkingHours.round()),
                  style: AppTextStyles.bodyText.copyWith(color: AppColor.neutral50, fontWeight: FontWeight.w600),
                ),
              ],
            ),
          ],
        ],
      );
    });
  }

  Widget _commentWidget({RecurrentWoData? model, required BuildContext context}) {
    return AppTextFormField(
      initialValue: model?.comment,
      labelText: context.translation.comment,
      backgroundColor: AppColor.neutral100,
      showShadow: false,
      labelStyle: AppTextStyles.textFieldLabelStyle,
      alignLabelWithHint: true,
      textInputType: TextInputType.multiline,
      onChange: (value) {
        model?.comment = value;
      },
      onSaved: (value) {
        model?.comment = value;
      },
    );
  }
}

Widget buildingInfoWidget({required String label, required String? value, required BuildContext context}) {
  return Column(
    mainAxisAlignment: MainAxisAlignment.start,
    crossAxisAlignment: CrossAxisAlignment.start,
    mainAxisSize: MainAxisSize.min,
    children: [
      Text(
        label,
        style: AppTextStyles.textFieldLabelStyle.copyWith(color: AppColor.neutral120),
      ),
      3.height,
      Text(
        value ?? '',
        style: AppTextStyles.bodyText2.copyWith(color: AppColor.black10),
      )
    ],
  ).toShadowContainer(context, backgroundColor: AppColor.neutral100, borderRadius: 10, paddingObject: EdgeInsets.all(12.toScreenHeight), showShadow: false);
}
