import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tone_snap/routes/app_routes.dart';

class MusicBarController extends GetxController with GetSingleTickerProviderStateMixin {
  static MusicBarController get to => Get.find<MusicBarController>();
  AnimationController? _controller;
  var bottom = 0.0.obs;
  double bottomPadding = 0.0;

  @override
  void onInit() {
    super.onInit();
    _controller = AnimationController(vsync: this)..duration = const Duration(milliseconds: 200);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      bottomPadding = MediaQuery.of(Get.context!).padding.bottom;
      bottom.value = kBottomNavigationBarHeight + bottomPadding;
    });
  }

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

  /// 底部导航栏消失时沉底
  void toBottom() {
    var animation = Tween<double>(begin: bottom.value, end: bottomPadding).animate(_controller!);
    animation.addListener(() {
      bottom.value = animation.value;
    });
    _controller!.forward();
  }

  /// 底部导航栏出现时抬高
  void riseUp() {
    var animation = Tween<double>(begin: bottom.value, end: kBottomNavigationBarHeight + bottomPadding).animate(_controller!);
    animation.addListener(() {
      bottom.value = animation.value;
    });
    _controller!.forward();
  }

  /// 打开播放页面
  void openPlayPage() {
    Get.toNamed(AppRoutes.playPage, arguments: {'isMusicBarOpen': true});
  }
}
