import 'dart:async';

import 'package:fis_common/event/event_type.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';

class FFreeSlider extends FStatefulWidget implements FInteractiveContainer {
  final double value;
  final double min;
  final double max;
  final double step;
  final Function(double value)? onChanged;
  final FEventHandler<double> onProcessChange;

  @override
  String get pageName => 'FreeSlider';

  FFreeSlider({
    required this.onProcessChange,
    this.value = 0.5,
    this.min = 0.0,
    this.max = 1.0,
    this.step = 0.1,
    this.onChanged,
  });

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

class _FreeSliderState extends FState<FFreeSlider>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _animation;
  double _value = 0;
  Timer? _debounceTimer;
  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 200),
    );
    _animation = CurvedAnimation(
      parent: _animationController,
      curve: Curves.easeInOut,
    );
    _value = widget.value;
    widget.onProcessChange.addListener(_onProcessChange);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      _animationController.animateTo(
        _value,
        duration: Duration(milliseconds: 0),
      );
      setState(() {});
    });
  }

  @override
  FWidget build(BuildContext context) {
    return FGestureDetector(
      onPanUpdate: (dragUpdateDetails) {
        setState(() {
          _value +=
              dragUpdateDetails.delta.dx / 300.0 * (widget.max - widget.min);
          if (_value < widget.min) {
            _value = widget.min;
          } else if (_value > widget.max) {
            _value = widget.max;
          }
          _value = (_value / widget.step).round() * widget.step;
        });
        if (widget.onChanged != null) {
          widget.onChanged!.call(_value);
        }
      },
      name: '',
      businessParent: widget,
      child: FSlider(
        value: _value,
        min: widget.min,
        max: widget.max,
        divisions: ((widget.max - widget.min) / widget.step).round(),
        onChanged: _updateValue,
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _animationController.dispose();
    widget.onProcessChange.removeListener(_onProcessChange);
  }

  void _onProcessChange(Object sender, double e) {
    setState(() {
      _value = e;
    });
  }

  void _updateValue(double newValue) {
    _value = newValue;
    _animationController.reset();
    _animationController.forward();
    if (_debounceTimer?.isActive ?? false) {
      _debounceTimer?.cancel();
    }

    ///防抖
    _debounceTimer = Timer(Duration(milliseconds: 200), () {
      setState(() {});
    });

    _animationController.reset();
    _animationController.forward();
    setState(() {});
    if (widget.onChanged != null) {
      widget.onChanged!.call(_value);
    }
  }
}
