import 'package:get/get.dart';
import 'package:tone_snap/components/base_easyloading.dart';
import 'package:tone_snap/data/models/voice_model.dart';
import 'package:tone_snap/generated/assets.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 HomeController extends GetxController {
  static HomeController get to => Get.find<HomeController>();
  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) {
    InitialController.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, InitialController.to.currentPlayVoiceModel.value)) {
        BaseEasyLoading.loading();
        await playerController.setFilePath(item.path);
        BaseEasyLoading.dismiss();
        InitialController.to.currentPlayVoiceModel.value = item;
        InitialController.to.isFavourite.value = InitialController.to.getIsFavouriteModel() != null;
      }
      await playerController.startPlay();
    }
  }

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