import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.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/cm_module/cm_detail_provider.dart';
import 'package:test_sa/modules/cm_module/views/forms/asset_retired/verify_asset_detail.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/views/widgets/requests/request_status.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/views/widgets/item_views/info_header_widget.dart';
import 'package:test_sa/views/widgets/item_views/info_text_widget.dart';

class AssetDetailCard extends StatelessWidget {
  const AssetDetailCard({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<CMDetailProvider>(builder: (context, CMDetailProvider requestDetailProvider, snapshot) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            Row(
              children: [
                StatusLabel(
                  label: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.equipmentStatus?.name,
                  id: requestDetailProvider.engineerUpdateWorkOrderHelperModel!.equipmentStatus!.id ?? 0,
                  radius: 4,
                  textColor: AppColor.getEquipmentStatusTextColor(context, requestDetailProvider.engineerUpdateWorkOrderHelperModel!.equipmentStatus!.id ?? 0),
                  backgroundColor: AppColor.getEquipmentStatusColor(context, requestDetailProvider.engineerUpdateWorkOrderHelperModel!.equipmentStatus!.id ?? 0),
                ),
                if (requestDetailProvider.engineerUpdateWorkOrderHelperModel?.cmFrameId != null) ...[
                  6.width,
                  StatusLabel(
                    label: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.cmFrameId?.name,
                    id: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.cmFrameId?.id ?? 0,
                    radius: 4,
                    textColor: AppColor.getActivityTypeTextColor(requestDetailProvider.engineerUpdateWorkOrderHelperModel!.cmFrameId?.name ?? ''),
                    backgroundColor: AppColor.getActivityTypeBgColor(requestDetailProvider.engineerUpdateWorkOrderHelperModel!.cmFrameId?.name ?? ''),
                  ),
                ],
                6.width,
                StatusLabel(
                  label: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.serviceType?.name,
                  id: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.serviceType?.id ?? 0,
                  radius: 4,
                  textColor: AppColor.getActivityTypeTextColor(requestDetailProvider.engineerUpdateWorkOrderHelperModel!.serviceType?.name ?? ''),
                  backgroundColor: AppColor.getActivityTypeBgColor(requestDetailProvider.engineerUpdateWorkOrderHelperModel!.serviceType?.name ?? ''),
                ),
              ],
            ),
            //hide if status is closed or complete .
            if (!requestDetailProvider.isReadOnlyRequest)
              "edit_icon".toSvgAsset(height: 24, width: 24, color: AppColor.icon2Color(context)).onPress(() async {
                requestDetailProvider.refreshTimer = false;
                await Navigator.push(context, MaterialPageRoute(builder: (context) => VerifyAssetDetails(isEdit: true)));
                requestDetailProvider.refreshTimer = true;
              }),
          ]),
          6.height,
          InfoHeader16Widget("Work Order Details"),
          InfoTextWidget(
            label: context.translation.returnToService,
            value: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.returnToService?.toAssetDetailsFormat ?? '-',
          ),
          InfoTextWidget(
            label: 'EDD',
            value: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.edd?.toAssetDetailsFormat ?? '-',
          ),
          InfoTextWidget(
            label: context.translation.loanAvailability,
            value: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.loanAvailability?.name ?? '-',
          ),
          InfoTextWidget(
            label: context.translation.failureReason,
            value: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.failureReason?.name ?? '-',
          ),
          InfoTextWidget(
            label: context.translation.faultDescription,
            value: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.faultDescription?.name ?? '-',
          ),
          InfoTextWidget(
            label: context.translation.solutions,
            value: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.solution ?? '-',
          ),
          if (Provider.of<SettingProvider>(context, listen: false).isUserFMS) ...[
            InfoTextWidget(
              label: 'Description of Finding',
              value: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.descriptionOfFinding ?? '-',
            ),
            InfoTextWidget(
              label: 'Action Taken',
              value: requestDetailProvider.engineerUpdateWorkOrderHelperModel?.actionTaken ?? '-',
            ),
          ]
        ],
      ).toShadowContainer(
        context,
      );
    });
  }
}
