package com.lloydm.geosword.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

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

public class LaunchAttackActivity extends StandardGameMenuActivity
{
     private final static String TAG = "com.lloydm.geosword.activities.LaunchAttackActivity";

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

     // added tonight
     private volatile int attplanetindex = -1; // attackplanetindex - for choosing the right planet in the 3d world....

     private String battlefile = "";
     private String targetsresult = "";
     private String starmapresult = "";

     // choose targets with the map now......
     private Button[] planetbtns = null;
     private ImageView[] planetrings = null;
     private FrameLayout flay = null;
     private ProgressBar progwheel = null;
     private TextView txthelp = null;
     private TextView txtsimprog = null;

     private int[] xps = null;
     private int[] yps = null;
     private int[] tmps = null;
     private int[] nodes = null;
     private int[] validids = null;
     private String[] callsigns = null;
     private boolean[] callsignsshown = null;

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

     private String replayfile = "";

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

     private UploadCompleteBroadcastReceiver loadreceiver = null;
     private UploadCompleteBroadcastReceiver updatereceiver = null;

     private UploadCompleteBroadcastReceiver simprogressreceiver = null;

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

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

                     setreceiver();

                     gameid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.gameid", -1);
                     playerid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.playerid", -1);
                     fleetid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.fleetid", -1);
                     teamid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.teamid", -1);

                     Log.i(TAG, "Team ID:" + teamid);

                     progwheel = (ProgressBar) findViewById(R.id.progressbar3);
                     progwheel.setVisibility(View.VISIBLE);

                     txtsimprog = (TextView) findViewById(R.id.txtsimprog);
                     txtsimprog.setTypeface(font);
                     txtsimprog.setText("0.0%");

                     flay = (FrameLayout) findViewById(R.id.frmlay_launchattack2);
                     planetbtns = new Button[10];

                     planetbtns[0] = (Button) findViewById(R.id.btnplanet1);
                     planetbtns[1] = (Button) findViewById(R.id.btnplanet2);
                     planetbtns[2] = (Button) findViewById(R.id.btnplanet3);
                     planetbtns[3] = (Button) findViewById(R.id.btnplanet4);
                     planetbtns[4] = (Button) findViewById(R.id.btnplanet5);
                     planetbtns[5] = (Button) findViewById(R.id.btnplanet6);
                     planetbtns[6] = (Button) findViewById(R.id.btnplanet7);
                     planetbtns[7] = (Button) findViewById(R.id.btnplanet8);
                     planetbtns[8] = (Button) findViewById(R.id.btnplanet9);
                     planetbtns[9] = (Button) findViewById(R.id.btnplanet10);

                     txthelp = (TextView) findViewById(R.id.txtplanethelp1);

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

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

                     planetrings = new ImageView[10];

                     final DisplayMetrics metrics = new DisplayMetrics();
                     getWindowManager().getDefaultDisplay().getMetrics(metrics);
                     final int actualWidth = (int) (metrics.widthPixels);
                     final int actualHeight = (int) (metrics.heightPixels);
                     xps = new int[10];
                     yps = new int[10];
                     tmps = new int[10];
                     callsigns = new String[10];
                     callsignsshown = new boolean[5];
                     for (int i = 0; i < 5; i++)
                     {
                                     callsignsshown[i] = false;
                     }
                     nodes = new int[100];
                     validids = new int[10];
                     for (int i = 0; i < 100; i++)
                     {
                                     nodes[i] = 0;
                     }

