package com.lloydm.geosword;

import java.io.File;
import java.util.Random;

import android.annotation.SuppressLint;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

public class PilotChatter
{
     // actually this is really just sound effects not just pilot chatter....
     private final static String TAG = "com.lloydm.geosword.PilotChatter";
     private SoundPool sndpool = null;

     public final static int fxtype_laser = 0;
     public final static int fxtype_beam = 1;
     public final static int fxtype_explosion = 2;
     public final static int fxtype_wind = 3;
    
     public final static int maxprep = 0;
     public final static int maxbattle = 0;
     public final static int maxbasicchatter = 202;
    
     public static boolean modded = false;
     public static int mybattle = 0;
     public static int myprep = 0;
     public static String battlefolder = "";
     public static String prepfolder = "";
    
     public final static int maxlaser = 2;
     public final static int maxexplosion = 2;
     public final static int maxbeam = 1;
     public final static int maxwind = 2;
    
     private int[] chatterprep = null;
     private int[] chatterbattle = null;

     private int[] basicchatter = null;

     private int[] laserfx = null;
     private int[] beamfx = null;
     private int[] explosionfx = null;
     private int[] windfx = null;
    
     private Random rand = null;
     private int oq = -1;
     private int q = 0;

     @SuppressLint("NewApi")
     @SuppressWarnings("deprecation")
     public PilotChatter(AssetManager assets,final int team)
     {
                     initfolders();
                    
                     rand = new Random();
                     if (Build.VERSION.SDK_INT < 21)
                     {
                                     sndpool = new SoundPool(32, AudioManager.STREAM_MUSIC, 0);
                     }
                     else
                     {
                                     AudioAttributes.Builder attr = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_GAME);
                                     AudioAttributes attrb = attr.build();
                                     SoundPool.Builder sndpoolbuilder = new SoundPool.Builder().setAudioAttributes(attrb).setMaxStreams(32);
                                     sndpool = sndpoolbuilder.build();
                     }
                     modded = false;
                     myprep = 0;
                     mybattle = 0;
                     checkmod(team);
                    
                     if(!modded)
                     {
                                     chatterprep = new int[maxprep * 5];
                                     chatterbattle = new int[maxbattle * 5];
                                     basicchatter = new int[maxbasicchatter];
                     }
                     else
                     {
                                     chatterprep = new int[myprep];
                                     chatterbattle = new int[mybattle];
                                     basicchatter = new int[myprep + mybattle];
                     }
                     laserfx = new int[maxlaser];
                     beamfx = new int[maxbeam];
                     explosionfx = new int[maxexplosion];
                     windfx = new int[maxwind];
                    
