import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:provider/provider.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/modules/asset_delivery_module/models/oracle_user_model.dart';
import 'package:test_sa/modules/asset_delivery_module/provider/asset_delivery_provider.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/app_style/app_text_style.dart';
import 'package:test_sa/views/app_style/sizing.dart';

class OracleUserAutoCompleteField extends StatefulWidget {
  final String initialValue;
  final String label;
  final bool clearAfterPick;
  final Function(OracleUserModel) onPick;
  final Function(String)? onChanged;

  const OracleUserAutoCompleteField({Key? key, required this.initialValue, required this.label, required this.onPick, this.clearAfterPick = true, this.onChanged}) : super(key: key);

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

class _AutoCompletePartsFieldState extends State<OracleUserAutoCompleteField> {
  AssetDeliveryProvider? assetDeliveryProvider;

  // late TextEditingController _controller;

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

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

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

  @override
  Widget build(BuildContext context) {
    assetDeliveryProvider ??= Provider.of<AssetDeliveryProvider>(context);
    final border = UnderlineInputBorder(borderSide: BorderSide.none, borderRadius: BorderRadius.circular(16));
    return Container(
      decoration: BoxDecoration(
        color: AppColor.background(context),
        border: Border.all(color: AppColor.border2Color(context), width: 1),
        borderRadius: BorderRadius.circular(16),
        //  boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 10)],
      ),
      child: Autocomplete<OracleUserModel>(
        key: ValueKey(widget.initialValue),
        optionsBuilder: (TextEditingValue textEditingValue) async {
          if (textEditingValue.text.isEmpty) {
            return const Iterable<OracleUserModel>.empty();
          }
          return await assetDeliveryProvider!.getOracleUserAutoCompleteDetails(query: textEditingValue.text);
        },
        initialValue: TextEditingValue(text: widget.initialValue),
        displayStringForOption: (OracleUserModel option) => option.fullName ?? "",
        fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
          return TextField(
            controller: fieldTextEditingController,
            focusNode: fieldFocusNode,
            style: TextStyle(fontWeight: FontWeight.w500, fontSize: context.isTablet() ? 16 : 14, color: AppColor.textStyleColor(context)),
            textAlign: TextAlign.start,
            decoration: InputDecoration(
              border: border,
              disabledBorder: border,
              focusedBorder: border,
              enabledBorder: border,
              errorBorder: border,
              contentPadding: EdgeInsets.symmetric(vertical: 12.toScreenHeight, horizontal: 16.toScreenWidth),
              constraints: const BoxConstraints(),
              suffixIconConstraints: const BoxConstraints(minWidth: 0),
              filled: true,
              fillColor: AppColor.dropDownBackground(context),
              errorStyle: AppTextStyle.tiny.copyWith(color: context.isDark ? AppColor.red50 : AppColor.red60),
              labelText: widget.label,
              floatingLabelStyle: TextStyle(fontWeight: FontWeight.w500, fontSize: context.isTablet() ? 16 : 12, color: AppColor.floatingTextStyleColor(context)),
              labelStyle: TextStyle(fontWeight: FontWeight.w500, fontSize: context.isTablet() ? 16 : 12, color: AppColor.labelTextStyleColor(context)),

            ),
            textInputAction: TextInputAction.search,
            onChanged: (text) {
              if (widget.onChanged != null) {
                widget.onChanged!(text);
                fieldTextEditingController.text = text;
              }
            },
            onSubmitted: (String value) {
              onFieldSubmitted();
            },
          );
        },
        onSelected: (OracleUserModel selection) {
          // if (widget.clearAfterPick) {
          //   fieldTextEditingController.clear();
          // } else {
          //   _controller.text = selection.fullName ?? '';
          // }
          widget.onPick(selection);
        },
      ),
    );
  }
}
