package com.lloydm.geosword.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.content.Context;
import android.os.Environment;

public class Config
{

     // not really used anymore...
     public static final boolean DEVELOPMENTMODE = true;
     public static final boolean DEVSCREENSHOTMODE = false;
     public static final boolean DEVMOVIECAPTUREMODE = false;

     public static final boolean INDEVMODE = true;

     public static boolean ISLOWSPECDEVICE = false;

     // user folders - most likely in external storage...
     public static final String GAMEFOLDER = "stardancer/"; // not used technically
     public static final String REPLAYFOLDER = "stardancer/replays/";
     public static final String DOWNLOADEDREPLAYFOLDER = "stardancer/downloadedreplays/";
     public static final String MODFOLDER = "stardancer/mods/"; // not used
     public static final String DEBUGFOLDER = "stardancer/debug/"; // not used
     public static final String INVITEFOLDER = "stardancer/invites/"; // not used

     // user content
     public static final String MENUMUSICFOLDER = "stardancer/menumusic";
     public static final String BATTLEMUSICFOLDER = "stardancer/battlemusic";
     //pilotchatter folders
     public static final String EARTHHOPECHATTER = "stardancer/earthhope/";
     public static final String AIOSENTICHATTER = "stardancer/aiosenti/";
     public static final String MARSCHATTER = "stardancer/mars/";
     public static final String HUSHAMICHATTER = "stardancer/hushami/";
     public static final String OUTLANDERCHATTER = "stardancer/outlander/";
     public static final String PREPCHATTER = "preparatory";
     public static final String BATTLECHATTER = "battle";

     // new
     public static final String ACCOUNTFOLDER = "stardancer/user";
     public static String accuser = "";
     public static String accpass = "";
     public static String acccall = "";

     public static final String SCREENSHOTFOLDER = "stardancer/screengrabs/"; // now in user picture folder....
     public static final String MOVIECAPTUREFOLDER = "stardancer/movieframes/"; // not used

     // files.....
     public static final String REPLAYPREFIX = "spacebattlereplay_";
     public static final String INVITEPREFIX = "spacebattleinvite_";

     // web folders/paths
     public static final String SUPPORTEMAIL = "contactus@mattiesgames.com";

     // EDIT - modified for v2 - hopefully more efficient webserver code (slightly)

     public static final String WEBPATH = "http://mattiesgames.com/convergence/game_php_pg_server2/pg_upload.php";

     // uploading files to webserver......(replays)
     public static final String WEBREPLAYTRANSFERPATH = "http://mattiesgames.com/convergence/game_php_pg_server2/pg_ep4_transfer.php";
     // location where replays may be accessed....
     public static final String WEBREPLAYDOWNLOADPATH = "http://mattiesgames.com/convergence/battle_replays/";

     public static String getStorageFolder(Context context)
     {
                     String folder = "/";

                     if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
                     {
                                     folder = Environment.getExternalStorageDirectory().getAbsolutePath();
                                     if (!folder.endsWith("/"))
                                     {
                                                     folder = folder + "/";
                                     }
                     }
                     else
                     {
                                     File fd = context.getDir("localstorage", 0);
                                     folder = fd.getAbsolutePath();
                                     if (!folder.endsWith("/"))
                                     {
                                                     folder = folder + "/";
                                     }
                     }
                     return folder;
     }

     public static long getUniqueID(long curID)
     {
                     long id
= System.currentTimeMillis() + curID;
                     return id;
     }

     public static long getUniqueID()
     {
                     long id = System.currentTimeMillis();
                     return id;
     }

     // new...
     public static void readuserdata()
     {
                     try
                     {
                                     File fd = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + Config.ACCOUNTFOLDER);
                                     fd.mkdirs();
                                     if (fd.exists() && fd.isDirectory())
                                     {
                                                     FileInputStream in = null;
                                                     File fi = new File(fd.getAbsolutePath() + "/" + "user.txt");
                                                     try
                                                     {
                                                                     in = new FileInputStream(fi);
                                                                     byte[] uu = new byte[100];
                                                                     byte[] pp = new byte[100];
                                                                     byte[] cc = new byte[100];
                                                                     in.read(uu);
                                                                     in.read(pp);
                                                                     in.read(cc);
                                                                     String uus = new String(uu, "UTF-8");
                                                                     String pps = new String(pp, "UTF-8");
                                                                     String ccs = new String(cc, "UTF-8");
                                                                     uus = uus.trim();
                                                                     pps = pps.trim();
                                                                     ccs = ccs.trim();
                                                                     Config.accuser = uus;
                                                                     Config.accpass = pps;
                                                                     Config.acccall = ccs;
                                                     }
                                                     catch (Exception f)
                                                     {

                                                     }
                                                     finally
                                                     {
                                                                     if (in != null)
                                                                     {
                                                                                     try
                                                                                     {
                                                                                                     in.close();
                                                                                     }
                                                                                     catch (Exception ff)
                                                                                     {

                                                                                     }
                                                                     }
                                                     }
                                     }
                     }
                     catch (Exception e)
                     {

                     }
     }

     public static void saveuserdata(final String username, final String userpass, final String callsign)
     {
                     String outputpath = Config.ACCOUNTFOLDER;
                     try
                     {
                                     File fd = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + outputpath);
                                     fd.mkdirs();
                                     if (fd.exists() && fd.isDirectory())
                                     {
                                                     File fo = new File(fd.getAbsolutePath() + "/" + "user.txt");
                                                     FileOutputStream out = null;
                                                     byte[] buffer = new byte[300];
                                                     byte[] uu = username.getBytes();
                                                     byte[] pp = userpass.getBytes();
                                                     byte[] cc = callsign.getBytes();
                                                     for (int j = 0; j < uu.length; j++)
                                                     {
                                                                     buffer[j] = uu[j];
                                                     }
                                                     for (int j = 0; j < pp.length; j++)
                                                     {
                                                                     buffer[100 + j] = pp[j];
                                                     }
                                                     for (int j = 0; j < cc.length; j++)
                                                     {
                                                                     buffer[j + 200] = cc[j];
                                                     }
                                                     try
                                                     {
                                                                     out = new FileOutputStream(fo);
                                                                     out.write(buffer);
                                                     }
                                                     catch (Exception ef)
                                                     {

                                                     }
                                                     finally
                                                     {
                                                                     try
                                                                     {
                                                                                     if (out != null)
                                                                                     {
                                                                                                     out.close();
                                                                                     }
                                                                     }
                                                                     catch (Exception ff)
                                                                     {

                                                                     }
                                                     }
                                     }

                     }
                     catch (Exception e)
                     {

                     }
     }
}