                     AssetFileDescriptor fd = null;
                     for (int i = 0; i < maxwind; i++)
                     {
                                     try
                                     {
                                                     fd = assets.openFd("effects/wind" + (i + 1) + ".ogg");

                                                     windfx[i] = sndpool.load(fd, 1);
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.w(TAG, "Sound Effect Not Loaded");
                                     }
                                     try
                                     {
                                                     fd.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing file descriptor");
                                     }
                     }
                     for (int i = 0; i < maxlaser; i++)
                     {
                                     try
                                     {
                                                     fd = assets.openFd("effects/laser" + (i + 1) + ".ogg");

                                                     laserfx[i] = sndpool.load(fd, 1);
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.w(TAG, "Sound Effect Not Loaded");
                                     }
                                     try
                                     {
                                                     fd.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing file descriptor");
                                     }
                     }
                     for (int i = 0; i < maxbeam; i++)
                     {
                                     try
                                     {
                                                     fd = assets.openFd("effects/beam" + (i + 1) + ".ogg");

                                                     beamfx[i] = sndpool.load(fd, 1);
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.w(TAG, "Sound Effect Not Loaded");
                                     }
                                     try
                                     {
                                                     fd.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing file descriptor");
                                     }
                     }
                     for (int i = 0; i < maxexplosion; i++)
                     {
                                     try
                                     {
                                                     fd = assets.openFd("effects/exp" + (i + 1) + ".ogg");

                                                     explosionfx[i] = sndpool.load(fd, 1);
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.w(TAG, "Sound Effect Not Loaded");
                                     }
                                     try
                                     {
                                                     fd.close();
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing file descriptor");
                                     }
                     }
                     for (int i = 0; i < maxprep; i++)
                     {
                                     for (int j = 0; j < 5; j++)
                                     {
                                                     try
                                                     {
                                                                     if (j == 0)
                                                                     {
                                                                                     fd = assets.openFd("chatter/preparatory/earth/" + i + ".ogg");

                                                                     }
                                                                     if (j == 1)
                                                                     {
                                                                                     fd = assets.openFd("chatter/preparatory/aiosenti/" + i + ".ogg");
                                                                     }
                                                                     if (j == 2)
                                                                     {
                                                                                     fd = assets.openFd("chatter/preparatory/mars/" + i + ".ogg");
                                                                     }
                                                                     if (j == 3)
                                                                     {
                                                                                     fd = assets.openFd("chatter/preparatory/hushami/" + i + ".ogg");
                                                                     }
                                                                     if (j == 4)
                                                                     {
                                                                                     fd = assets.openFd("chatter/preparatory/outlander/" + i + ".ogg");
                                                                     }
                                                                     chatterprep[i + j * maxprep] = sndpool.load(fd, 1);
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.w(TAG, "Sound effect not found");
                                                     }
                                                     try
                                                     {
                                                                     fd.close();
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "Error closing file descriptor");
                                                     }
                                     }
                     }
                     for (int i = 0; i < maxbattle; i++)
                     {
                                     for (int j = 0; j < 5; j++)
                                     {
                                                     try
                                                     {
                                                                     if (j == 0)
                                                                     {
                                                                                     fd = assets.openFd("chatter/battley/earth/" + i + ".ogg");

                                                                     }
                                                                     if (j == 1)
                                                                     {
                                                                                     fd = assets.openFd("chatter/battle/aiosenti/" + i + ".ogg");
                                                                     }
                                                                     if (j == 2)
                                                                     {
                                                                                     fd = assets.openFd("chatter/battle/mars/" + i + ".ogg");
                                                                     }
                                                                     if (j == 3)
                                                                     {
                                                                                     fd = assets.openFd("chatter/battle/hushami/" + i + ".ogg");
                                                                     }
                                                                     if (j == 4)
                                                                     {
                                                                                     fd = assets.openFd("chatter/battle/outlander/" + i + ".ogg");
                                                                     }
                                                                     chatterbattle[i + j * maxbattle] = sndpool.load(fd, 1);
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.w(TAG, "Sound effect not found");
                                                     }
                                                     try
                                                     {
                                                                     fd.close();
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "Error closing file descriptor");
                                                     }
                                     }
                     }
                     if(!modded)
                     {
                                     for (int i = 0; i < maxbasicchatter; i++)
                                     {
                                                     try
                                                     {
                                                                     fd = assets.openFd("chatter/" + (i + 1) + ".ogg");

                                                                     basicchatter[i] = sndpool.load(fd, 1);
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.w(TAG, "Sound Effect Not Loaded");
                                                     }
                                                     try
                                                     {
                                                                     fd.close();
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "Error closing file descriptor");
                                                     }
                                     }
                     }
                     else
                     {
                                     for(int i=0;i<myprep;i++)
                                     {
                                                     try
                                                     {
                                                                     chatterprep[i] = sndpool.load(prepfolder+"/"+(i+1)+".ogg", 1);

                                                     }
                                                     catch(Exception e)
                                                     {
                                                                     Log.w(TAG,"could not read chatter prep file");
                                                     }
                                     }
                                     for(int i=0;i<mybattle;i++)
                                     {
                                                     try
                                                     {
                                                                     chatterbattle[i] = sndpool.load(battlefolder+"/"+(i+1)+".ogg", 1);

                                                     }
                                                     catch(Exception e)
                                                     {
                                                                     Log.w(TAG,"could not read battle chatter file");
                                                     }
                                     }
                     }
     }

     public void playfx(final int fxtype, float vl, float vr)
     {
                     if (sndpool == null)
                     {
                                     return;
                     }
                     if (fxtype == fxtype_wind)
                     {
                                     q++;
                                     q %= maxwind;
                                     sndpool.play(windfx[q], 0, vr, 4, 0, 1);
                     }
                     if (fxtype == fxtype_laser)
                     {
                                     q++;
                                     q %= maxlaser;
                                     sndpool.play(laserfx[q], 0, vr, 2, 0, 1);
                     }
                     if (fxtype == fxtype_beam)
                     {
                                     q++;
                                     q %= maxbeam;
                                     sndpool.play(beamfx[q], 0, vr, 1, 0, 1);
                     }
                     if (fxtype == fxtype_explosion)
                     {
                                     q++;
                                     q %= maxexplosion;
                                     sndpool.play(explosionfx[q], 0, vr, 3, 0, 1);
                     }
     }

     @SuppressWarnings("unused")

     public void
