import '../../tmg_audio_effect_ctrl.dart';
import '../../tmg_type.dart';
import '../../tmg_error.dart';
import '../../bindings/v2/tmg_audio_effect_ctrl_bindings.dart'
    as TMGAudioEffectCtrlBindings;

class TMGAudioEffectCtrlImpl extends ITMGAudioEffectCtrl {
  late final TMGAudioEffectCtrlBindings.TMGAudioEffectCtrlBindings _instance;
  final int DEFAULT_VALUE = 100;
  TMGAudioEffectCtrlImpl() {
    _instance = new TMGAudioEffectCtrlBindings.TMGAudioEffectCtrlBindings();
  }

  @override
  int StartPlayMusic(int soundId, String filePath, int loopCount) {
    if (soundId == 0) {
      return _instance.StartAccompany(filePath, loopCount);
    }
    return _instance.PlayEffect(soundId, filePath, loopCount > 1);
  }

  @override
  int StopPlayMusic(int soundId) {
    if (soundId == 0) {
      return _instance.StopAccompany(0);
    }
    return _instance.StopEffect(soundId);
  }

  @override
  int PausePlayMusic(int soundId) {
    if (soundId == 0) {
      return _instance.PauseAccompany();
    }
    return TMGError.AV_ERR_NOT_IMPLEMENTED;
  }

  @override
  int ResumePlayMusic(int soundId) {
    if (soundId == 0) {
      return _instance.ResumeAccompany();
    }
    return TMGError.AV_ERR_NOT_IMPLEMENTED;
  }

  @override
  bool IsMusicPlayEnd(int soundId) {
    if (soundId == 0) {
      return _instance.IsAccompanyPlayEnd();
    }
    return true;
  }

  @override
  int SetAllMusicVolume(int volume) {
    int effectRet = _instance.SetEffectsVolume(volume);
    int accompanyRet = _instance.SetAccompanyVolume(volume);
    if (effectRet != TMGError.AV_OK) {
      return effectRet;
    }
    if (accompanyRet != TMGError.AV_OK) {
      return accompanyRet;
    }
    return TMGError.AV_OK;
  }

  @override
  int SetMusicPublishVolume(int soundId, int volume) {
    return TMGError.AV_ERR_NOT_IMPLEMENTED;
  }

  @override
  int GetMusicPublishVolume(int soundId) {
    return TMGError.AV_ERR_NOT_IMPLEMENTED;
  }

  @override
  int SetMusicPlayoutVolume(int soundId, int volume) {
    if (soundId == 0) {
      return _instance.SetAccompanyVolume(volume);
    }
    return _instance.SetEffectVolume(soundId, volume);
  }

  @override
  int GetMusicPlayoutVolume(int soundId) {
    if (soundId == 0) {
      return _instance.GetAccompanyVolume();
    }
    return DEFAULT_VALUE;
  }

  @override
  int SetMusicPitch(int soundId, double pitch) {
    return TMGError.AV_ERR_NOT_IMPLEMENTED;
  }

  @override
  int GetMusicDurationInMS(int soundId) {
    if (soundId == 0) {
      return _instance.GetAccompanyFileTotalTimeByMs();
    }
    return 0;
  }

  @override
  int GetMusicCurrentPosInMS(int soundId) {
    if (soundId == 0) {
      return _instance.GetAccompanyFileCurrentPlayedTimeByMs();
    }
    return 0;
  }

  @override
  int SeekMusicToPosInTime(int soundId, int time) {
    return _instance.SetAccompanyFileCurrentPlayedTimeByMs(time);
  }

  @override
  int EnableMusicPublish(int soundId, bool enable) {
    return TMGError.AV_ERR_NOT_IMPLEMENTED;
  }

  @override
  int EnableMusicPlayout(int soundId, bool enable) {
    return TMGError.AV_ERR_NOT_IMPLEMENTED;
  }

  @override
  int SetVoiceType(ITMG_VOICE_TYPE voiceType) {
    return _instance.SetVoiceType(voiceType);
  }

  @override
  int SetKaraokeType(ITMG_KARAOKE_TYPE type) {
    return _instance.SetKaraokeType(type);
  }

  @override
  int StartRecord(String filePath, ITMG_AUDIO_RECORDING_CONTENT content) {
    return TMGError.AV_ERR_NOT_IMPLEMENTED;
  }

  @override
  int StopRecord() {
    return TMGError.AV_ERR_NOT_IMPLEMENTED;
  }
}
