import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:provider/provider.dart';

import '../../../controllers/localization/localization.dart';
import '../../../controllers/providers/api/hospitals_provider.dart';
import '../../../controllers/providers/settings/setting_provider.dart';
import '../../../models/hospital.dart';
import '../../../models/subtitle.dart';
import '../../../models/user.dart';
import '../../app_style/colors.dart';
import '../../app_style/sizing.dart';
import 'hospital_item.dart';

class HospitalAutoCompleteField extends StatefulWidget {
  final String? initialValue;
  final Function(String)? onSearch;
  final Function(String)? onSave;

  const HospitalAutoCompleteField({Key? key, this.onSearch, this.initialValue, this.onSave}) : super(key: key);

  @override
  _HospitalAutoCompleteFieldState createState() => _HospitalAutoCompleteFieldState();
}

class _HospitalAutoCompleteFieldState extends State<HospitalAutoCompleteField> {
  SettingProvider? _settingProvider;
  late TextEditingController _controller;

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

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

  @override
  Widget build(BuildContext context) {
    _settingProvider = Provider.of<SettingProvider>(context);
    Subtitle? _subtitle = AppLocalization.of(context)?.subtitle;
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16),
      decoration: BoxDecoration(
        color: AColors.inputFieldBackgroundColor,
        border: Border.all(
          color: Color(0xffefefef),
        ),
        borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context)),
        // boxShadow: [
        //   AppStyle.boxShadow
        // ]
      ),
      child: TypeAheadField<Hospital>(
        textFieldConfiguration: TextFieldConfiguration(
            style: Theme.of(context).textTheme.headline6,
            onSubmitted: widget.onSave,
            controller: _controller,
            textAlign: TextAlign.center,
            decoration: InputDecoration(
              hintText: _subtitle?.hospital,
              border: InputBorder.none,
              disabledBorder: InputBorder.none,
              focusedBorder: InputBorder.none,
              enabledBorder: InputBorder.none,
            ),
            textInputAction: TextInputAction.search,
            onEditingComplete: () {
              widget.onSearch!(_controller.text);
            }),
        suggestionsCallback: (vale) async {
          return await HospitalsProvider().getHospitalsList(host: _settingProvider?.host ?? "", title: vale, user: User());
        },
        itemBuilder: (context, hospital) {
          return HospitalItem(
            hospital: hospital,
          );
        },
        onSuggestionSelected: (hospital) {
          widget.onSearch!(hospital.name ?? "");
        },
      ),
    );
  }
}