play(final int team, final boolean inbattle)
     {
                     if (sndpool == null)
                     {
                                     return;
                     }
                     if(!modded)
                     {
                                     if (maxbasicchatter > 0)
                                     {
                                                     q = rand.nextInt(maxbasicchatter);
                                                     if (inbattle)
                                                     {
                                                                     // requires maxbasicchatter not be equal to 42....pretty much guaranteed...
                                                                     q %= (maxbasicchatter - 42);
                                                                     q += 42;
                                                     }
                                                     else
                                                     {
                                                                     q %= 42;
                                                     }
                                                     if (q != oq)
                                                     {
                                                                     oq = q;
                                                                     sndpool.play(basicchatter[q], 0, 0.1f, 1, 0, 1);
                                                     }
                                     }
                                     else
                                     {
                                                     if (maxbattle > 0 && maxprep > 0)
                                                     {
                                                                     if (inbattle)
                                                                     {
                                                                                     q = rand.nextInt(maxbattle);
                                                                                     if (q != oq)
                                                                                     {
                                                                                                     sndpool.play(chatterbattle[q + team * maxbattle], 0, 0.9f, 1, 0, 1);
                                                                                                     oq = q;
                                                                                     }
                                                                     }
                                                                     else
                                                                     {
                                                                                     q = rand.nextInt(maxprep);
                                                                                     if (q != oq)
                                                                                     {
                                                                                                     sndpool.play(chatterprep[q + team * maxprep], 0, 0.9f, 1, 0, 1);
                                                                                                     oq = q;
                                                                                     }
                                                                     }
                                                     }
                                     }
                     }
                     else
                     {
                                     //we are modded......
                                     if(inbattle)
                                     {
                                                     q = rand.nextInt(mybattle);
                                                     if(q!=oq)
                                                     {
                                                                     sndpool.play(chatterbattle[q], 0, 0.25f, 1, 0, 1);
                                                                     oq = q;
                                                     }
                                     }
                                     else
                                     {
                                                     q = rand.nextInt(myprep);
                                                     if(q!=oq)
                                                     {
                                                                     sndpool.play(chatterprep[q], 0, 0.25f, 1, 0, 1);
                                                                     oq = q;
                                                     }
                                     }
                     }
                    
     }

     public void
