import 'dart:async';

import 'package:get/get.dart';
import 'package:tone_snap/data/models/voice_model.dart';
import 'package:tone_snap/modules/sidea/controllers/player_controller.dart';
import 'package:tone_snap/modules/sidea/initial/initial_controller.dart';
import 'package:tone_snap/routes/app_routes.dart';

class PlaySoundController extends GetxController {
  var playerController = PlayerController.to;
  late VoiceModel voiceModel;

  @override
  void onInit() {
    super.onInit();
    voiceModel = Get.arguments;
  }

  @override
  void onReady() {
    super.onReady();
    InitialController.to.isFavourite.value = InitialController.to.getIsFavouriteModel() != null;
    if (playerController.filePath != voiceModel.path || playerController.isCompleted.value) {
      Future.delayed(const Duration(milliseconds: 200), () async {
        // 初始化操作
        await playerController.setFilePath(voiceModel.path);
        await playerController.startPlay();
      });
    }
  }

  Future<void> togglePlayback() async {
    playerController.isPlaying.value
        ? await playerController.pausePlay()
        : await playerController.startPlay();
  }

  void goChangeVoice() async {
    await PlayerController.to.stopPlay();
    Get.toNamed(AppRoutes.changeVoice, arguments: voiceModel.path);
  }
}
