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/views/widgets/equipment/single_device_picker.dart';

import '../../../models/device/asset.dart';
import '../../../new_views/app_style/app_color.dart';
import '../../../new_views/common_widgets/asset_info_card.dart';

class PickAsset extends StatelessWidget {
  final Function(Asset) onPickAsset;
  final Asset? device; // Now nullable
  final bool editable;
  final bool showAssetInfo;
  final Color? cardColor;
  final bool forPPM;

  const PickAsset({Key? key, this.editable = true, this.device, required this.onPickAsset, this.cardColor, this.showAssetInfo = true, this.forPPM = false}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        if (editable)
          Container(
            decoration: BoxDecoration(
              color: cardColor ?? Theme.of(context).cardColor,
              borderRadius: BorderRadius.circular(10),
            ),
            padding: EdgeInsets.symmetric(horizontal: 16.toScreenWidth, vertical: 8.toScreenHeight),
            child: Row(
              children: [
                Expanded(
                  // Wrap the Column with Expanded
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      context.translation.device.tinyFont(context).custom(color: context.isDark ? AppColor.neutral40 : AppColor.neutral120),
                      if (device != null)
                        device!.assetNumber!.bodyText(context).custom(color: context.isDark ? AppColor.neutral40 : AppColor.neutral50)
                      else
                        context.translation.pickAsset.bodyText(context).custom(color: context.isDark ? AppColor.neutral40 : AppColor.neutral50),
                    ],
                  ).onPress(() async {
                    final selectedDevice = await Navigator.of(context).pushNamed(MyAssetsPage.id) as Asset?;
                    if (selectedDevice != null) {
                      onPickAsset(selectedDevice);
                    }
                  }),
                ),
                if (device != null)
                  Icon(Icons.change_circle_rounded, size: 22, color: context.isDark ? AppColor.primary40 : AppColor.primary70)
                else
                  "qr".toSvgAsset(height: 24, fit: BoxFit.fitHeight, color: context.isDark ? AppColor.primary40 : AppColor.primary70),
              ],
            ),
          ),
        if (device != null && showAssetInfo) AssetInfoCard(asset: device!).paddingOnly(top: 8),
      ],
    );
  }
}
