import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/user_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/enums/user_types.dart';
import 'package:test_sa/models/service_request/search_work_order.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/views/pages/user/requests/work_order/update_service_report.dart';
import 'package:test_sa/views/pages/user/requests/work_order/work_order_details_page.dart';

import '../../../../views/widgets/requests/request_status.dart';

class WorkOrderCardView extends StatelessWidget {
  final SearchWorkOrder workOrder;
  final ServiceRequest serviceRequest;
  final bool showShadow;

  WorkOrderCardView(this.workOrder, this.serviceRequest, {Key? key, this.showShadow = true}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    UserProvider _userProvider = Provider.of<UserProvider>(context, listen: false);
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            StatusLabel(
              label: workOrder.currentSituation!.name,
              textColor: AppColor.getRequestStatusTextColorByName(context, workOrder.currentSituation?.name),
              backgroundColor: AppColor.getRequestStatusColorByName(context, workOrder.currentSituation?.name),
            ),
            8.height,
            Text(serviceRequest.requestCode!, style: AppTextStyles.heading5.copyWith(color: context.isDark ? AppColor.neutral30 : AppColor.neutral50)),
            Text(
              '${context.translation.assetName}: ${workOrder.callRequest?.asset?.modelDefinition?.assetName?.cleanupWhitespace?.capitalizeFirstOfEach}',
              style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20),
            ),
            Text(
              '${context.translation.currentSituation}:  ${workOrder.currentSituation?.name}',
              style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20),
            ),
            16.height,
            Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  context.translation.readCompleteThread,
                  style: AppTextStyles.bodyText.copyWith(color: AppColor.blueStatus(context), decoration: TextDecoration.underline),
                ),
                // 4.width,
                // Icon(Icons.arrow_forward, color: AppColor.blueStatus(context), size: 14)
              ],
            ),
          ],
        ).onPress(() {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => WorkOrderDetailsPage(workOrder: workOrder, serviceRequest: serviceRequest)),
          );
        }).expanded,
        Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            if (_userProvider.user!.type == UsersTypes.engineer && serviceRequest.statusValue != 5 && serviceRequest.statusValue != 3)
              "edit_icon".toSvgAsset(height: 21, width: 21).onPress(() {
                Navigator.of(context).push(
                  MaterialPageRoute(builder: (_) => UpdateServiceReport(request: serviceRequest, workOrder: workOrder)),
                );
              }),
            if (_userProvider.user!.type == UsersTypes.engineer && serviceRequest.statusValue != 5 && serviceRequest.statusValue != 3) 8.height,
            Text(workOrder.visitDate?.toServiceRequestCardFormat ?? "",
                textAlign: TextAlign.end, style: AppTextStyles.tinyFont.copyWith(color: context.isDark ? AppColor.neutral30 : const Color(0xFF3B3D4A))),
          ],
        )
      ],
    ).toShadowContainer(context);
  }
}