end()
     {
                     if (sndpool != null)
                     {
                                     try
                                     {
                                                     sndpool.release();
                                                     sndpool = null;
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.w(TAG, "could not release sound pool");
                                     }
                     }
     }
     private void initfolders()
     {
                     try
                     {
                                     File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.EARTHHOPECHATTER+com.lloydm.geosword.common.Config.PREPCHATTER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch(Exception ff)
                                     {
                                                     Log.w(TAG,"error mkdir");
                                     }
                                     folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.EARTHHOPECHATTER+com.lloydm.geosword.common.Config.BATTLECHATTER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch(Exception ff)
                                     {
                                                     Log.w(TAG,"error mkdir");
                                     }
                                     folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.AIOSENTICHATTER+com.lloydm.geosword.common.Config.PREPCHATTER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch(Exception ff)
                                     {
                                                     Log.w(TAG,"error mkdir");
                                     }
                                     folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.AIOSENTICHATTER+com.lloydm.geosword.common.Config.BATTLECHATTER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch(Exception ff)
                                     {
                                                     Log.w(TAG,"error mkdir");
                                     }
                                     folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.MARSCHATTER+com.lloydm.geosword.common.Config.PREPCHATTER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch(Exception ff)
                                     {
                                                     Log.w(TAG,"error mkdir");
                                     }
                                     folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.MARSCHATTER+com.lloydm.geosword.common.Config.BATTLECHATTER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch(Exception ff)
                                     {
                                                     Log.w(TAG,"error mkdir");
                                     }
                                     folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.HUSHAMICHATTER+com.lloydm.geosword.common.Config.PREPCHATTER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch(Exception ff)
                                     {
                                                     Log.w(TAG,"error mkdir");
                                     }
                                     folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.HUSHAMICHATTER+com.lloydm.geosword.common.Config.BATTLECHATTER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch(Exception ff)
                                     {
                                                     Log.w(TAG,"error mkdir");
                                     }
                                     folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.OUTLANDERCHATTER+com.lloydm.geosword.common.Config.PREPCHATTER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch(Exception ff)
                                     {
                                                     Log.w(TAG,"error mkdir");
                                     }
                                     folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.OUTLANDERCHATTER+com.lloydm.geosword.common.Config.BATTLECHATTER);
                                     try
                                     {
                                                     folder.mkdirs();
                                     }
                                     catch(Exception ff)
                                     {
                                                     Log.w(TAG,"error mkdir");
                                     }
                     }
                     catch(Exception e)
                     {
                                     Log.w(TAG,"unable to setup folders for pilot chatter");
                     }
     }
     private void checkmod(final int team)
     {
                     //check which team we are and see if there are .ogg files numbered in the right folders..
                     File bfolder = null;
                     File pfolder = null;
                     modded = false;
                     if(team==1)
                     {
                                     //earthhope
                                     pfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.EARTHHOPECHATTER+com.lloydm.geosword.common.Config.PREPCHATTER);

                                     bfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.EARTHHOPECHATTER+com.lloydm.geosword.common.Config.BATTLECHATTER);
                                     validfiles(pfolder,bfolder);
                     }
                     if(team==2)
                     {
                                     //aiosenti
                                     pfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.AIOSENTICHATTER+com.lloydm.geosword.common.Config.PREPCHATTER);
                                     bfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.AIOSENTICHATTER+com.lloydm.geosword.common.Config.BATTLECHATTER);
                                     validfiles(pfolder,bfolder);
                     }
                     if(team==3)
                     {
                                     //mars
                                     pfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.MARSCHATTER+com.lloydm.geosword.common.Config.PREPCHATTER);
                                     bfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.MARSCHATTER+com.lloydm.geosword.common.Config.BATTLECHATTER);
                                     validfiles(pfolder,bfolder);
                     }
                     if(team==4)
                     {
                                     //hushami
                                     pfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.HUSHAMICHATTER+com.lloydm.geosword.common.Config.PREPCHATTER);
                                     bfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.HUSHAMICHATTER+com.lloydm.geosword.common.Config.BATTLECHATTER);
                                     validfiles(pfolder,bfolder);
                     }
                     if(team==5)
                     {
                                     //outlanders
                                     pfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.OUTLANDERCHATTER+com.lloydm.geosword.common.Config.PREPCHATTER);
                                     bfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/"+com.lloydm.geosword.common.Config.OUTLANDERCHATTER+com.lloydm.geosword.common.Config.BATTLECHATTER);
                                     validfiles(pfolder,bfolder);
                     }
     }
     private void validfiles(File pfolder, File bfolder)
     {
                     File[] blist = null;
                     File[] plist = null;
                     boolean foldersokay = true;
                     if(pfolder.canRead() && bfolder.canRead() && pfolder.isDirectory() && bfolder.isDirectory())
                     {
                                     blist = bfolder.listFiles();
                                     plist = pfolder.listFiles();
                                     if(blist!=null)
                                     {
                                                     for(int i=0;i<blist.length;i++)
                                                     {
                                                                     String bfilename = blist[i].getName();
                                                                     if(bfilename!=null)
                                                                     {
                                                                                     if(bfilename.endsWith(".ogg"))

                                                                                     {
                                                                                                     String prefix = bfilename.substring(0, bfilename.length()-4);
                                                                                                     try
                                                                                                     {
                                                                                                                     int myint = Integer.decode(prefix);
                                                                                                                     if(myint>mybattle && myint<200)
                                                                                                                     {
                                                                                                                                     mybattle = myint;
                                                                                                                     }
                                                                                                     }
                                                                                                     catch(Exception e)
                                                                                                     {
                                                                                                                     Log.w(TAG,"invalid file in folder");
                                                                                                     }
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     foldersokay = false;
                                                                                     }
                                                                     }
                                                                     else
                                                                     {
                                                                                     foldersokay = false;
                                                                     }
                                                     }
                                     }
                                     else
                                     {
                                                     foldersokay = false;
                                     }
                                     if(plist!=null)
                                     {
                                                     for(int i=0;i<plist.length;i++)
                                                     {
                                                                     String pfilename = plist[i].getName();
                                                                     if(pfilename!=null)
                                                                     {
                                                                                     if(pfilename.endsWith(".ogg"))

                                                                                     {
                                                                                                     String prefix = pfilename.substring(0, pfilename.length()-4);
                                                                                                     try
                                                                                                     {
                                                                                                                     int myint = Integer.decode(prefix);
                                                                                                                     if(myint>myprep && myint<200)
                                                                                                                     {
                                                                                                                                     myprep = myint;
                                                                                                                     }
                                                                                                     }
                                                                                                     catch(Exception e)
                                                                                                     {
                                                                                                                     Log.w(TAG,"invalid file in folder");
                                                                                                     }
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     foldersokay = false;
                                                                                     }
                                                                     }
                                                                     else
                                                                     {
                                                                                     foldersokay = false;
                                                                     }
                                                     }
                                     }
                                     else
                                     {
                                                     foldersokay = false;
                                     }
                     }
                     else
                     {
                                     foldersokay = false;
                     }
                     if(foldersokay && myprep>0 && mybattle>0)
                     {
                                     modded = true;
                                     prepfolder = pfolder.getAbsolutePath();
                                     battlefolder = bfolder.getAbsolutePath();
                     }

     }
}