import 'package:get/get.dart';
import 'package:tone_snap/components/base_easyloading.dart';
import 'package:tone_snap/controllers/player_controller.dart';
import 'package:tone_snap/data/models/voice_model.dart';
import 'package:tone_snap/generated/assets.dart';
import 'package:tone_snap/modules/voice/voice_initial/voice_initial_controller.dart';
import 'package:tone_snap/routes/app_routes.dart';

class VoiceHomeController extends GetxController {
  static VoiceHomeController get to => Get.find<VoiceHomeController>();
  var playerController = PlayerController.to;
  var voiceList = [
    VoiceModel(name: 'Baby', path: Assets.audioChild, cover: Assets.coverChild),
    VoiceModel(name: 'Boy', path: Assets.audioBoy, cover: Assets.coverBoy),
    VoiceModel(name: 'Man', path: Assets.audioMan, cover: Assets.coverMan),
    VoiceModel(name: 'Elder', path: Assets.audioElder, cover: Assets.coverElder),
    VoiceModel(name: 'Robot', path: Assets.audioRobot, cover: Assets.coverRobot),
    VoiceModel(name: 'Girl', path: Assets.audioGirl, cover: Assets.coverGirl),
    VoiceModel(name: 'Sexy', path: Assets.audioSexy, cover: Assets.coverSexy),
    VoiceModel(name: 'Woman', path: Assets.audioWoman, cover: Assets.coverWoman),
    VoiceModel(name: 'Monsters', path: Assets.audioMonsters, cover: Assets.coverMonsters),
  ];

  void onTapItem(VoiceModel item) {
    VoiceInitialController.to.currentPlayVoiceModel.value = item;
    Get.toNamed(AppRoutes.playSound, arguments: item);
  }

  Future<void> onTapPlayBarPlay(VoiceModel item) async {
    if (isPlayItem(item)) {
      await playerController.pausePlay();
    } else {
      if (!identical(item, VoiceInitialController.to.currentPlayVoiceModel.value)) {
        BaseEasyLoading.loading();
        await playerController.setFilePath(item.path);
        BaseEasyLoading.dismiss();
        VoiceInitialController.to.currentPlayVoiceModel.value = item;
        VoiceInitialController.to.isFavourite.value = VoiceInitialController.to.getIsFavouriteModel() != null;
      }
      await playerController.startPlay();
    }
  }

  bool isPlayItem(VoiceModel item) {
    return playerController.isPlaying.value && identical(VoiceInitialController.to.currentPlayVoiceModel.value, item);
  }
}