package com.lloydm.geosword.activities;

import java.io.File;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
//import android.widget.TextView;

import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lloydm.geosword1.R;
import com.lloydm.geosword.common.Config;
import com.lloydm.geosword.common.StandardGameMenuActivity;
import com.lloydm.geosword.common.UploadCompleteBroadcastReceiver;
import com.lloydm.geosword.common.UploadService;
import com.lloydm.geosword.common.MenuMusicService;

public class InGameFrontMenuActivity extends StandardGameMenuActivity
{

     // private final static String TAG =
     // "com.lloydm.geosword.activities.InGameFrontMenuActivity";

     private int gameid = -1;
     private int playerid = -1;
     private int teamid = -1;

     private String gamekey = "";
     private String gamename = "";

     private boolean istablet = false;

     private volatile boolean goingback = true;
     private volatile boolean waiting = false;

     private Button btnfleet = null;
     private Button btnresearch = null;
     private Button btninvite = null;

     private LinearLayout laybattles = null;

     private TextView txtreplayhelp1 = null;
     private TextView txtreplayhelp2 = null;

     private Button btntoggleaudio = null;

     private ScrollView scrreplay = null;

     // private TextView txtbattles[] = null;

     private String[] battlesondevice = null;

     private UploadCompleteBroadcastReceiver receiver = null;

     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
                     super.onCreate(savedInstanceState);
     }

     @Override
     public void setup()
     {
                     setContentView(R.layout.activity_ingamefrontmenu);
                     mainview = (View) findViewById(R.layout.activity_ingamefrontmenu);

                     bvidresid = R.raw.mnumov;
                     bvideolooped = true;
                     bvideo = (SurfaceView) findViewById(R.id.surfacevidingamefrontmenu);

                     setreceiver();

                     btnfleet = (Button) findViewById(R.id.btn_fleets);
                     btnresearch = (Button) findViewById(R.id.btn_research);

                     btninvite = (Button) findViewById(R.id.btn_invite);
                     laybattles = (LinearLayout) findViewById(R.id.laybattles);
                     if (laybattles != null)
                     {
                                     if (laybattles.getContentDescription().equals("tablet"))
                                     {
                                                     istablet = true;
                                     }
                     }
                     scrreplay = (ScrollView) findViewById(R.id.scrollreplay2);

                     txtreplayhelp1 = (TextView) findViewById(R.id.txtreplayhelp1);
                     txtreplayhelp2 = (TextView) findViewById(R.id.txtreplayhelp2);

                     btntoggleaudio = (Button) findViewById(R.id.btntoggleaudio1);
                     // check whether it should be activated or not....based on shared prefs....
                     String audiostatus = getprefs("com.lloydm.geosword.toggleaudio", "1");
                     if (audiostatus.equals("1"))
                     {
                                     btntoggleaudio.setActivated(true);
                     }
                     else
                     {
                                     btntoggleaudio.setActivated(false);
                     }

                     btnback = (Button) findViewById(R.id.btnback3);
                     btnback.setOnClickListener(new View.OnClickListener()
                     {

                                     @Override
                                     public void onClick(View v)
                                     {
                                                     goingback = true;
                                                     finish();
                                     }
                     })
;

                     btntoggleaudio.setOnClickListener(new View.OnClickListener()
                     {
                                     @Override
                                     public void onClick(View v)
                                     {
                                                     runOnUiThread(new Runnable()
                                                     {

                                                                     @Override
                                                                     public void run()
                                                                     {
                                                                                     if (btntoggleaudio.isActivated())
                                                                                     {
                                                                                                     btntoggleaudio.setActivated(false);
                                                                                                     saveprefs("com.lloydm.geosword.toggleaudio", "0");

                                                                                                     // mute the audio.....send a broadcast....
                                                                                                     Intent serviceintent = new Intent(InGameFrontMenuActivity.this, MenuMusicService.class);
                                                                                                     if (Build.VERSION.SDK_INT >= 12)
                                                                                                     {
                                                                                                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                                                                                     }
                                                                                                     serviceintent.setAction("com.lloydm.geosword.common.MenuMusicService.muteon");
                                                                                                     startService(serviceintent);
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     btntoggleaudio.setActivated(true);
                                                                                                     saveprefs("com.lloydm.geosword.toggleaudio", "1");
                                                                                                     // unmute the audio....send a broadcast.....
                                                                                                     Intent serviceintent = new Intent(InGameFrontMenuActivity.this, MenuMusicService.class);
                                                                                                     if (Build.VERSION.SDK_INT >= 12)
                                                                                                     {
                                                                                                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                                                                                     }
                                                                                                     serviceintent.setAction("com.lloydm.geosword.common.MenuMusicService.muteoff");
                                                                                                     startService(serviceintent);
                                                                                     }

                                                                     }
                                                     });
                                     }
                     });

                     gameid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.gameid", -1);
                     playerid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.playerid", -1);
                     teamid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.teamid", -1);
                     if (gameid == -1)
                     {
                                     gameid = getIntent().getIntExtra("com.lloydm.geosword.activities.InGameFrontMenuActivity.enterthewarzone.gameid", -1);
                     }
                     if (playerid == -1)
                     {
                                     playerid = getIntent().getIntExtra("com.lloydm.geosword.activities.PreGameLobbyActivity.playerid", -1);
                     }
                     if (playerid == -1)
                     {
                                     playerid = getIntent().getIntExtra("com.lloydm.geosword.activities.PreGameLobbyActivity.enterthewarzone.playerid", -1);
                     }
                     if (playerid == -1)
                     {
                                     playerid = getIntent().getIntExtra("com.lloydm.geosword.activities.InGameFrontMenuActivity.enterthewarzone.playerid", -1);
                     }
                     if (teamid == -1)
                     {
                                     teamid = getIntent().getIntExtra("com.lloydm.geosword.activities.InGameFrontMenuActivity.enterthewarzone.teamid", -1);
                     }

                     if (gameid != -1 && playerid != -1 && teamid != -1)
                     {
                                     saveprefs("game_data_basicids_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid), "1");
                     }

                     gamekey = getprefs("com.lloydm.geosword.gamekey_" + gameid, "");
                     gamename = getprefs("com.lloydm.geosword.gamename_" + gameid, "");

                     if (getprefs("gameresearchiddata_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid), "none").equals("none"))
                     {
                                     // indicate need to do research! (same with ai later)
                                     btnresearch.setActivated(true);
                     }

                     String downloadedbattles = getprefs("com.lloydm.geosword.downloadedbattles_" + gameid, "");

                     String downloadedbattlesdesc = getprefs("com.lloydm.geosword.downloadedbattlesdesc_" + gameid, "");

                     if (!downloadedbattles.equals("") && !downloadedbattlesdesc.equals(""))
                     {
                                     String[] battlelinks = downloadedbattles.split(":");
                                     String[] battledescs = downloadedbattlesdesc.split(":");
                                     if (battlelinks != null && battledescs != null)
                                     {
                                                     if (battlelinks.length == battledescs.length)
                                                     {
                                                                     battlesondevice = new String[battlelinks.length];
                                                                     for (int i = 0; i < battlelinks.length; i++)
                                                                     {
                                                                                     boolean tmpbool = false;
                                                                                     for (int mm = 0; mm < laybattles.getChildCount(); mm++)
                                                                                     {
                                                                                                     try
                                                                                                     {
                                                                                                                     TextView txt1 = (TextView) laybattles.getChildAt(mm);
                                                                                                                     if (txt1 != null)
                                                                                                                     {
                                                                                                                                     if (txt1.getContentDescription().equals(battlelinks[i]))
                                                                                                                                     {
                                                                                                                                                     // we want to skip this...we already have it...doh!
                                                                                                                                                     tmpbool = true;
                                                                                                                                                     break;

                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                                     catch (Exception ffge)
                                                                                                     {
                                                                                                                     Log.i(TAG, "unusual...laybattles cant find textveiw");
                                                                                                     }
                                                                                     }

                                                                                     // check the file actually exists....
                                                                                     try
                                                                                     {
                                                                                                     String outputpath = Config.getStorageFolder(InGameFrontMenuActivity.this);
                                                                                                     if (!outputpath.endsWith("/"))
                                                                                                     {
                                                                                                                     outputpath = outputpath + "/";
                                                                                                     }
                                                                                                     outputpath = outputpath + Config.DOWNLOADEDREPLAYFOLDER + battlelinks[i];
                                                                                                     File tf = new File(outputpath);
                                                                                                     if (!tf.exists())
                                                                                                     {
                                                                                                                     continue;
                                                                                                     }
                                                                                                     if (!tf.isFile())
                                                                                                     {
                                                                                                                     continue;
                                                                                                     }
                                                                                                     if (!tf.canRead())
                                                                                                     {
                                                                                                                     continue;
                                                                                                     }
                                                                                     }
                                                                                     catch (Exception ffg)
                                                                                     {
                                                                                                     Log.w(TAG, "problem checking replay");
                                                                                     }

                                                                                     if (tmpbool)
                                                                                     {
                                                                                                     continue;// skip this one...
                                                                                     }
                                                                                     TextView txt = new TextView(this);
                                                                                     txt.setText(battledescs[i]);
                                                                                     txt.setTextColor(Color.WHITE);
                                                                                     txt.setBackgroundColor(Color.rgb(48, 48, 80));
                                                                                     txt.setTypeface(font);
                                                                                     txt.setSingleLine();
                                                                                     txt.setEllipsize(TruncateAt.MARQUEE);
                                                                                     txt.setFocusable(true);
                                                                                     txt.setFocusableInTouchMode(true);
                                                                                     txt.setSelected(true);
                                                                                     txt.setTextSize(18);
                                                                                     txt.setHorizontallyScrolling(true);
                                                                                     txt.setMarqueeRepeatLimit(-1);

                                                                                     txt.setContentDescription(battlelinks[i]);
                                                                                     battlesondevice[i] = battlelinks[i];
                                                                                     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
                                                                                     if (istablet)
                                                                                     {
                                                                                                     params.topMargin = 30;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     params.topMargin = 60;
                                                                                     }
                                                                                     params.leftMargin = 2;
                                                                                     txt.setLayoutParams(params);
                                                                                     final String battledescription = battledescs[i];
                                                                                     txt.setOnClickListener(new View.OnClickListener()
                                                                                     {
                                                                                                     @Override
                                                                                                     public void onClick(View v)
                                                                                                     {
                                                                                                                     // watch the battle!
                                                                                                                     goingback = false;
                                                                                                                     // String outputpath = Environment.getExternalStorageDirectory().getAbsolutePath();
                                                                                                                     String outputpath = Config.getStorageFolder(InGameFrontMenuActivity.this);
                                                                                                                     if (!outputpath.endsWith("/"))

                                                                                                                     {
                                                                                                                                     outputpath = outputpath + "/";
                                                                                                                     }
                                                                                                                     outputpath = outputpath + Config.DOWNLOADEDREPLAYFOLDER;
                                                                                                                     String attname = "";
                                                                                                                     String defname = "";

                                                                                                                     try
                                                                                                                     {
                                                                                                                                     attname = battledescription.substring(0, battledescription.indexOf(" fought"));
                                                                                                                                     defname = battledescription.substring(battledescription.indexOf(" fought") + 8, battledescription.indexOf(" on "));
                                                                                                                     }
                                                                                                                     catch (Exception fgg)
                                                                                                                     {
                                                                                                                                     Log.e(TAG, "error parsing battle description");
                                                                                                                     }

                                                                                                                     startActivity(new Intent(InGameFrontMenuActivity.this, TestActivity.class).putExtra("com.lloydm.geosword.activities.TestActivity.replayfile", outputpath + v.getContentDescription()).putExtra("com.lloydm.geosword.common.UploadService.gameid", gameid).putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid).putExtra("com.lloydm.geosword.common.UploadService.attname", attname).putExtra("com.lloydm.geosword.common.UploadService.defname", defname));
                                                                                                                     finish();
                                                                                                     }
                                                                                     })
;
                                                                                     txt.setOnLongClickListener(new View.OnLongClickListener()
                                                                                     {
                                                                                                     @Override
                                                                                                     public boolean onLongClick(View v)
                                                                                                     {
                                                                                                                     // send the battle to a friend.....
                                                                                                                     // it will send the link to the battle....not the actual file....
                                                                                                                     // start a chooser etc.....
                                                                                                                     Intent sendIntent = new Intent(Intent.ACTION_SEND);
                                                                                                                     sendIntent.putExtra(Intent.EXTRA_SUBJECT, R.string.sharesubject);
                                                                                                                     final String emailmsg = "Link to Star Dancer battle replay: "
+
Config.WEBREPLAYDOWNLOADPATH + v.getContentDescription().toString().replace(".EP4", ".php") + "\n\n\n" + " Download the full game from google play at " + Uri.parse("https://play.google.com/store/apps/details?id=com.lloydm.geosword1") + "\n\n";
                                                                                                                     sendIntent.putExtra(Intent.EXTRA_TEXT, emailmsg);
                                                                                                                     sendIntent.setType("text/plain");

                                                                                                                     String title = getResources().getString(R.string.sharereplay);
                                                                                                                     Intent chooser = Intent.createChooser(sendIntent, title);
                                                                                                                     if (sendIntent.resolveActivity(getPackageManager()) != null)
                                                                                                                     {
                                                                                                                                     goingback = false;
                                                                                                                                     startActivity(chooser);
                                                                                                                     }
                                                                                                                     return true;
                                                                                                     }
                                                                                     });
                                                                                     scrreplay.setVisibility(View.VISIBLE);
                                                                                     txtreplayhelp1.setVisibility(View.VISIBLE);
                                                                                     txtreplayhelp2.setVisibility(View.VISIBLE);
                                                                                     txtreplayhelp1.invalidate();
                                                                                     txtreplayhelp2.invalidate();
                                                                                     if (txt != null)
                                                                                     {
                                                                                                     laybattles.addView(txt);
                                                                                                     laybattles.invalidate();
                                                                                     }
                                                                                     scrreplay.invalidate();
                                                                     }
                                                     }
                                     }
                     }
                     if (!gamekey.equals("") && !gamename.equals(""))
                     {
                                     // enable inviting others to the game.......
                                     btninvite.setVisibility(View.VISIBLE);
                                     btninvite.setTypeface(font);
                                     btninvite.setOnClickListener(new View.OnClickListener()
                                     {
                                                     @Override
                                                     public void onClick(View v)
                                                     {
                                                                     onChangeActivity(4);
                                                     }
                                     })
;
                     }

                     // requestlist();
                     // technically we don't bring anything back in here (at the
                     // moment/anymore)

                     // newtoast("GameID:"+gameid+" PlayerId:"+playerid);
                     btnfleet.setOnClickListener(new View.OnClickListener()
                     {
                                     @Override
                                     public void onClick(View v)
                                     {
                                                     onChangeActivity(1);
                                     }
                     })
;
                     btnresearch.setOnClickListener(new View.OnClickListener()
                     {
                                     @Override
                                     public void onClick(View v)
                                     {
                                                     onChangeActivity(2);
                                     }
                     })
;
                     /*
                         * btnnews.setOnClickListener(new View.OnClickListener() {
                         *
                         * @Override public void onClick(View v) { onChangeActivity(3); } })
;
                         *
/
     }

     @SuppressLint("InlinedApi")
     private void requestlist()
     {

                     if (waiting)
                     {
                                     newtoast("Please wait....trying to talk to database");
                                     return;
                     }
                     waiting = true;

                     Intent serviceintent = new Intent(InGameFrontMenuActivity.this, UploadService.class);
                     if (Build.VERSION.SDK_INT >= 12)
                     {
                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                     }
                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.ingamefrontmenu.load");
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.gameid", gameid);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);

                     startService(serviceintent);

     }

     @Override
     protected void onResume()
     {
                     super.onResume();
                     setreceiver();
                     // there is a chance that we have just returned from the login screen -
                     // in which case....
                     // if we are logged in then we most likely want to attempt to redownload
                     // stuff....
                     /*
                         * try { if(txtbattles!=null) { for(int i=0;i<txtbattles.length;i++) { try { txtbattles[i] = null; } catch(Exception e) { Log.i(TAG,"Error removing text view"); } } txtbattles = null; } }
                         * catch(Exception e) { Log.i(TAG, "Error nulling battle text views"); } //clear the laybattles if possible...... if(laybattles!=null) { try { laybattles.removeAllViews();
                         *
                         * } catch(Exception e) { Log.i(TAG,"Error removing views from laybattles"); } } System.gc();
                         *
/
                     // if (isloggedin() && loginstatuschanged())
                     // {
                     requestlist();
                     // }
                     setuphelp(R.string.htt_ingf, R.string.htxt_ingf, R.string.htt_ingf);
     }

     @Override
     protected void onPause()
     {
                     super.onPause();
                     if (receiver != null)
                     {
                                     unregisterReceiver(receiver);
                     }
                     receiver = null;

                     if (isFinishing())
                     {
                                     if (goingback)
                                     {
                                                     // we want to start the first main menu activity.....
                                                     onChangeActivity(0);
                                     }
                     }
     }

     @Override
     public void onChangeActivity(int id)
     {
                     super.onChangeActivity(id);
                     if (id == 1)
                     {
                                     goingback = false;
                                     startActivity(new Intent(this, InspectFleetActivity.class).putExtra("com.lloydm.geosword.common.UploadService.gameid", gameid).putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid));
                                     finish();
                     }
                     if (id == 2)
                     {
                                     goingback = false;
                                     startActivity(new Intent(this, ResearchActivity.class).putExtra("com.lloydm.geosword.common.UploadService.gameid", gameid).putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid));
                                     finish();
                     }
                     if (id == 4)
                     {
                                     goingback = false;
                                     startActivity(new Intent(this, InviteActivity.class).putExtra("com.lloydm.geosword.common.UploadService.gameid", gameid).putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid).putExtra("com.lloydm.geosword.common.UploadService.gamekey", gamekey).putExtra("com.lloydm.geosword.common.UploadService.gamename", gamename));
                                     finish();
                     }
                     if (id == 0)
                     {
                                     startActivity(new Intent(this, MainMenuActivity.class));
                     }
     }

     private void setreceiver()
     {
                     if (receiver == null)
                     {
                                     receiver = new UploadCompleteBroadcastReceiver()
                                     {
                                                     @Override
                                                     public void onReceive(Context context, Intent intent)
                                                     {

                                                                     // the intent will contain the response from the
                                                                     // server.....either true or false....very simple - that's
                                                                     // all we care
                                                                     // about when we send information to the server...and
                                                                     // possibly an error code...
                                                                     boolean success = intent.getBooleanExtra("com.lloydm.geosword.activities.InGameFrontMenuActivity.resultcode", false);

                                                                     long tuid = intent.getLongExtra("com.lloydm.geosword.common.UploadService.uniqueid", -1);
                                                                     if (tuid != uid)
                                                                     {
                                                                                     // ignore this one and try again.....
                                                                                     Log.i(TAG, "uid does not match");
                                                                                     // success = false;
                                                                                     // in this particular case we don't care about the uid's not matching....not really anyway...
                                                                                     // because this is usually involves a download that has taken place in the background....

                                                                     }
                                                                     if (success)
                                                                     {
                                                                                     // this now returns a list of battles (and begins downloading them one after the other)
                                                                                     //
                                                                                     //
                                                                                     //
                                                                                     //
                                                                                     final String battlelist = intent.getStringExtra("com.lloydm.geosword.activities.InGameFrontMenuActivity.battlelist");
                                                                                     // final String downloadresult = intent.getStringExtra("com.lloydm.geosword.activities.InGameFrontMenuActivity.downloadresult");

                                                                                     // do download result first....(it may affect what we do with battlelist)
                                                                                     /*
                                                                                         * if(downloadresult!=null) { final String[] battlelistarray = downloadresult.split(":"); if(battlelistarray!=null && !downloadresult.startsWith(":")) { String downloads =
                                                                                         * getprefs("com.lloydm.geosword.downloadedbattles_"+gameid,""); if(downloads.endsWith(":")) {
                                                                                         * saveprefs("com.lloydm.geosword.downloadedbattles_"+gameid,downloads+downloadresult); } else {
                                                                                         * saveprefs("com.lloydm.geosword.downloadedbattles_"+gameid,downloads+":"+downloadresult); } } }
                                                                                         *
/
                                                                                     if (battlelist != null)
                                                                                     {
                                                                                                     // parse the list......
                                                                                                     final String[] battlelistarray = battlelist.split("\\|");
                                                                                                     Log.i(TAG, "battlelist1:" + battlelist);
                                                                                                     if (battlelistarray != null)
                                                                                                     {
                                                                                                                     Log.i(TAG, "11");
                                                                                                                     String gettheseones = "";
                                                                                                                     String gettheseonesdesc = "";
                                                                                                                     for (int i = 0; i < battlelistarray.length; i++)
                                                                                                                     {
                                                                                                                                     Log.i(TAG, "22:" + i);
                                                                                                                                     String[] fields = battlelistarray[i].split(":");
                                                                                                                                     if (fields != null)
                                                                                                                                     {
                                                                                                                                                     Log.i(TAG, "33:" + i);
                                                                                                                                                     // url, description ? is that it? I think so....at the moment at least...
                                                                                                                                                     // not really a url just a file name....
                                                                                                                                                     if (fields.length >= 2)
                                                                                                                                                     {
                                                                                                                                                                     Log.i(TAG, "44:" + i);
                                                                                                                                                                     final String battleurl = fields[0]; // actually this is just the file name...our service should know where to locate it....
                                                                                                                                                                     final String battledescription = fields[1];
                                                                                                                                                                     Log.i(TAG, "battleurl:" + battleurl);
                                                                                                                                                                     // check if the battle has been downloaded already....if so populate
                                                                                                                                                                     // the laybattle with textviews.....and also write it to shared prefs
                                                                                                                                                                     // if haven't already....
                                                                                                                                                                     String downloadedbattles = getprefs("com.lloydm.geosword.downloadedbattles_" + gameid, "");
                                                                                                                                                                     Log.i(TAG, "downloaded:" + downloadedbattles);
                                                                                                                                                                     // it is an array.....
                                                                                                                                                                     String[] dbattles = downloadedbattles.split(":");
                                                                                                                                                                     boolean exitearly = false;
                                                                                                                                                                     if (dbattles != null)
                                                                                                                                                                     {
                                                                                                                                                                                     for (int j = 0; j < dbattles.length; j++)
                                                                                                                                                                                     {
                                                                                                                                                                                                     if (dbattles[j].equals(battleurl))
                                                                                                                                                                                                     {

                                                                                                                                                                                                                     // don't download - already have....
                                                                                                                                                                                                                     exitearly = true;
                                                                                                                                                                                                                     // however we do want to add the description to the textviews etc...
                                                                                                                                                                                                                     // and set the onclick listener etc.....
                                                                                                                                                                                                                     // check that the file is not already on the screen...
                                                                                                                                                                                                                     boolean skip = false;
                                                                                                                                                                                                                     if (battlesondevice != null)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     for (int k = 0; k < battlesondevice.length; k++)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     if (battleurl.equals(battlesondevice[k]))
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     skip = true;
                                                                                                                                                                                                                                                                     break;
                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     if (skip)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     continue;
                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     runOnUiThread(new Runnable()
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     @Override
                                                                                                                                                                                                                                     public void run()
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     if (laybattles != null)
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     boolean tmpbool = false;
                                                                                                                                                                                                                                                                     for (int mm = 0; mm < laybattles.getChildCount(); mm++)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     try
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     TextView txt1 = (TextView) laybattles.getChildAt(mm);
                                                                                                                                                                                                                                                                                                     if (txt1 != null)
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     if (txt1.getContentDescription().equals(battleurl))
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     // we want to skip this...we already have it...doh!
                                                                                                                                                                                                                                                                                                                                     tmpbool = true;
                                                                                                                                                                                                                                                                                                                                     break;

                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     catch (Exception ffge)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     Log.i(TAG, "unusual...laybattles cant find textveiw");

                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                     if (tmpbool)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     return; // dont proceed further...
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     TextView txt = new TextView(InGameFrontMenuActivity.this);
                                                                                                                                                                                                                                                                     txt.setText(battledescription);
                                                                                                                                                                                                                                                                     txt.setTextColor(Color.WHITE);
                                                                                                                                                                                                                                                                     txt.setTypeface(font);
                                                                                                                                                                                                                                                                     txt.setSingleLine();
                                                                                                                                                                                                                                                                     txt.setEllipsize(TruncateAt.MARQUEE);
                                                                                                                                                                                                                                                                     txt.setSingleLine();
                                                                                                                                                                                                                                                                     txt.setBackgroundColor(Color.rgb(48, 48, 80));
                                                                                                                                                                                                                                                                     txt.setFocusable(true);
                                                                                                                                                                                                                                                                     txt.setFocusableInTouchMode(true);
                                                                                                                                                                                                                                                                     txt.setSelected(true);
                                                                                                                                                                                                                                                                     txt.setTextSize(18);
                                                                                                                                                                                                                                                                     txt.setHorizontallyScrolling(true);
                                                                                                                                                                                                                                                                     txt.setMarqueeRepeatLimit(-1);

                                                                                                                                                                                                                                                                     txt.setContentDescription(battleurl);
                                                                                                                                                                                                                                                                     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
                                                                                                                                                                                                                                                                     if (istablet)
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     params.topMargin = 30;
                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     else
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     params.topMargin = 60;
                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                     params.leftMargin = 2;

                                                                                                                                                                                                                                                                     txt.setLayoutParams(params);
                                                                                                                                                                                                                                                                     txt.setOnClickListener(new View.OnClickListener()
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     @Override
                                                                                                                                                                                                                                                                                     public void onClick(View v)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     // watch the battle!
                                                                                                                                                                                                                                                                                                     goingback = false;
                                                                                                                                                                                                                                                                                                     // String outputpath = Environment.getExternalStorageDirectory().getAbsolutePath();
                                                                                                                                                                                                                                                                                                     String outputpath = Config.getStorageFolder(InGameFrontMenuActivity.this);
                                                                                                                                                                                                                                                                                                     if (!outputpath.endsWith("/"))

                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     outputpath = outputpath + "/";
                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                     outputpath = outputpath + Config.DOWNLOADEDREPLAYFOLDER;

                                                                                                                                                                                                                                                                                                     String attname = "";
                                                                                                                                                                                                                                                                                                     String defname = "";

                                                                                                                                                                                                                                                                                                     try
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     attname = battledescription.substring(0, battledescription.indexOf(" fought"));
                                                                                                                                                                                                                                                                                                                     defname = battledescription.substring(battledescription.indexOf(" fought") + 8, battledescription.indexOf(" on "));
                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                     catch (Exception fgg)
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     Log.e(TAG, "error parsing battle description");
                                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                                                     startActivity(new Intent(InGameFrontMenuActivity.this, TestActivity.class).putExtra("com.lloydm.geosword.activities.TestActivity.replayfile", outputpath + v.getContentDescription()).putExtra("com.lloydm.geosword.common.UploadService.gameid", gameid).putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid).putExtra("com.lloydm.geosword.common.UploadService.attname", attname).putExtra("com.lloydm.geosword.common.UploadService.defname", defname));
                                                                                                                                                                                                                                                                                                     finish();
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     })
;
                                                                                                                                                                                                                                                                     txt.setOnLongClickListener(new View.OnLongClickListener()
                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                     @Override
                                                                                                                                                                                                                                                                                     public boolean onLongClick(View v)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     // send the battle to a friend.....
                                                                                                                                                                                                                                                                                                     // it will send the link to the battle....not the actual file....
                                                                                                                                                                                                                                                                                                     // start a chooser etc.....
                                                                                                                                                                                                                                                                                                     Intent sendIntent = new Intent(Intent.ACTION_SEND);
                                                                                                                                                                                                                                                                                                     sendIntent.putExtra(Intent.EXTRA_SUBJECT, R.string.sharesubject);
                                                                                                                                                                                                                                                                                                     final String emailmsg = "Link to Star Dancer battle replay: "
+
Config.WEBREPLAYDOWNLOADPATH + v.getContentDescription() + "\n\n\n" + " Download the full game from google play at " + Uri.parse("https://play.google.com/store/apps/details?id=com.lloydm.geosword1") + "\n\n";
                                                                                                                                                                                                                                                                                                     sendIntent.putExtra(Intent.EXTRA_TEXT, emailmsg);
                                                                                                                                                                                                                                                                                                     sendIntent.setType("text/plain");

                                                                                                                                                                                                                                                                                                     String title = getResources().getString(R.string.sharereplay);
                                                                                                                                                                                                                                                                                                     Intent chooser = Intent.createChooser(sendIntent, title);
                                                                                                                                                                                                                                                                                                     if (sendIntent.resolveActivity(getPackageManager()) != null)
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     goingback = false;
                                                                                                                                                                                                                                                                                                                     startActivity(chooser);
                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                     return true;
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                     });
                                                                                                                                                                                                                                                                     scrreplay.setVisibility(View.VISIBLE);
                                                                                                                                                                                                                                                                     txtreplayhelp1.setVisibility(View.VISIBLE);
                                                                                                                                                                                                                                                                     txtreplayhelp2.setVisibility(View.VISIBLE);
                                                                                                                                                                                                                                                                     txtreplayhelp1.invalidate();
                                                                                                                                                                                                                                                                     txtreplayhelp2.invalidate();
                                                                                                                                                                                                                                                                     laybattles.addView(txt);
                                                                                                                                                                                                                                                                     laybattles.invalidate();
                                                                                                                                                                                                                                                                     scrreplay.invalidate();

                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     });

                                                                                                                                                                                                                     break;
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                                     if (exitearly)
                                                                                                                                                                     {
                                                                                                                                                                                     continue;
                                                                                                                                                                     }
                                                                                                                                                                     // we haven't actually tried to download this battle before....
                                                                                                                                                                     // we will do so now....

                                                                                                                                                                     // put the file into a list and pass it to the service again for download...
                                                                                                                                                                     gettheseones = gettheseones + ":" + battleurl;
                                                                                                                                                                     gettheseonesdesc = gettheseonesdesc + ":" + battledescription;
                                                                                                                                                                     Log.i(TAG, "gettheseones:" + gettheseones);
                                                                                                                                                                     Log.i(TAG, "getheseonesdesc:" + gettheseonesdesc);

                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                     }
                                                                                                                     if (!gettheseones.equals("") && !gettheseonesdesc.equals(""))
                                                                                                                     {
                                                                                                                                     // download!
                                                                                                                                     Intent serviceintent = new Intent(InGameFrontMenuActivity.this, UploadService.class);
                                                                                                                                     if (Build.VERSION.SDK_INT >= 12)
                                                                                                                                     {
                                                                                                                                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                                                                                                                     }
                                                                                                                                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.ingamefrontmenu.getbattles");
                                                                                                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.gameid", gameid);
                                                                                                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid);
                                                                                                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.battlefiles", gettheseones);
                                                                                                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.battlefilesdesc", gettheseonesdesc);
                                                                                                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);
                                                                                                                                     startService(serviceintent);
                                                                                                                                     Log.i(TAG, "starting download service");
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     int reason = intent.getIntExtra("com.lloydm.geosword.activities.InGameFrontMenuActivity.authenticationerror", -1);
                                                                                     if (reason == UploadService.LOGIN_NOT_FOUND)
                                                                                     {
                                                                                                     // show the authdialog.....
                                                                                                     login();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     newtoast("Something went wrong during the upload process.");

                                                                                     }
                                                                     }
                                                                     // reset waiting flag.....
                                                     }
                                     };
                                     registerReceiver(receiver, new IntentFilter("com.lloydm.geosword.activities.InGameFrontMenuActivity.load"));
                     }
     }
}