import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/devices_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/views/app_style/sizing.dart';
import 'package:test_sa/views/widgets/equipment/pick_asset.dart';

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

class CalibrationToolAssetPicker extends StatefulWidget {
  final Lookup? initialValue;
  final int? hospitalId;
  final Function(Asset) onPick;

  const CalibrationToolAssetPicker({Key? key, this.initialValue, required this.onPick, this.hospitalId}) : super(key: key);

  @override
  State<CalibrationToolAssetPicker> createState() => _CalibrationToolAssetPickerState();
}

class _CalibrationToolAssetPickerState extends State<CalibrationToolAssetPicker> {
  late SettingProvider _settingProvider;
  late AssetProvider _devicesProvider;
  late UserProvider _userProvider;
  late TextEditingController _controller;

  @override
  void initState() {
    _controller = TextEditingController(text: widget.initialValue?.name);
    super.initState();
  }

  @override
  void didUpdateWidget(covariant CalibrationToolAssetPicker oldWidget) {
    if (widget.initialValue != oldWidget.initialValue) {
      _controller = TextEditingController(text: widget.initialValue?.name);
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _settingProvider = Provider.of<SettingProvider>(context);
    _userProvider = Provider.of<UserProvider>(context);
    _devicesProvider = Provider.of<AssetProvider>(context);
    final border = UnderlineInputBorder(borderSide: BorderSide.none, borderRadius: BorderRadius.circular(10));
    return Container(
        decoration: BoxDecoration(
          color: AppColor.background(context),
          borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context)),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            PickAsset(
              showAssetInfo: false,
              forPPM: true,
              cardColor: AppColor.neutral100,
              device: widget.initialValue == null
                  ? null
                  : Asset(
                      id: widget.initialValue!.id,
                      assetNumber: widget.initialValue!.name,
                    ),
              onPickAsset: (device) {
                widget.onPick(device);
              },
            ),
          ],
        ));
  }
}
