import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_inspection/presentation/widget/base_page/logic.dart' hide BaseLogic;


class SearchWidget2 extends StatefulWidget {
  const SearchWidget2({super.key, this.onSearchChanged});

  final void Function(String?)? onSearchChanged;

  @override
  State<SearchWidget2> createState() => _SearchWidgetState();
}

class _SearchWidgetState extends State<SearchWidget2> {
  late final BaseLogic controller;
  final TextEditingController textEditingController = TextEditingController();

  @override
  void initState() {
    super.initState();
    controller = Get.find<BaseLogic>();
    controller.setSearchCallback(widget.onSearchChanged);
  }

  @override
  Widget build(BuildContext context) {
    return ObxValue((data) {
      return AnimatedContainer(
        duration: const Duration(milliseconds: 300),
        curve: Curves.easeInOut,
        height: data.value ? 40 : 0,
        child: Visibility(
          visible: data.value,
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8),
            child: RTextField(
              height: 40,
              borderColor: AppColor.blackLight,
              suffixIcon: ObxValue(
                (data) => Padding(
                  padding: const EdgeInsets.symmetric(vertical: 8),
                  child: (data.value == null)
                      ? Assets.vec.searchSvg.svg(
                          width: 10,
                          height: 10,
                          colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
                        )
                      : IconButton(
                          onPressed: () {
                            textEditingController.clear();
                            controller.searchValue.value = null;
                            controller.isSearchSelected.value = false;
                            widget.onSearchChanged?.call(null);
                          },
                          enableFeedback: true,
                          padding: EdgeInsets.zero,
                          iconSize: 24,
                          splashRadius: 50,
                          icon: Assets.vec.closeCircleSvg.svg(
                            width: 20,
                            height: 20,
                            colorFilter: ColorFilter.mode(AppColor.blueNormal, BlendMode.srcIn),
                          ),
                        ),
                ),
                controller.searchValue,
              ),
              hintText: 'جستجو کنید ...',
              hintStyle: AppFonts.yekan16.copyWith(color: AppColor.blueNormal),
              filledColor: Colors.white,
              filled: true,
              controller: textEditingController,
              onChanged: (val) => controller.searchValue.value = val,
            ),
          ),
        ),
      );
    }, controller.isSearchSelected);
  }
}
