import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/service_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/service_request/service_request.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/new_views/common_widgets/default_app_bar.dart';
import 'package:test_sa/views/pages/sub_workorder/create_sub_workorder_page.dart';
import 'package:test_sa/views/pages/user/requests/work_order/update_service_report.dart';
import 'package:test_sa/views/widgets/images/files_list.dart';
import 'package:test_sa/views/widgets/loaders/app_loading.dart';

import '../../../../../controllers/providers/api/user_provider.dart';
import '../../../../../models/enums/user_types.dart';
import '../../../../../models/service_request/search_work_order.dart';
import '../../../../../models/service_request/service_report.dart';
import '../../../../widgets/requests/request_status.dart';

class WorkOrderDetailsPage extends StatelessWidget {
  static const String id = "/work_order_details_page";
  final SearchWorkOrder? workOrder;
  final ServiceRequest? serviceRequest;

  const WorkOrderDetailsPage({Key? key, this.workOrder, this.serviceRequest}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    UserProvider userProvider = Provider.of<UserProvider>(context);
    return Scaffold(
      appBar: DefaultAppBar(title: context.translation.activities),
      body: FutureBuilder<ServiceReport>(
          future: Provider.of<ServiceRequestsProvider>(context).getSingleServiceReport(context, reportId: workOrder!.id!.toInt()),
          builder: (context, snapshot) {
            final workOrderDetails = snapshot.data;
            return Column(
              children: [
                snapshot.connectionState == ConnectionState.waiting
                    ? const Expanded(child: ALoading())
                    : SingleChildScrollView(
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                StatusLabel(
                                  label: workOrderDetails?.currentSituation?.name,
                                  textColor: AppColor.getRequestStatusTextColorByName(context, workOrderDetails?.currentSituation?.name),
                                  backgroundColor: AppColor.getRequestStatusColorByName(context, workOrderDetails?.currentSituation?.name),
                                ),
                                8.height,
                                Text(serviceRequest!.requestCode!, style: AppTextStyles.heading5.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral50)),
                                8.height,
                                Text(
                                  '${context.translation.assetName}: ${workOrderDetails?.callRequest?.asset?.modelDefinition?.assetName?.cleanupWhitespace.capitalizeFirstOfEach}',
                                  style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20),
                                ),
                                Text(
                                  '${context.translation.orderWorkNumber}:  ${workOrderDetails!.workOrderNo}',
                                  style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20),
                                ),
                                Text(
                                  '${context.translation.assignedEmployee}:  ${workOrderDetails.assignedEmployee?.name ?? ""}',
                                  style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20),
                                ),
                                Text('${context.translation.assetSN}:  ${workOrderDetails.callRequest?.asset?.assetSerialNo ?? ""}',
                                    style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20)),
                                Text('${context.translation.assetName}:  ${workOrderDetails.callRequest?.asset?.modelDefinition?.assetName ?? ""}',
                                    style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20)),
                                Text('${context.translation.assetNumber}:  ${workOrderDetails.callRequest?.asset?.assetNumber ?? ""}',
                                    style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20)),
                                Text('${context.translation.model}:  ${workOrderDetails.callRequest?.asset?.modelDefinition?.modelName ?? ""}',
                                    style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20)),
                                Text('${context.translation.site}:  ${workOrderDetails.callRequest?.asset?.site?.custName ?? ""}',
                                    style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20)),
                                Text('${context.translation.currentSituation}:  ${workOrderDetails.currentSituation?.name}',
                                    style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20)),
                                Text('Situation:  ${workOrderDetails.calllastSituation?.name ?? '' ?? ""}',
                                    style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20)),
                                Text('${context.translation.startDate}:  ${workOrderDetails.startofWorkTime?.toServiceRequestDetailsFormat ?? "" ?? '' ?? ""}',
                                    style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20)),
                                Text('${context.translation.endDate}:  ${workOrderDetails.endofWorkTime?.toServiceRequestDetailsFormat ?? "" ?? '' ?? ""}',
                                    style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20)),
                                if (workOrderDetails.attachmentsWorkOrder?.isNotEmpty ?? false) ...[
                                  8.height,
                                  const Divider().defaultStyle(context),
                                  FilesList(images: workOrderDetails.attachmentsWorkOrder!.map((e) => e.name!).toList()),
                                ],
                              ],
                            ).expanded,
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.end,
                              children: [
                                if (userProvider.user!.type == UsersTypes.engineer && serviceRequest?.statusValue != 5 && serviceRequest?.statusValue != 3)
                                  Align(alignment: AlignmentDirectional.centerEnd, child: "edit_icon".toSvgAsset(height: 21, width: 21)).onPress(() {
                                    Navigator.of(context).push(MaterialPageRoute(builder: (_) => UpdateServiceReport(workOrder: workOrder!, request: serviceRequest!)));
                                  }),
                                // if (userProvider.user.type == UsersTypes.engineer && serviceRequest.statusValue != 5 && serviceRequest.statusValue != 3) 8.height,
                                // Text(workOrderDetails.endofWorkTime?.toServiceRequestCardFormat ?? "",
                                //     style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20)),
                              ],
                            )
                          ],
                        ).toShadowContainer(context),
                      ).paddingAll(16).expanded,
                if (userProvider.user!.type == UsersTypes.engineer && serviceRequest?.statusValue != 5 && serviceRequest?.statusValue != 3)
                  AppFilledButton(
                    onPressed: () {
                      Navigator.of(context).push(
                        MaterialPageRoute(builder: (context) => CreateSubWorkOrderPage(workOrder: workOrder)),
                      );
                    },
                    label: context.translation.createSubWorkOrder,
                  ).paddingAll(16),
              ],
            );
          }),
    );
  }
}
