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/models/device/device.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/app_style/sizing.dart';

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

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

  @override
  State<AutoCompleteDeviceNumberField> createState() => _AutoCompleteDeviceNumberFieldState();
}

class _AutoCompleteDeviceNumberFieldState extends State<AutoCompleteDeviceNumberField> {
  SettingProvider _settingProvider;
  DevicesProvider _devicesProvider;
  UserProvider _userProvider;
  TextEditingController _controller;

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

  @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<DevicesProvider>(context);
    //Subtitle _subtitle = AppLocalization.of(context).subtitle;
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border.all(color: AColors.black),
          borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context)),
          boxShadow: const [AppStyle.boxShadow]),
      child: TypeAheadField<Device>(
        textFieldConfiguration: TextFieldConfiguration(
          style: Theme.of(context).textTheme.headline6,
          controller: _controller,
          textAlign: TextAlign.center,
          decoration: const InputDecoration(
            border: InputBorder.none,
            disabledBorder: InputBorder.none,
            focusedBorder: InputBorder.none,
            enabledBorder: InputBorder.none,
          ),
          textInputAction: TextInputAction.search,
        ),
        suggestionsCallback: (vale) async {
          return await _devicesProvider.getDevicesList(
            host: _settingProvider.host,
            user: _userProvider.user,
            hospitalId: widget.hospitalId,
            number: vale,
          );
        },
        itemBuilder: (context, device) {
          return ListTile(
            title: Text(device.number),
            subtitle: Text("${device.modelDefinition.modelName}/${device.modelDefinition.manufacturerName}"),
          );
        },
        onSuggestionSelected: (device) {
          _controller.text = device.number;
          widget.onPick(Lookup(id: device.id, name: device.number));
        },
      ),
    );
  }
}
