import 'dart:developer';

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/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/modules/asset_delivery_module/models/technical_inspection_asset_model.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/widgets/requests/request_status.dart';

class TechnicalInspectionAssetCard extends StatelessWidget {
  TechnicalInspectionAssetModel assetModel;
  bool isViewOnly;
  bool ischild;
  VoidCallback? editPress;

  TechnicalInspectionAssetCard({
    super.key,
    required this.isViewOnly,
    this.ischild = false,
    required this.assetModel,
    this.editPress,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (!ischild) ...[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              StatusLabel(
                label: getAssetStatus(),
                radius: 4,
                textColor: getAssetStatusColor(context: context, status: getAssetStatus(), isBackground: false),
                backgroundColor: getAssetStatusColor(context: context, status: getAssetStatus()),
              ),
              if (!isViewOnly)
                'edit_icon'.toSvgAsset().onPress(() {
                  if (editPress != null) {
                    editPress!();
                  }
                }),
            ],
          ),
          8.height,
        ],
        if (ischild) ...[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                'Asset Number: ${assetModel.assetNumber ?? '-'}',
                style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
              ),
              if (!isViewOnly)
                'edit_icon'.toSvgAsset().onPress(() {
                  if (editPress != null) {
                    editPress!();
                  }
                }),
            ],
          ),
          2.height,
        ],
        if (!ischild)
        Text(
          'Line No: ${assetModel.lineNo ?? '-'}',
          style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
        ),
        Text(
          'Asset Name: ${assetModel.modelDefinition?.assetName ?? '-'}',
          style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
        ),
        if (!ischild)
          Text(
            'Asset Number: ${assetModel.assetNumber ?? '-'}',
            style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
          ),
        Text(
          'Model: ${assetModel.modelDefinition?.model ?? '-'}',
          style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
        ),
        Text(
          'Manufacturer: ${assetModel.modelDefinition?.manufacturer ?? '-'}',
          style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
        ),
        Text(
          'Serial Number: ${assetModel.serialNo ?? '-'}',
          style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
        ),
        Text(
          'System ID: ${assetModel.systemId ?? '-'}',
          style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
        ),
        Text(
          'Medical Department: ${assetModel.department?.name ?? '-'}',
          style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
        ),
        Text(
          'Installation Date: ${assetModel.installationDate ?? '-'}',
          style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
        ),
        Text(
          'Asset Type: ${assetModel.assetType?.name ?? '-'}',
          style: AppTextStyles.bodyText.copyWith(color: context.isDark ? AppColor.neutral10 : AppColor.neutral120),
        ),
      ],
    ).toShadowContainer(context, borderRadius: 20, padding: 12);
  }

  Color getAssetStatusColor({required BuildContext context, required String status, bool? isBackground = true}) {
    switch (status) {
      case 'PASS':
        return isBackground! ? AppColor.green70.withOpacity(0.4) : AppColor.green15;
      case 'FAIL':
        return isBackground! ? AppColor.red30.withOpacity(0.4) : AppColor.red30;
      default:
        return Colors.white10;
    }
  }

  String getAssetStatus() {
    final flags = [
      assetModel.flagphysicalInspection,
      assetModel.flagFunctionPerformance,
      assetModel.flagGroundingResistance,
      assetModel.flagChassiss,
      assetModel.flagLeadsLeakage,
      assetModel.flagAlert,
    ];
    if (flags.any((f) => f == false)) {
      return 'FAIL';
    }
    if (flags.every((f) => f == true)) {
      return 'PASS';
    }
    return 'FAIL';
  }
}
