import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.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/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.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 '../../../../extensions/text_extensions.dart';
import '../../../../models/device/asset.dart';
import '../../../../models/device/asset_search.dart';
import '../../../../new_views/app_style/app_color.dart';
import '../../../../new_views/app_style/app_text_style.dart';

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

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

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

class _CalibrationToolAssetPickerState extends State<CalibrationToolAssetPicker> {
  SettingProvider _settingProvider;
  AssetProvider _devicesProvider;
  UserProvider _userProvider;
  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)),
          boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 10)],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            PickAsset(
              showAssetInfo: false,
              forPPM: true,
              device: widget.initialValue == null
                  ? null
                  : Asset(
                      id: widget.initialValue.id,
                      assetNumber: widget.initialValue.name,
                    ),
              onPickAsset: (device) {
                widget.onPick(Lookup(id: device.id, name: device.assetNumber));
              },
            ),
          ],
        ));
  }
}