                     for (int i = 0; i < 10; i++)
                     {
                                     // assumes a screen resolution of 560 x 315 which we will scale back
                                     // up.....
                                     int xi = 40 + ((i + 1) % 3) * 192;
                                     int yi = 40 + ((i - (i % 3)) / 3) * 80;

                                     if (yi == 120)
                                     {
                                                     xi += 96;
                                     }
                                     if (i == 6)
                                     {
                                                     xi -= 36;
                                     }
                                     if (i == 0)
                                     {
                                                     yi += 12;
                                     }
                                     if (xi == 40 && yi == 200)
                                     {
                                                     xi += 24;
                                                     yi += 24;
                                     }
                                     if (xi == 424 && yi == 40)
                                     {
                                                     xi -= 24;
                                                     yi += 16;
                                     }
                                     if (i == 7)
                                     {
                                                     xi += 12;
                                                     yi += 12;
                                     }
                                     if (i == 9)
                                     {
                                                     xi += 80;
                                                     yi = 220;
                                     }
                                     if (xi >= 512)
                                     {
                                                     xi = 490;
                                     }

                                     if (xi > 100 && xi < 490)
                                     {
                                                     if (xi < 256)
                                                     {
                                                                     xi += 3;
                                                     }
                                                     else
                                                     {
                                                                     xi -= 2;
                                                     }
                                     }
                                     if (yi > 50 && yi < 230)
                                     {
                                                     if (yi > 128)
                                                     {
                                                                     yi -= 2;
                                                     }
                                                     else
                                                     {
                                                                     yi += 3;
                                                     }
                                     }

                                     xps[i] = xi;
                                     yps[i] = yi;
                                     tmps[i] = 1 + (i % 5); // need to pass number of players through
                                                                                                                                 // eventually...

                                     FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                                     params.gravity = Gravity.START | Gravity.TOP;

                                     params.leftMargin = (int) ((float) xi * (float) actualWidth / 560f);

                                     params.topMargin = (int) ((float) yi * (float) actualHeight / 315f);
                                     params.width = (int) ((float) actualWidth * (48f / 560f));
                                     params.height = (int) ((float) actualHeight * (48f / 315f));

                                     planetbtns[i].setLayoutParams(params);
                                     planetbtns[i].getLayoutParams().width = (int) ((float) actualWidth * (48f / 560f));
                                     planetbtns[i].getLayoutParams().height = (int) ((float) actualHeight * (48f / 315f));
                                     planetbtns[i].setVisibility(View.VISIBLE);
                                     planetbtns[i].setTypeface(font);
                                     planetbtns[i].setTextColor(Color.WHITE);
                                     planetbtns[i].invalidate();

                                     FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                                     params2.gravity = Gravity.START | Gravity.TOP;

                                     params2.leftMargin = (int) ((float) (xi - 16) * (float) actualWidth / 560f);
                                     params2.topMargin = (int) ((float) (yi - 16) * (float) actualHeight / 315f);
                                     params2.width = (int) ((float) actualWidth * (80f / 560f));
                                     params2.height = (int) ((float) actualHeight * (80f / 315f));

                                     planetrings[i] = new ImageView(this);
                                     planetrings[i].setVisibility(View.INVISIBLE);
                                     planetrings[i].setEnabled(true);
                                     flay.addView(planetrings[i]);
                                     planetrings[i].setLayoutParams(params2);
                                     planetrings[i].invalidate();
                     }
                     for (int i = 0; i < 10; i++)
                     {
                                     int ddmin1 = 100000;
                                     int ddmin2 = 100000;
                                     int nj = -1;
                                     for (int j = 0; j < 10; j++)
                                     {
                                                     int dd = (int) (Math.abs(xps[i] - xps[j]) + Math.abs(yps[i] - yps[j]));
                                                     if (dd < ddmin1 && i != j && xps[i] != xps[j] && yps[i] != yps[j])
                                                     {
                                                                     if (nodes[i + j * 10] != 1 && nodes[j + i * 10] != 1)
                                                                     {
                                                                                     ddmin1 = dd;
                                                                                     nj = j;
                                                                     }
                                                     }
                                     }
                                     if (nj != -1)
                                     {
                                                     nodes[i + nj * 10] = 1;
                                                     nodes[nj + i * 10] = 1;
                                     }

                                     nj = -1;
                                     for (int j = 0; j < 10; j++)
                                     {
                                                     int dd = (int) (Math.abs(xps[i] - xps[j]) + Math.abs(yps[i] - yps[j]));
                                                     if (dd < ddmin2 && i != j && xps[i] != xps[j] && yps[i] != yps[j] && dd > ddmin1)
                                                     {
                                                                     if (nodes[i + j * 10] != 1 && nodes[j + i * 10] != 1)
                                                                     {
                                                                                     ddmin2 = dd;
                                                                                     nj = j;
                                                                     }
                                                     }
                                     }
                                     if (nj != -1)
                                     {
                                                     nodes[i + nj * 10] = 1;
                                                     nodes[nj + i * 10] = 1;
                                     }

                                     planetbtns[i].setEnabled(false);
                                     planetbtns[i].setId(i);
                                     planetbtns[i].setOnClickListener(new View.OnClickListener()
                                     {
                                                     public void onClick(View v)
                                                     {

                                                                     final int teamchosen = tmps[v.getId()];

                                                                     if (callsigns != null)
                                                                     {
                                                                                     if (callsigns.length > v.getId() && v.getId() >= 0)
                                                                                     {
                                                                                                     defname = callsigns[v.getId()];
                                                                                     }

                                                                     }
                                                                     attplanetindex = v.getId() + 1;
                                                                     if (attplanetindex > 5)
                                                                     {
                                                                                     attplanetindex = 11 - attplanetindex;
                                                                     }
                                                                     if (!targetsresult.equals(""))

                                                                     {
                                                                                     String[] records = targetsresult.split(":");
                                                                                     for (int i = 0; i < records.length; i++)
                                                                                     {
                                                                                                     String[] fields = records[i].split("\\|");
                                                                                                     if (teamchosen == Integer.decode(fields[0]))
                                                                                                     {

                                                                                                                     enemyfleetid = Integer.decode(fields[2]);
                                                                                                                     // attack this one......
                                                                                                                     Intent serviceintent = new Intent(LaunchAttackActivity.this, UploadService.class);
                                                                                                                     if (Build.VERSION.SDK_INT >= 12)
                                                                                                                     {
                                                                                                                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                                                                                                     }
                                                                                                                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.launchattack.update");
                                                                                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.myfleetid", fleetid);
                                                                                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.enemyfleetid", enemyfleetid);
                                                                                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.xcoord", v.getId()); // totally
                                                                                                                                                                                                                                                                                                                                                                                                                                                                             // unnecessary
                                                                                                                                                                                                                                                                                                                                                                                                                                                                             // now.....
                                                                                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.ycoord", v.getId());
                                                                                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);
                                                                                                                     startService(serviceintent);
                                                                                                                     for (int j = 0; j < 10; j++)
                                                                                                                     {
                                                                                                                                     planetbtns[j].setEnabled(false);
                                                                                                                                     if (j == v.getId())
                                                                                                                                     {
                                                                                                                                                     planetrings[j].setActivated(true);
                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     planetrings[j].setEnabled(false);
                                                                                                                                     }
                                                                                                                     }
                                                                                                                     // newtoast("Preparing for battle - please wait");
                                                                                                                     runOnUiThread(new Runnable()
                                                                                                                     {
                                                                                                                                     @Override
                                                                                                                                     public void run()
                                                                                                                                     {
                                                                                                                                                     progwheel.setVisibility(View.VISIBLE);
                                                                                                                                     }
                                                                                                                     })
;
                                                                                                     }
                                                                                     }
                                                                     }

                                                     }
                                     });
                     }
                     bvidresid = R.raw.mapmov;
                     // bvidresid = R.raw.contmov; //for now
                     bhidevidonfinish = true;
                     bvideo = new SurfaceView(this);
                     bvideo.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                     flay.addView(bvideo);

                     txtwait = (TextView) findViewById(R.id.txtpleasewait2);
                     txtwait.setTypeface(font);
                     txtwait.setVisibility(View.VISIBLE);
                     txtwait.bringToFront();
                     try
                     {
                                     flay.requestLayout();
                                     flay.invalidate();
                     }
                     catch (Exception e)
                     {
                                     Log.w(TAG, "error bringing to front");
                     }

                     // bvideo = (SurfaceView) findViewById(R.id.vidsurfacelaunchattack);

                     requestlist();
     }

     @SuppressLint("InlinedApi")
     private void requestlist()
     {
                     if (waiting)
                     {
                                     newtoast("Please wait....trying to talk to database");
                                     return;
                     }
                     waiting = true;

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

     }

     @SuppressLint("InlinedApi")
     private void requestlist(boolean tmp)
     {
                     if (tmp)
                     {
                                     Intent serviceintent = new Intent(LaunchAttackActivity.this, UploadService.class);
                                     if (Build.VERSION.SDK_INT >= 12)
                                     {
                                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                     }
                                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.launchattack.load");
                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid);
                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.gameid", gameid);
                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);
                                     startService(serviceintent);
                     }
     }

     @Override
     protected void onResume()
     {
                     super.onResume();
                     battlefile = "";
                     setreceiver();
                     if (isloggedin() && loginstatuschanged())
                     {
                                     requestlist();
                     }
                     setuphelp(R.string.htt_map, R.string.htxt_map, R.string.htt_map);
     }

     @Override
     protected void onPause()
     {
                     super.onPause();
                     if (loadreceiver != null)
                     {
                                     unregisterReceiver(loadreceiver);
                     }
                     loadreceiver = null;
                     if (updatereceiver != null)
                     {
                                     unregisterReceiver(updatereceiver);
                     }
                     updatereceiver = null;
                     if (simprogressreceiver != null)
                     {
                                     unregisterReceiver(simprogressreceiver);
                     }
                     simprogressreceiver = null;
                     if (isFinishing())
                     {
                                     if (goingback && battlefile.equals(""))
                                     {
                                                     onChangeActivity(1);
                                     }
                     }
     }

     @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.fleetid", fleetid).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid));
                                     finish();
                     }
                     if (id == 0) // unlikely - flow seems wrong.
                     {
                                     startActivity(new Intent(this, MainMenuActivity.class));
                     }
                     if (id == 3) // while testing...
                     {
                                     // setkillmusicflag();
                                     goingback = false;
                                     Log.i(TAG, "Replay file sent to test activity:" + replayfile);
                                     Log.i(TAG, "TIME:(START REPLAY ACTIVITY):" + System.currentTimeMillis());
                                     startActivity(new Intent(this, TestActivity.class).putExtra("com.lloydm.geosword.activities.TestActivity.replayfile", replayfile).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.attplanetid", attplanetindex).putExtra("com.lloydm.geosword.common.UploadService.attname", attname).putExtra("com.lloydm.geosword.common.UploadService.defname", defname));
                                     finish();
                     }
     }

     private void setreceiver()
     {
                     if (simprogressreceiver == null)
                     {
                                     simprogressreceiver = new UploadCompleteBroadcastReceiver()
                                     {
                                                     @Override
                                                     public void onReceive(Context context, Intent intent)
                                                     {
                                                                     final float simprog = intent.getFloatExtra("com.lloydm.geosword.activities.LaunchAttackActivity.simprogvalue", 0)
*
100f;
                                                                     // do something with this.....even if it is just logging it...
                                                                     Log.i(TAG, "Sim progress:" + simprog);

                                                                     runOnUiThread(new Runnable()
                                                                     {

                                                                                     @Override
                                                                                     public void run()
                                                                                     {
                                                                                                     try
                                                                                                     {
                                                                                                                     int prg = (int) simprog;
                                                                                                                     String strsp = Integer.toString(prg);
                                                                                                                     String status = "Preparing for Battle";

                                                                                                                     if (prg < 20)
                                                                                                                     {
                                                                                                                                     status = "Preparing for Warp Jump " + strsp + "%";
                                                                                                                     }
                                                                                                                     if (prg >= 20 && prg < 30)
                                                                                                                     {
                                                                                                                                     status = "Entering System " + strsp + "%";
                                                                                                                     }
                                                                                                                     if (prg >= 30 && prg < 50)
                                                                                                                     {
                                                                                                                                     status = "Arming Weapons And Shields " + strsp + "%";
                                                                                                                     }
                                                                                                                     if (prg >= 50 && prg < 85)
                                                                                                                     {
                                                                                                                                     status = "Checking Comms " + strsp + "%";
                                                                                                                     }
                                                                                                                     if (prg >= 85 && prg < 95)
                                                                                                                     {
                                                                                                                                     status = "Engaging " + strsp + "%";
                                                                                                                     }
                                                                                                                     if (prg >= 95)
                                                                                                                     {
                                                                                                                                     status = "Visual Contact Established " + strsp + "%";
                                                                                                                     }
                                                                                                                     txtsimprog.setText(status);

                                                                                                                     txtsimprog.setVisibility(View.VISIBLE);
                                                                                                                     txtsimprog.bringToFront();
                                                                                                                     flay.requestLayout();
                                                                                                                     flay.invalidate();
                                                                                                     }
                                                                                                     catch (Exception e)
                                                                                                     {
                                                                                                                     Log.e(TAG, "error converting simprog to string");
                                                                                                     }

                                                                                     }
                                                                     });

                                                     }
                                     };
                                     registerReceiver(simprogressreceiver, new IntentFilter("com.lloydm.geosword.activities.LaunchAttackActivity.simprogress"));
                     }
                     if (loadreceiver == null)
                     {
                                     loadreceiver = 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.LaunchAttackActivity.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;

                                                                     }
                                                                     runOnUiThread(new Runnable()
                                                                     {
                                                                                     @Override
                                                                                     public void run()
                                                                                     {
                                                                                                     progwheel.setVisibility(View.INVISIBLE);
                                                                                     }
                                                                     })
;
                                                                     if (success)
                                                                     {
                                                                                     targetsresult = intent.getStringExtra("com.lloydm.geosword.activities.LaunchAttackActivity.attacktargets");
                                                                                     starmapresult = intent.getStringExtra("com.lloydm.geosword.activities.LaunchAttackActivity.starmap");
                                                                                     final String nameresult = intent.getStringExtra("com.lloydm.geosword.activities.LaunchAttackActivity.callsigns");
                                                                                     Log.i(TAG, "gettargets:" + targetsresult + " getstarmap:" + starmapresult + " nameresult:" + nameresult);
                                                                                     String[] cteam = new String[5];
                                                                                     for (int i = 0; i < 5; i++)
                                                                                     {
                                                                                                     cteam[i] = "";
                                                                                     }
                                                                                     if (nameresult.contains(":"))
                                                                                     {
                                                                                                     String[] nlist = nameresult.split(":");
                                                                                                     if (nlist != null)
                                                                                                     {
                                                                                                                     for (int i = 0; i < nlist.length; i++)
                                                                                                                     {
                                                                                                                                     try
                                                                                                                                     {
                                                                                                                                                     String[] fields = nlist[i].split("\\|");
                                                                                                                                                     String callsign = fields[0];
                                                                                                                                                     int callsignteam = Integer.decode(fields[1]);
                                                                                                                                                     if (callsignteam >= 1 && callsignteam <= 5)
                                                                                                                                                     {
                                                                                                                                                                     cteam[callsignteam - 1] = callsign;
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                                     catch (Exception e)
                                                                                                                                     {
                                                                                                                                                     Log.i(TAG, "no data in teamresult:" + i);
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                     }

                                                                                     // simply use the first 10 digits of the starmap values
                                                                                     // should be 1-5.........
                                                                                     for (int i = 0; i < 10; i++)
                                                                                     {
                                                                                                     tmps[i] = Integer.decode(starmapresult.substring(i, i + 1));
                                                                                                     callsigns[i] = cteam[tmps[i] - 1];
                                                                                                     validids[i] = 0;
                                                                                     }
                                                                                     // everything is currently disabled....
                                                                                     // enable the buttons that are actually open and
                                                                                     // available.....(ie valid enemies)
                                                                                     // also hide everything that is not our neighbour.......

                                                                                     for (int i = 0; i < 10; i++)
                                                                                     {

                                                                                                     if (tmps[i] == teamid)
                                                                                                     {
                                                                                                                     for (int j = 0; j < 10; j++)
                                                                                                                     {
                                                                                                                                     if (tmps[j] != teamid)
                                                                                                                                     {
                                                                                                                                                     if (nodes[i + j * 10] == 1)
                                                                                                                                                     {
                                                                                                                                                                     // we can go for this one.....
                                                                                                                                                                     // set the button to enabled.....
                                                                                                                                                                     validids[j] = 1;
                                                                                                                                                                     final int jtarget = j;
                                                                                                                                                                     if (targetsresult.equals("none"))
                                                                                                                                                                     {
                                                                                                                                                                                     // don't show any enemies.....
                                                                                                                                                                                     runOnUiThread(new Runnable()
                                                                                                                                                                                     {

                                                                                                                                                                                                     @Override
                                                                                                                                                                                                     public void run()
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     planetbtns[jtarget].setEnabled(false);
                                                                                                                                                                                                                     planetbtns[jtarget].setVisibility(View.VISIBLE);
                                                                                                                                                                                                                     planetrings[jtarget].setVisibility(View.INVISIBLE);

                                                                                                                                                                                                     }
                                                                                                                                                                                     })
;

                                                                                                                                                                     }
                                                                                                                                                                     else
                                                                                                                                                                     {

                                                                                                                                                                                     String[] records = targetsresult.split(":");
                                                                                                                                                                                     for (int ti = 0; ti < records.length; ti++)
                                                                                                                                                                                     {
                                                                                                                                                                                                     String[] fields = records[ti].split("\\|");
                                                                                                                                                                                                     if (fields != null)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     if (fields.length > 0)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     if (tmps[jtarget] == Integer.decode(fields[0]))
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     runOnUiThread(new Runnable()
                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                     @Override
                                                                                                                                                                                                                                                                     public void run()
                                                                                                                                                                                                                                                                     {

                                                                                                                                                                                                                                                                                     planetbtns[jtarget].setEnabled(true);

                                                                                                                                                                                                                                                                                     if (tmps[jtarget] == 1)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     planetrings[jtarget].setBackgroundResource(R.drawable.full_red_pt);
                                                                                                                                                                                                                                                                                                     planetbtns[jtarget].setTextColor(Color.RED);
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     if (tmps[jtarget] == 2)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     planetrings[jtarget].setBackgroundResource(R.drawable.full_blue_pt);
                                                                                                                                                                                                                                                                                                     planetbtns[jtarget].setTextColor(Color.CYAN);
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     if (tmps[jtarget] == 3)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     planetrings[jtarget].setBackgroundResource(R.drawable.full_yellow_pt);
                                                                                                                                                                                                                                                                                                     planetbtns[jtarget].setTextColor(Color.YELLOW);
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     if (tmps[jtarget] == 4)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     planetrings[jtarget].setBackgroundResource(R.drawable.full_green_pt);
                                                                                                                                                                                                                                                                                                     planetbtns[jtarget].setTextColor(Color.GREEN);
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     if (tmps[jtarget] == 5)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     planetrings[jtarget].setBackgroundResource(R.drawable.full_purple_pt);
                                                                                                                                                                                                                                                                                                     planetbtns[jtarget].setTextColor(Color.MAGENTA);
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     planetbtns[jtarget].setVisibility(View.VISIBLE);
                                                                                                                                                                                                                                                                                     planetrings[jtarget].setVisibility(View.VISIBLE);
                                                                                                                                                                                                                                                                                     if (!callsigns[jtarget].equals(""))

                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     if (tmps[jtarget] >= 1 && tmps[jtarget] <= 5)
                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                     if (!callsignsshown[tmps[jtarget] - 1])
                                                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                                                     planetbtns[jtarget].setText(callsigns[jtarget]);
                                                                                                                                                                                                                                                                                                                                     planetbtns[jtarget].invalidate();
                                                                                                                                                                                                                                                                                                                                     // callsignsshown[tmps[jtarget]-1] = true;
                                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     try
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     txthelp.bringToFront();
                                                                                                                                                                                                                                                                                                     flay.requestLayout();
                                                                                                                                                                                                                                                                                                     flay.invalidate();
                                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                                                     catch (Exception e)
                                                                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                                                                     Log.i(TAG, "cant bring txtview to front");
                                                                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                                     });
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     }
                                                                                                                                                                                                     }
                                                                                                                                                                                     }

                                                                                                                                                                     }

                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                                     // put our own stuff here
                                                                                                                                     final int myi = i;
                                                                                                                                     runOnUiThread(new Runnable()
                                                                                                                                     {
                                                                                                                                                     @Override
                                                                                                                                                     public void run()
                                                                                                                                                     {
                                                                                                                                                                     if (tmps[myi] == 1)
                                                                                                                                                                     {
                                                                                                                                                                                     planetrings[myi].setBackgroundResource(R.drawable.full_red_pt);
                                                                                                                                                                                     planetbtns[myi].setTextColor(Color.RED);
                                                                                                                                                                     }
                                                                                                                                                                     if (tmps[myi] == 2)
                                                                                                                                                                     {
                                                                                                                                                                                     planetrings[myi].setBackgroundResource(R.drawable.full_blue_pt);
                                                                                                                                                                                     planetbtns[myi].setTextColor(Color.CYAN);
                                                                                                                                                                     }
                                                                                                                                                                     if (tmps[myi] == 3)
                                                                                                                                                                     {
                                                                                                                                                                                     planetrings[myi].setBackgroundResource(R.drawable.full_yellow_pt);
                                                                                                                                                                                     planetbtns[myi].setTextColor(Color.YELLOW);
                                                                                                                                                                     }
                                                                                                                                                                     if (tmps[myi] == 4)
                                                                                                                                                                     {
                                                                                                                                                                                     planetrings[myi].setBackgroundResource(R.drawable.full_green_pt);
                                                                                                                                                                                     planetbtns[myi].setTextColor(Color.GREEN);
                                                                                                                                                                     }
                                                                                                                                                                     if (tmps[myi] == 5)
                                                                                                                                                                     {
                                                                                                                                                                                     planetrings[myi].setBackgroundResource(R.drawable.full_purple_pt);
                                                                                                                                                                                     planetbtns[myi].setTextColor(Color.MAGENTA);
                                                                                                                                                                     }
                                                                                                                                                                     planetbtns[myi].setVisibility(View.VISIBLE);
                                                                                                                                                                     planetrings[myi].setVisibility(View.VISIBLE);

                                                                                                                                                                     if (!callsigns[myi].equals(""))

                                                                                                                                                                     {
                                                                                                                                                                                     if (tmps[myi] >= 1 && tmps[myi] <= 5)
                                                                                                                                                                                     {
                                                                                                                                                                                                     if (!callsignsshown[tmps[myi] - 1])
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     planetbtns[myi].setText(callsigns[myi]);
                                                                                                                                                                                                                     attname = callsigns[myi];
                                                                                                                                                                                                                     planetbtns[myi].invalidate();
                                                                                                                                                                                                                     // callsignsshown[tmps[myi]-1] = true;
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                     }

                                                                                                                                                                     // bbeginfade = true;

                                                                                                                                                                     try
                                                                                                                                                                     {
                                                                                                                                                                                     txthelp.bringToFront();
                                                                                                                                                                                     flay.requestLayout();
                                                                                                                                                                                     flay.invalidate();
                                                                                                                                                                     }
                                                                                                                                                                     catch (Exception e)
                                                                                                                                                                     {
                                                                                                                                                                                     Log.i(TAG, "cant bring txtview to front");
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                     });

                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     goingback = true;
                                                                                     waiting = false;

                                                                                     int reason = intent.getIntExtra("com.lloydm.geosword.activities.LaunchAttackActivity.authenticationerror", -1);
                                                                                     if (reason == UploadService.LOGIN_NOT_FOUND)
                                                                                     {
                                                                                                     // show the authdialog.....
                                                                                                     login();
                                                                                     }
                                                                                     else
                                                                                     {// most likely no SD card or similar.....
                                                                                                     newtoast("Something went wrong during the upload process.");
                                                                                     }
                                                                     }
                                                                     // reset waiting flag.....
                                                     }
                                     };
                                     registerReceiver(loadreceiver, new IntentFilter("com.lloydm.geosword.activities.LaunchAttackActivity.load"));
                     }
                     if (updatereceiver == null)
                     {
                                     updatereceiver = 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.LaunchAttackActivity.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;

                                                                     }
                                                                     if (success)
                                                                     {
                                                                                     runOnUiThread(new Runnable()
                                                                                     {
                                                                                                     @Override
                                                                                                     public void run()
                                                                                                     {
                                                                                                                     progwheel.setVisibility(View.INVISIBLE);
                                                                                                     }
                                                                                     })
;

                                                                                     // check result first.......
                                                                                     final String attresult = intent.getStringExtra("com.lloydm.geosword.activities.LaunchAttackActivity.attresult");
                                                                                     boolean playbattle = true;
                                                                                     if (attresult != null)
                                                                                     {
                                                                                                     if (attresult.equals("A0"))
                                                                                                     {
                                                                                                                     // we have no ships....
                                                                                                                     // change the text message.....and grey out all the planetrings......
                                                                                                                     runOnUiThread(new Runnable()
                                                                                                                     {
                                                                                                                                     @Override
                                                                                                                                     public void run()
                                                                                                                                     {
                                                                                                                                                     txthelp.setText(R.string.planethelp2);
                                                                                                                                                     txthelp.invalidate();
                                                                                                                                                     for (int i = 0; i < planetbtns.length; i++)
                                                                                                                                                     {
                                                                                                                                                                     planetbtns[i].setEnabled(false);
                                                                                                                                                                     planetbtns[i].invalidate();
                                                                                                                                                     }
                                                                                                                                                     for (int i = 0; i < planetrings.length; i++)
                                                                                                                                                     {
                                                                                                                                                                     planetrings[i].setEnabled(false);
                                                                                                                                                                     planetrings[i].setActivated(false);
                                                                                                                                                                     planetrings[i].invalidate();
                                                                                                                                                     }
                                                                                                                                                     requestlist(true);
                                                                                                                                     }
                                                                                                                     })
;
                                                                                                                     playbattle = false;
                                                                                                     }
                                                                                                     if (attresult.equals("D0"))
                                                                                                     {
                                                                                                                     // they have no ships....
                                                                                                                     runOnUiThread(new Runnable()
                                                                                                                     {
                                                                                                                                     @Override
                                                                                                                                     public void run()
                                                                                                                                     {
                                                                                                                                                     txthelp.setText(R.string.planethelp3);
                                                                                                                                                     txthelp.invalidate();
                                                                                                                                                     for (int i = 0; i < planetbtns.length; i++)
                                                                                                                                                     {
                                                                                                                                                                     planetbtns[i].setEnabled(false);
                                                                                                                                                                     planetbtns[i].invalidate();
                                                                                                                                                     }
                                                                                                                                                     for (int i = 0; i < planetrings.length; i++)
                                                                                                                                                     {
                                                                                                                                                                     planetrings[i].setEnabled(false);
                                                                                                                                                                     planetrings[i].setActivated(false);
                                                                                                                                                                     planetrings[i].invalidate();
                                                                                                                                                     }
                                                                                                                                                     requestlist(true);
                                                                                                                                     }
                                                                                                                     })
;
                                                                                                                     playbattle = false;
                                                                                                     }
                                                                                     }
                                                                                     if (playbattle)
                                                                                     {
                                                                                                     battlefile = intent.getStringExtra("com.lloydm.geosword.activities.LaunchAttackActivity.battlefile");
                                                                                                     // final long elapsedtime = intent.getLongExtra("com.lloydm.geosword.activities.LaunchAttackActivity.elapsedtime",0);
                                                                                                     replayfile = battlefile; // oops naming error
                                                                                                     runOnUiThread(new Runnable()
                                                                                                     {
                                                                                                                     @Override
                                                                                                                     public void run()
                                                                                                                     {
                                                                                                                                     // for now.....
                                                                                                                                     if (!battlefile.equals(""))

                                                                                                                                     {
                                                                                                                                                     /*
                                                                                                                                                         * Log.i(TAG, "Attempting to upload replay to server as well - we don't care about a result...." ); Intent serviceintent = new Intent(LaunchAttackActivity
                                                                                                                                                         * .this,ReplayTransferService.class)
; if(Build.VERSION.SDK_INT>=12) { serviceintent.addFlags(Intent. FLAG_INCLUDE_STOPPED_PACKAGES); } serviceintent.setAction(
                                                                                                                                                         * "com.lloydm.geosword.common.ReplayTransferService.send" )
; serviceintent.putExtra( "com.lloydm.geosword.common.ReplayTransferService.localfile" ,
                                                                                                                                                         * battlefile)
; startService(serviceintent);
                                                                                                                                                         *
/
                                                                                                                                                     // newtoast("Battle Replay Loading");
                                                                                                                                                     goingback = false;
                                                                                                                                                     waiting = false;
                                                                                                                                                     onChangeActivity(3);
                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     newtoast("Battle file error");
                                                                                                                                     }
                                                                                                                     }
                                                                                                     });
                                                                                     }
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     if (tuid == uid)
                                                                                     {
                                                                                                     runOnUiThread(new Runnable()
                                                                                                     {
                                                                                                                     @Override
                                                                                                                     public void run()
                                                                                                                     {
                                                                                                                                     progwheel.setVisibility(View.INVISIBLE);
                                                                                                                     }
                                                                                                     })
;

                                                                                     }
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     int reason = intent.getIntExtra("com.lloydm.geosword.activities.LaunchAttackActivity.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(updatereceiver, new IntentFilter("com.lloydm.geosword.activities.LaunchAttackActivity.update"));
                     }
     }
}