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/all_requests_and_count_model.dart';
import 'package:test_sa/models/new_models/dashboard_detail.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/widgets/requests/request_status.dart';

import 'task_request_detail_view.dart';

class TaskRequestItemView extends StatelessWidget {
  final Data? requestData;
  final RequestsDetails? requestDetails;
  final bool showShadow;

  const TaskRequestItemView({Key? key, this.requestData, this.requestDetails, this.showShadow = true}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (requestData != null) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              StatusLabel(
                label: requestData!.priorityName!,
                textColor: AppColor.getRequestStatusTextColorByName(context, requestData!.priorityName!),
                backgroundColor: AppColor.getRequestStatusColorByName(context, requestData!.priorityName!),
              ),
              8.width,
              StatusLabel(
                label: requestData!.statusName!,
                textColor: AppColor.getRequestStatusTextColorByName(context, requestData!.statusName!),
                backgroundColor: AppColor.getRequestStatusColorByName(context, requestData!.statusName!),
              ),
              1.width.expanded,
              Text(
                requestData!.transactionDate?.toServiceRequestCardFormat ?? "",
                textAlign: TextAlign.end,
                style: AppTextStyles.tinyFont.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral50),
              ),
            ],
          ),
          8.height,
          (requestData?.typeTransaction ?? context.translation.taskRequest).heading5(context),
          infoWidget(label: context.translation.requestNo, value: requestData?.requestNo, context: context),
          infoWidget(label: context.translation.assetName, value: requestData?.assetName, context: context),
          infoWidget(label: context.translation.assetNo, value: requestData?.assetNumber, context: context),
          8.height,
          Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                context.translation.viewDetails,
                style: AppTextStyles.bodyText.copyWith(color: AppColor.blueStatus(context)),
              ),
              4.width,
              Icon(Icons.arrow_forward, color: AppColor.blueStatus(context), size: 14)
            ],
          ),
        ],
      ).toShadowContainer(context, withShadow: showShadow).onPress(() async {
        Navigator.of(context).push(MaterialPageRoute(
            builder: (_) => TaskRequestDetailsView(
                  taskId: requestData!.id!,
                  requestDetails: RequestsDetails(nameOfType: requestData?.nameOfType, status: requestData?.statusName, priority: requestData?.priorityName, date: requestData?.transactionDate),
                )));
      });
    }
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            StatusLabel(
              label: requestDetails!.priority!,
              textColor: AppColor.getRequestStatusTextColorByName(context, requestDetails?.priority!),
              backgroundColor: AppColor.getRequestStatusColorByName(context, requestDetails?.priority!),
            ),
            8.width,
            StatusLabel(
              label: requestDetails!.status!,
              textColor: AppColor.getRequestStatusTextColorByName(context, requestDetails?.status!),
              backgroundColor: AppColor.getRequestStatusColorByName(context, requestDetails?.status!),
            ),
            1.width.expanded,
            Text(
              requestDetails!.date?.toServiceRequestCardFormat ?? "",
              textAlign: TextAlign.end,
              style: AppTextStyles.tinyFont.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral50),
            ),
          ],
        ),
        8.height,
        (requestDetails?.nameOfType ?? context.translation.taskRequest).heading5(context),
        8.height,
        infoWidget(label: context.translation.requestNo, value: requestDetails!.requestNo, context: context),
        infoWidget(label: context.translation.assetName, value: requestDetails!.assetName, context: context),
        infoWidget(label: context.translation.assetNo, value: requestDetails!.assetNo, context: context),
        infoWidget(label: context.translation.assetSN, value: requestDetails!.assetSN, context: context),
        infoWidget(label: context.translation.site, value: requestDetails!.site, context: context),
        8.height,
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              context.translation.viewDetails,
              style: AppTextStyles.bodyText.copyWith(color: AppColor.blueStatus(context)),
            ),
            4.width,
            Icon(Icons.arrow_forward, color: AppColor.blueStatus(context), size: 14)
          ],
        ),
      ],
    ).toShadowContainer(context, withShadow: showShadow).onPress(() async {
      Navigator.of(context).push(MaterialPageRoute(
          builder: (_) => TaskRequestDetailsView(
                taskId: requestDetails!.id!,
                requestDetails: requestDetails,
              )));
    });
  }

  Widget infoWidget({required String label, String? value, required BuildContext context}) {
    if (value != null && value.isNotEmpty) {
      return '$label:  $value'.bodyText(context);
    }
    return const SizedBox();
  }
}
