/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package macampcorp.macamp.ui;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import java.io.IOException;

import macampcorp.macamp.activities.R;

public class Sound {
    public interface MediaCompletionListener{
        void completed(int id);
    }
    public static final int BOOM = 0;
    public static final int OK = 1;
    public static final int FAIL = 2;

    protected static int mIds[] = {R.raw.boom, R.raw.ok, R.raw.fail};
    protected static MediaPlayer mPlayers[] = new MediaPlayer[mIds.length];




    protected static MediaPlayer.OnCompletionListener mM = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {

        }
    };

    private static void precache(Activity a,int x)
    {
        MediaPlayer p = mPlayers[x];
        int res = mIds[x];

        if(p==null)
            p=mPlayers[x]=MediaPlayer.create(a, mIds[x]);
        p.setOnCompletionListener(mM);
        p.setAudioStreamType(AudioManager.STREAM_MUSIC);
        p.setLooping(false);

    }

    public static void init(Activity a)
    {
        for(int i = 0; i<mIds.length; i++)
            precache(a, i);
    }

    public static void play(Activity a, int x)
    {
        MediaPlayer p = mPlayers[x];
        int res = mIds[x];
        p.start();
    }


    public static void boom(Activity a)
    {
        play(a, BOOM);
    }

    public static void ok(Activity a)
    {
        play(a, OK);
    }

    public static void fail(Activity a)
    {
        play(a, FAIL);
        vibrate(a,500);
        animateFail(a);
    }


    public static void animateFail(Activity a)
    {
        View v = a.getWindow().getDecorView().findViewById(android.R.id.content);
        final Animation animShake = AnimationUtils.loadAnimation(a, R.anim.shake);
        v.startAnimation(animShake);
    }

    public static void vibrate(Activity a, int ms)
    {
        Vibrator v = (Vibrator) a.getSystemService(Context.VIBRATOR_SERVICE);
        // Vibrate for 500 milliseconds
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            v.vibrate(VibrationEffect.createOneShot(500,VibrationEffect.DEFAULT_AMPLITUDE));
        }else{
            //deprecated in API 26
            v.vibrate(500);
        }
    }
}
