import 'package:flutter/material.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/new_models/work_order_detail_model.dart';
import 'package:test_sa/modules/cm_module/cm_request_utils.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/widgets/item_views/info_header_widget.dart';
import 'package:test_sa/models/size_config.dart';

class TotalWorkingTimeDetailBottomSheet extends StatelessWidget {
  final List<TimerHistoryModel> timerList;

  const TotalWorkingTimeDetailBottomSheet({Key? key, this.timerList = const []}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: SizeConfig.screenHeight! / 2.2,
      width: SizeConfig.screenWidth,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        // crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 40.toScreenWidth,
            height: 5.toScreenHeight,
            decoration: BoxDecoration(color: AppColor.neutral40, borderRadius: BorderRadius.circular(30)),
          ),
          16.height,
          Align(
            alignment: AlignmentDirectional.centerStart,
            child: InfoHeader16Widget("Total Working Time".addTranslation),
          ),
          8.height,
          ListView.separated(
              itemCount: timerList.length,
              separatorBuilder: (cxt, index) => const Divider().defaultStyle(context),
              padding: const EdgeInsets.only(bottom: 16),
              itemBuilder: (cxt, index) {
                int totalWorkingHours = DateTime.parse(timerList[index].endTime!).difference(DateTime.parse(timerList[index].startTime!)).inSeconds;
                return Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    RichText(
                        text: TextSpan(text: "From: ", style: AppTextStyles.bodyText.copyWith(color: AppColor.lightTextColor(context)), children: [
                      TextSpan(
                          text: timerList[index].startTime!.toServiceRequestDetailsFormatWithSS,
                          style: AppTextStyles.bodyText.copyWith(fontWeight: FontWeight.normal, color: AppColor.lightTextColor(context)))
                    ])),
                    4.height,
                    RichText(
                        text: TextSpan(text: "To: ", style: AppTextStyles.bodyText.copyWith(color: AppColor.lightTextColor(context)), children: [
                      TextSpan(
                          text: timerList[index].endTime!.toServiceRequestDetailsFormatWithSS,
                          style: AppTextStyles.bodyText.copyWith(fontWeight: FontWeight.normal, color: AppColor.lightTextColor(context)))
                    ])),
                    4.height,
                    RichText(
                        text: TextSpan(text: "Duration: ", style: AppTextStyles.bodyText.copyWith(color: AppColor.lightTextColor(context)), children: [
                      TextSpan(
                          text: " ${CMRequestUtils.formatTimerDuration(totalWorkingHours.round())}",
                          style: AppTextStyles.bodyText.copyWith(fontWeight: FontWeight.normal, color: AppColor.lightTextColor(context)))
                    ])),
                  ],
                );
              }).expanded,
        ],
      ),
    );
  }
}

class TimerHistoryModel {
  int? id;
  String? startTime;
  String? endTime;
  dynamic workingHours;

  TimerHistoryModel({this.id, this.startTime, this.endTime, this.workingHours});

  TimerHistoryModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    startTime = json['startTime'];
    endTime = json['endTime'];
    workingHours = json['workingHours'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['startTime'] = startTime;
    data['endTime'] = endTime;
    data['workingHours'] = workingHours;
    return data;
  }
}
