package com.lloydm.geosword.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.view.SurfaceView;

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 ModifyAIActivity extends StandardGameMenuActivity
{
     // making modifications.....
     // we are now setting these per SHIPTYPE not per SHIP
     // too many options for users currently.

     // We need a plus or minus as a helper indicator....

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

     private int gameid = -1;
     private int playerid = -1;
     private int fleetid = -1;
     private int shipid = -1;

     private int teamid = -1;
     private String slidervalues = "";
     private int shiptypeid = -1;

     private String currentaisettings = "";

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

     private TextView[] txtainame = null;
     private SeekBar[] seekai = null;
     // private TextView txtdescription = null;

     private FrameLayout flay = null;

     private String[] descs = null;

     private boolean dontupdate = true;

     private Button btnshiptype = null;

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

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

     @SuppressLint("NewApi")
     @Override
     public void setup()
     {
                     setContentView(R.layout.activity_modifyai);
                     mainview = (View) findViewById(R.layout.activity_modifyai);

                     worldcampaign = getIntent().getBooleanExtra("com.lloydm.geosword.activities.PreGameLobbyActivity.worldcampaign", false);

                     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);
                     shipid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.shipid", -1);

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

                     // we will keep shipid in this although it is now just a dummy var...

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

                     txtainame = new TextView[16];
                     seekai = new SeekBar[16];

                     btnshiptype = (Button) findViewById(R.id.aishiptype);

                     if (shiptypeid == 1)
                     {
                                     btnshiptype.setBackgroundResource(R.drawable.full_menu_button_left_blue_cap);
                     }
                     if (shiptypeid == 2)
                     {
                                     btnshiptype.setBackgroundResource(R.drawable.full_menu_button_left_blue_cruiser);
                     }
                     if (shiptypeid == 3)
                     {
                                     btnshiptype.setBackgroundResource(R.drawable.full_menu_button_left_blue_bomber);
                     }
                     if (shiptypeid == 4)
                     {
                                     btnshiptype.setBackgroundResource(R.drawable.full_menu_button_left_blue_fighter);
                     }
                     btnshiptype.setVisibility(View.VISIBLE);
                     btnshiptype.setOnClickListener(new View.OnClickListener()
                     {

                                     @Override
                                     public void onClick(View v)
                                     {
                                                     if (shipid != -1)
                                                     {
                                                                     if (!worldcampaign)
                                                                     {
                                                                                     onChangeActivity(4);
                                                                     }
                                                     }
                                     }
                     })
;

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

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

                     txtainame[0] = (TextView) findViewById(R.id.aiindex1);
                     txtainame[1] = (TextView) findViewById(R.id.aiindex2);
                     txtainame[2] = (TextView) findViewById(R.id.aiindex3);
                     txtainame[3] = (TextView) findViewById(R.id.aiindex4);
                     txtainame[4] = (TextView) findViewById(R.id.aiindex5);
                     txtainame[5] = (TextView) findViewById(R.id.aiindex6);
                     txtainame[6] = (TextView) findViewById(R.id.aiindex7);
                     txtainame[7] = (TextView) findViewById(R.id.aiindex8);
                     txtainame[8] = (TextView) findViewById(R.id.aiindex9);
                     txtainame[9] = (TextView) findViewById(R.id.aiindex10);
                     txtainame[10] = (TextView) findViewById(R.id.aiindex11);
                     txtainame[11] = (TextView) findViewById(R.id.aiindex12);
                     txtainame[12] = (TextView) findViewById(R.id.aiindex13);
                     txtainame[13] = (TextView) findViewById(R.id.aiindex14);
                     txtainame[14] = (TextView) findViewById(R.id.aiindex15);
                     txtainame[15] = (TextView) findViewById(R.id.aiindex16);

                     if (worldcampaign)
                     {
                                     txtainame[0].setText(getString(R.string.ai1));
                                     txtainame[1].setText(getString(R.string.ai2));
                                     txtainame[2].setText(getString(R.string.ai3));
                                     txtainame[3].setText(getString(R.string.ai4));
                                     txtainame[4].setText(getString(R.string.ai5));
                                     txtainame[5].setText(getString(R.string.ai6));
                                     txtainame[6].setText(getString(R.string.ai7));
                                     txtainame[7].setText(getString(R.string.ai8));
                                     txtainame[8].setText(getString(R.string.ai9));
                                     txtainame[9].setText(getString(R.string.ai10));
                                     txtainame[10].setText(getString(R.string.ai11));
                                     txtainame[11].setText(getString(R.string.ai12));
                                     txtainame[12].setText(getString(R.string.ai13));
                                     txtainame[13].setText(getString(R.string.ai14));
                                     txtainame[14].setText(getString(R.string.ai15));
                                     txtainame[15].setText(getString(R.string.ai16));

                     }

                     seekai[0] = (SeekBar) findViewById(R.id.ai1);
                     seekai[1] = (SeekBar) findViewById(R.id.ai2);
                     seekai[2] = (SeekBar) findViewById(R.id.ai3);
                     seekai[3] = (SeekBar) findViewById(R.id.ai4);
                     seekai[4] = (SeekBar) findViewById(R.id.ai5);
                     seekai[5] = (SeekBar) findViewById(R.id.ai6);
                     seekai[6] = (SeekBar) findViewById(R.id.ai7);
                     seekai[7] = (SeekBar) findViewById(R.id.ai8);
                     seekai[8] = (SeekBar) findViewById(R.id.ai9);
                     seekai[9] = (SeekBar) findViewById(R.id.ai10);
                     seekai[10] = (SeekBar) findViewById(R.id.ai11);
                     seekai[11] = (SeekBar) findViewById(R.id.ai12);
                     seekai[12] = (SeekBar) findViewById(R.id.ai13);
                     seekai[13] = (SeekBar) findViewById(R.id.ai14);
                     seekai[14] = (SeekBar) findViewById(R.id.ai15);
                     seekai[15] = (SeekBar) findViewById(R.id.ai16);

                     for (int i = 0; i < 16; i++)
                     {
                                     seekai[i].setEnabled(false);

                                     if (Build.VERSION.SDK_INT >= 16)
                                     {
                                                     seekai[i].getThumb().setAlpha(0);
                                     }
                                     seekai[i].setId(i);
                                     seekai[i].setEnabled(false); // don't allow updates until internet
                                                                                                                                                         // accessed....and most recent
                                                                                                                                                         // received...
                                     if (worldcampaign)
                                     {

                                                     final int ii = Integer.decode(getprefs("com.lloydm.geosword.activities.ModifyAIActivity.ai_" + shiptypeid + "_" + i, "5"));

                                                     seekai[i].setProgress(ii);
                                                     seekai[i].setEnabled(true);
                                     }
                                     seekai[i].setOnFocusChangeListener(new View.OnFocusChangeListener()
                                     {
                                                     @Override
                                                     public void onFocusChange(View v, boolean hasFocus)
                                                     {
                                                                     if (dontupdate && !worldcampaign)
                                                                     {
                                                                                     return;
                                                                     }
                                                                     if (hasFocus)
                                                                     {
                                                                                     v.setAlpha(1);
                                                                     }
                                                                     else
                                                                     {
                                                                                     v.setAlpha(0.7f);
                                                                     }
                                                     }
                                     })
;
                                     seekai[i].setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
                                     {
                                                     @Override
                                                     public void onStopTrackingTouch(SeekBar seekBar)
                                                     {
                                                                     if (dontupdate && !worldcampaign)
                                                                     {
                                                                                     return;
                                                                     }
                                                                     if (changed)
                                                                     {
                                                                                     if (worldcampaign)
                                                                                     {
                                                                                                     ModifyAIActivity.this.saveprefs("com.lloydm.geosword.activities.ModifyAIActivity.ai_" + shiptypeid + "_" + seekBar.getId(), Integer.toString(seekBar.getProgress()));

                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     updateslidersettings(seekBar.getId(), seekBar.getProgress());
                                                                                     }

                                                                                     changed = false;
                                                                     }
                                                     }

                                                     @Override
                                                     public void onStartTrackingTouch(SeekBar seekBar)
                                                     {

                                                     }

                                                     @Override
                                                     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
                                                     {
                                                                     changed = true;
                                                     }
                                     })
;

                     }
                     if (!worldcampaign)
                     {
                                     setreceiver();
                     }
                     // get shared prefs here.....

                     if (!worldcampaign)
                     {
                                     final String aiinfo = getprefs("gameaidata_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid) + "_" + Integer.toString(fleetid) + "_" + Integer.toString(shipid) + "_" + Integer.toString(shiptypeid), "none");
                                     if (!aiinfo.equals("none"))

                                     {
                                                     currentaisettings = aiinfo;
                                                     final String sliderarray[] = currentaisettings.split(":");

                                                     if (sliderarray != null)
                                                     {
                                                                     if (sliderarray.length == 16)
                                                                     {
                                                                                     descs = new String[16];
                                                                                     for (int i = 0; i < 16; i++)
                                                                                     {
                                                                                                     String fields[] = sliderarray[i].split("\\|");

                                                                                                     if (fields != null)
                                                                                                     {
                                                                                                                     Log.i(TAG, "Fields Length:" + fields.length);
                                                                                                                     Log.i(TAG, "fields txt:" + sliderarray[i]);
                                                                                                                     descs[i] = fields[2];
                                                                                                                     // txtainame[i].setText(fields[1] + " - " + fields[2]);
                                                                                                                     txtainame[i].setText(fields[2]);
                                                                                                                     seekai[i].setProgress(Integer.decode(fields[6]));
                                                                                                     }
                                                                                     }
                                                                     }
                                                     }
                                     }
                                     else
                                     {
                                                     // only show the ai video if the settings haven't been set yet....
                                                     flay = (FrameLayout) findViewById(R.id.frmlay_modifyai);
                                                     bvidresid = R.raw.aimov;
                                                     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.txtpleasewait3);
                                                     txtwait.setTypeface(font);
                                                     txtwait.setVisibility(View.VISIBLE);
                                                     txtwait.bringToFront();
                                                     try
                                                     {
                                                                     flay.requestLayout();
                                                                     flay.invalidate();
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.w(TAG, "error bringing to front");
                                                     }
                                     }
                                     requestlist();
                     }
     }

     @SuppressLint("InlinedApi")
     private void updateslidersettings(int geindex, int valint)
     {
                     if (geindex < 0 || geindex > 15)
                     {
                                     Log.i(TAG, "GE INDEX ERROR:" + geindex);
                     }
                     if (valint < 0 || valint > 9)
                     {
                                     Log.i(TAG, "VALINT ERROR:" + valint);

                     }
                     Intent serviceintent = new Intent(ModifyAIActivity.this, UploadService.class);
                     if (Build.VERSION.SDK_INT >= 12)
                     {
                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                     }
                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.modifyai.update");
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.shipid", shipid);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.geindex", geindex);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.valint", valint);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);
                     startService(serviceintent);
     }

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

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

                     Intent serviceintent = new Intent(ModifyAIActivity.this, UploadService.class);
                     if (Build.VERSION.SDK_INT >= 12)
                     {
                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                     }
                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.modifyai.load");
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.shipid", shipid);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);
                     startService(serviceintent);
                     // newtoast("Contacting database");
     }

     @Override
     protected void onResume()
     {
                     super.onResume();
                     if (!worldcampaign)
                     {
                                     setreceiver();
                                     // there is a chance that we have just returned from the login screen -
                                     // in which case....
                                     // if we are logged in then we most likely want to attempt to redownload
                                     // stuff....
                                     if (isloggedin() && loginstatuschanged())
                                     {
                                                     requestlist();
                                     }
                     }
                     setuphelp(R.string.htt_ai, R.string.htxt_ai, R.string.htt_ai);

     }

     @Override
     protected void onPause()
     {
                     super.onPause();
                     if (loadreceiver != null)
                     {
                                     unregisterReceiver(loadreceiver);
                     }
                     loadreceiver = null;
                     if (updatereceiver != null)
                     {
                                     unregisterReceiver(updatereceiver);
                     }
                     updatereceiver = null;
                     if (isFinishing())
                     {
                                     if (goingback)
                                     {
                                                     // go to the previous activity...
                                                     if (!worldcampaign)
                                                     {
                                                                     onChangeActivity(1);
                                                     }
                                                     else
                                                     {
                                                                     onChangeActivity(11);
                                                     }
                                     }
                     }
     }

     @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).putExtra("com.lloydm.geosword.common.UploadService.shiptypeid", shiptypeid));
                                     finish();
                     }
                     if (id == 4)
                     {
                                     goingback = false;
                                     Log.d(TAG, "ShipTypeID:" + shiptypeid);

                                     startActivity(new Intent(this, ShipActivity.class).putExtra("com.lloydm.geosword.common.UploadService.gameid", gameid).putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid).putExtra("com.lloydm.geosword.common.UploadService.shipid", shipid).putExtra("com.lloydm.geosword.common.UploadService.shiptypeid", shiptypeid).putExtra("com.lloydm.geosword.common.UploadService.fleetid", fleetid).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid).putExtra("com.lloydm.geosword.common.UploadService.fromai", 1));
                                     finish();
                     }
                     if (id == 11)
                     {
                                     goingback = false;
                                     startActivity(new Intent(this, InspectFleetActivity.class).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid).putExtra("com.lloydm.geosword.common.UploadService.shiptypeid", shiptypeid).putExtra("com.lloydm.geosword.activities.PreGameLobbyActivity.worldcampaign", true));

                                     finish();
                     }
     }

     private void setreceiver()
     {
                     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.ModifyAIActivity.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;
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     return;
                                                                     }

                                                                     if (success)
                                                                     {
                                                                                     slidervalues = intent.getStringExtra("com.lloydm.geosword.activities.ModifyAIActivity.shipinfo");

                                                                                     if (slidervalues.equals(currentaisettings))
                                                                                     {
                                                                                                     // don't need to do anything - nothing has
                                                                                                     // changed......
                                                                                                     // simply enable the sliders.....
                                                                                                     runOnUiThread(new Runnable()
                                                                                                     {
                                                                                                                     @Override
                                                                                                                     public void run()
                                                                                                                     {
                                                                                                                                     for (int i = 0; i < 16; i++)
                                                                                                                                     {
                                                                                                                                                     seekai[i].setEnabled(true);
                                                                                                                                     }
                                                                                                                                     dontupdate = false;
                                                                                                                     }
                                                                                                     })
;
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     // update as normal....

                                                                                                     final String sliderarray[] = slidervalues.split(":");
                                                                                                     runOnUiThread(new Runnable()
                                                                                                     {
                                                                                                                     @Override
                                                                                                                     public void run()
                                                                                                                     {
                                                                                                                                     if (sliderarray != null)
                                                                                                                                     {
                                                                                                                                                     if (sliderarray.length == 16)
                                                                                                                                                     {
                                                                                                                                                                     descs = new String[16];
                                                                                                                                                                     for (int i = 0; i < 16; i++)
                                                                                                                                                                     {
                                                                                                                                                                                     String fields[] = sliderarray[i].split("\\|");


                                                                                                                                                                                     if (fields != null)
                                                                                                                                                                                     {
                                                                                                                                                                                                     Log.i(TAG, "Fields Length:" + fields.length);
                                                                                                                                                                                                     Log.i(TAG, "fields txt:" + sliderarray[i]);
                                                                                                                                                                                                     descs[i] = fields[2];
                                                                                                                                                                                                     // txtainame[i].setText(fields[1] + " - " + fields[2]);
                                                                                                                                                                                                     txtainame[i].setText(fields[2]);
                                                                                                                                                                                                     seekai[i].setProgress(Integer.decode(fields[6]));
                                                                                                                                                                                                     // seekai[i].setId(i);
                                                                                                                                                                                                     seekai[i].setEnabled(true);
                                                                                                                                                                                                     /*
                                                                                                                                                                                                         * seekai[i]. setOnFocusChangeListener (new View.OnFocusChangeListener () {
                                                                                                                                                                                                         *
                                                                                                                                                                                                         * @Override public void onFocusChange(View v, boolean hasFocus) { //txtdescription .setText(descs [v.getId()]); } }); seekai[i].
                                                                                                                                                                                                         * setOnSeekBarChangeListener (new SeekBar. OnSeekBarChangeListener() {
                                                                                                                                                                                                         *
                                                                                                                                                                                                         * @Override public void onStopTrackingTouch (SeekBar seekBar) {
                                                                                                                                                                                                         *
                                                                                                                                                                                                         * }
                                                                                                                                                                                                         *
                                                                                                                                                                                                         * @Override public void onStartTrackingTouch (SeekBar seekBar) { //txtdescription .setText(descs [seekBar.getId()]); }
                                                                                                                                                                                                         *
                                                                                                                                                                                                         * @Override public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) { if(dontupdate) { return; } updateslidersettings(seekBar
                                                                                                                                                                                                         * .getId(),progress)
; } })
;
                                                                                                                                                                                                         *
/
                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                                     dontupdate = false;
                                                                                                                                                                     saveprefs("gameaidata_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid) + "_" + Integer.toString(fleetid) + "_" + Integer.toString(shipid) + "_" + Integer.toString(shiptypeid), slidervalues);
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                     }
                                                                                                     })
;
                                                                                     }
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     bbeginfade = true;
                                                                                     if (txtwait != null)
                                                                                     {
                                                                                                     txtwait.postDelayed(new Runnable()
                                                                                                     {
                                                                                                                     @Override
                                                                                                                     public void run()
                                                                                                                     {
                                                                                                                                     txtwait.setVisibility(View.INVISIBLE);

                                                                                                                     }
                                                                                                     }, 9000)
;

                                                                                     }

                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     int reason = intent.getIntExtra("com.lloydm.geosword.activities.ModifyAIActivity.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(loadreceiver, new IntentFilter("com.lloydm.geosword.activities.ModifyAIActivity.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.ModifyAIActivity.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;
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     return;

                                                                     }

                                                                     if (success)
                                                                     {

                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     int reason = intent.getIntExtra("com.lloydm.geosword.activities.ModifyAIActivity.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.ModifyAIActivity.update"));
                     }
     }

}