package space.animefu.starfallhaven;

import android.content.*;
import android.media.*;
import java.util.*;

/** Owns packaged original sound effects and looping ambience. */
public final class GameAudioManager {
    public enum Cue { TAP, GATHER, BUILD, RAID, RESONANCE }
    private static final String PREFS="starfall_settings",MUTED="muted";
    private final Context context;private final SharedPreferences prefs;private final SoundPool pool;private final EnumMap<Cue,Integer> sounds=new EnumMap<>(Cue.class);private MediaPlayer ambience;private boolean muted;
    public GameAudioManager(Context c){context=c.getApplicationContext();prefs=context.getSharedPreferences(PREFS,Context.MODE_PRIVATE);muted=prefs.getBoolean(MUTED,false);pool=new SoundPool.Builder().setMaxStreams(5).setAudioAttributes(new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_GAME).setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build()).build();sounds.put(Cue.TAP,pool.load(context,R.raw.ui_tap,1));sounds.put(Cue.GATHER,pool.load(context,R.raw.gather_crystal,1));sounds.put(Cue.BUILD,pool.load(context,R.raw.build_knock,1));sounds.put(Cue.RAID,pool.load(context,R.raw.raid_warning,1));sounds.put(Cue.RESONANCE,pool.load(context,R.raw.resonance_chime,1));}
    public boolean isMuted(){return muted;}public void toggleMuted(){muted=!muted;prefs.edit().putBoolean(MUTED,muted).apply();if(muted)pause();else resume();}
    public void play(Cue cue){if(!muted){Integer id=sounds.get(cue);if(id!=null)pool.play(id,1,1,1,0,1);}}
    public void resume(){if(muted)return;if(ambience==null){ambience=MediaPlayer.create(context,R.raw.night_ambience);if(ambience!=null){ambience.setLooping(true);ambience.setVolume(.34f,.34f);}}if(ambience!=null&&!ambience.isPlaying())ambience.start();}
    public void pause(){if(ambience!=null&&ambience.isPlaying())ambience.pause();}
    public void release(){if(ambience!=null){ambience.release();ambience=null;}pool.release();}
}
