import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:test_sa/controllers/api_routes/urls.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/modules/cm_module/cm_request_utils.dart';
import 'package:test_sa/modules/loan_module/models/loan_request_model.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/widgets/images/files_list.dart';

class InstallationDetailsView extends StatelessWidget {
  final LoanRequestModel loanData;

  const InstallationDetailsView({super.key, required this.loanData});

  @override
  Widget build(BuildContext context) {
    String workingTime = "-";

    if (loanData.startTime != null && loanData.endTime != null) {
      try {
        final timeFormat = DateFormat('h:mm a'); // Matches "3:00 PM" format
        final now = DateTime.now();

        final start = timeFormat.parse(loanData.startTime!);
        var end = timeFormat.parse(loanData.endTime!);

        // Set the date to today for both times
        var startDateTime = DateTime(now.year, now.month, now.day, start.hour, start.minute);
        var endDateTime = DateTime(now.year, now.month, now.day, end.hour, end.minute);

        // If end time is before start time, shift crossed midnight
        if (endDateTime.isBefore(startDateTime)) {
          endDateTime = endDateTime.add(const Duration(days: 1));
        }

        double totalWorkingTime = endDateTime.difference(startDateTime).inSeconds / 3600.0;
      } catch (ex) {}
    }

    // if (loanData.startTime != null && loanData.endTime != null) {
    //   final start = DateTime.parse(loanData.startTime!);
    //   var end = DateTime.parse(loanData.endTime!);
    //
    //   // If end time is before start time, it means the shift crossed midnight
    //   if (end.isBefore(start)) {
    //     end = end.add(const Duration(days: 1));
    //   }
    //
    //   double totalWorkingTime = end.difference(start).inSeconds / 3600.0;
    //   workingTime = CMRequestUtils.formatTotalWorkingHours(totalWorkingTime);
    // }

    // if (loanData.startTime != null && loanData.endTime != null) {
    //   final start = DateTime.parse(loanData.startTime!);
    //   final end = DateTime.parse(loanData.endTime!);
    //   double totalWorkingTime = end.difference(start).inSeconds / 3600.0;
    //   workingTime = CMRequestUtils.formatTotalWorkingHours(totalWorkingTime);
    // }

    return Padding(
      padding: const EdgeInsets.all(16),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            "Installation Details".addTranslation,
            style: AppTextStyles.heading6.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral50),
          ),
          4.height,
          'Updates SN: ${loanData.assetSerialNumber ?? "-"}'.bodyText(context),
          'Installation Date: ${loanData.installationDate?.toAssetDetailsFormat ?? "-"}'.bodyText(context),
          'Start Time: ${loanData.startTime ?? "-"}'.bodyText(context),
          'End Time: ${loanData.endTime ?? "-"}'.bodyText(context),
          'Total Working Time: $workingTime'.bodyText(context),
          if (loanData.loanAttachments!.isNotEmpty) ...[
            12.height,
            Text(
              "Uploads Phase 2 docs".addTranslation,
              style: AppTextStyles.heading6.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral50),
            ),
            8.height,
            FilesList(showAsListView: true, images: loanData.loanAttachments?.map((e) => URLs.getFileUrl(e.name ?? '') ?? '').toList() ?? []),
          ],
        ],
      ).toShadowContainer(context, borderRadius: 20),
    );
  }
}
