content: import 'package:anymex/controllers/settings/methods.dart';
  import 'package:anymex/controllers/settings/settings.dart';
  import 'package:anymex/widgets/common/slider_semantics.dart';
  import 'package:anymex/widgets/custom_widgets/custom_icon_wrapper.dart';
  import 'package:anymex/widgets/custom_widgets/custom_text.dart';
  import 'package:anymex/widgets/helper/tv_wrapper.dart';
  import 'package:flutter/material.dart';
  import 'package:flutter/services.dart';
  import 'package:get/get.dart';
  import 'package:iconly/iconly.dart';
  
  class CustomSearchBar extends StatefulWidget {
    final TextEditingController? controller;
    final Function(String)? onChanged;
    final String hintText;
  
    const CustomSearchBar({
      super.key,
      this.controller,
      this.onChanged,
      required this.hintText,
    });
  
    @override
    State<CustomSearchBar> createState() => _CustomSearchBarState();
  }
  
  class _CustomSearchBarState extends State<CustomSearchBar> {
    late FocusNode _focusNode;
    final settings = Get.find<Settings>();
  
    @override
    void initState() {
      super.initState();
      if (settings.isTV.value) {
        _focusNode = FocusNode(
          onKeyEvent: (node, event) {
            if (event is KeyDownEvent) {
              if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
                _focusNode.focusInDirection(TraversalDirection.left);
                return KeyEventResult.skipRemainingHandlers;
              } else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
                _focusNode.focusInDirection(TraversalDirection.right);
                return KeyEventResult.skipRemainingHandlers;
              } else if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
                _focusNode.focusInDirection(TraversalDirection.up);
                return KeyEventResult.skipRemainingHandlers;
              } else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
                _focusNode.focusInDirection(TraversalDirection.down);
                return KeyEventResult.skipRemainingHandlers;
              }
            }
            return KeyEventResult.ignored;
          },
        );
      } else {
        _focusNode = FocusNode();
      }
    }
  
    @override
    void dispose() {
      _focusNode.dispose();
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return TextField(
        focusNode: _focusNode,
        controller: widget.controller,
        onChanged: widget.onChanged,
        decoration: InputDecoration(
          hintText: widget.hintText,
          filled: true,
          fillColor:
              Theme.of(context).colorScheme.secondaryContainer.withOpacity(0.5),
          prefixIcon: const Icon(IconlyLight.search),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(16.multiplyRadius()),
            borderSide: BorderSide(
              color: Theme.of(context).colorScheme.secondaryContainer,
              width: 1,
            ),
          ),
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(16.multiplyRadius()),
            borderSide: BorderSide(
              color: Theme.of(context).colorScheme.secondaryContainer,
              width: 1,
            ),
          ),
        ),
      );
    }
  }
  
  class CustomSliderTile extends StatelessWidget {
    final IconData icon;
    final String title;
    final String description;
    final double sliderValue;
    final double max;
    final double min;
    final double? divisions;
    final String? label;
    final Function(double value) onChanged;
    final Function(double value)? onChangedEnd;
  
    const CustomSliderTile({
      super.key,
      required this.icon,
      this.label,
      required this.title,
      required this.description,
      required this.sliderValue,
      required this.onChanged,
      this.onChangedEnd,
      required this.max,
      this.divisions,
      this.min = 0.0,
    });
  
    @override
    Widget build(BuildContext context) {
      return AnymexOnTapAdv(
        onKeyEvent: (p0, e) {
          if (e is KeyDownEvent) {
            double step = (max - min) / (divisions ?? (max - min));
  
            if (e.logicalKey == LogicalKeyboardKey.arrowRight) {
              double newValue = (sliderValue + step).clamp(min, max);
              onChanged(newValue);
              return KeyEventResult.handled;
            } else if (e.logicalKey == LogicalKeyboardKey.arrowLeft) {
              double newValue = (sliderValue - step).clamp(min, max);
              onChanged(newValue);
              return KeyEventResult.handled;
            }
          } else if (e is KeyUpEvent) {
            return KeyEventResult.handled;
          }
          return KeyEventResult.ignored;
        },
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
          child: Column(
            children: [
              Row(
                children: [
                  AnymexIcon(icon,
                      size: 30, color: Theme.of(context).colorScheme.primary),
                  const SizedBox(width: 20),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          title,
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.w600,
                            color: Theme.of(context).colorScheme.onSurface,
                          ),
                        ),
                        const SizedBox(height: 5),
                        Text(
                          description,
                          style: TextStyle(
                            fontSize: 14,
                            color: Theme.of(context)
                                .colorScheme
                                .onSurface
                                .withOpacity(0.6),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 20),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 10.0),
                child: Row(
                  children: [
                    AnymexText(
                      text: sliderValue.toInt() == 0
                          ? 'Auto'
                          : (sliderValue % 1 == 0
                              ? sliderValue.toInt().toString()
                              : sliderValue.toStringAsFixed(1)),
                      variant: TextVariant.semiBold,
                    ),
                    const SizedBox(width: 10),
                    Expanded(
                      child: CustomSlider(
                        focusNode: FocusNode(
                            canRequestFocus: false, skipTraversal: true),
                        value: double.parse(sliderValue.toStringAsFixed(1)),
                        onChanged: onChanged,
                        max: max,
                        min: min,
                        label: label ?? sliderValue.toInt().toString(),
                        onDragEnd: onChangedEnd,
                        glowBlurMultiplier: 1,
                        glowSpreadMultiplier: 1,
                        divisions: divisions?.toInt() ?? (max * 10).toInt(),
                        customValueIndicatorSize: RoundedSliderValueIndicator(
                            Theme.of(context).colorScheme,
                            width: 40,
                            height: 40,
                            radius: 50),
                      ),
                    ),
                    const SizedBox(width: 10),
                    AnymexText(
                      text: max % 1 == 0
                          ? max.toInt().toString()
                          : max.toStringAsFixed(1),
                      variant: TextVariant.semiBold,
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      );
    }
  }
  