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.SurfaceView;
import android.view.View;
import android.widget.Button;
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;

@SuppressLint("ClickableViewAccessibility")
public class ResearchActivity extends StandardGameMenuActivity
{
     // private final static String TAG =
     // "com.lloydm.geosword.activities.ResearchActivity";

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

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

     private String researchinfo = "";

     private String currentresearch = "";
     private int currentrid = -1;
     // private Animation animin = null;
     // private Animation animout= null;

     // private LinearLayout rtechs;
     // private LinearLayout atechs;
     // private TextView rdesc;
     // private ProgressBar pbar;
     // private TextView pbartxt;

     // private ArrayList<Button> btnrtechs;
     // private ArrayList<Button> btnatechs;

     // private ArrayList<String> rtechdesc;
     // private ArrayList<String> atechdesc;
     // private ArrayList<Integer> rtechindex;
     // private ArrayList<Integer> atechindex;

     private Button[] btns = null;
     // private SeekBar[] sk = null;
     private TextView[] tx = 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_research);
                     mainview = (View) findViewById(R.layout.activity_research);

                     bvidresid = R.raw.resmov;
                     bvideolooped = true;
                     bvideo = (SurfaceView) findViewById(R.id.vidsurfaceresearch);

                     // animin = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
                     // animout = AnimationUtils.loadAnimation(this,
                     // android.R.anim.fade_out);

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

                     /*
                         * rtechs = (LinearLayout) findViewById(R.id.layrtechs); atechs = (LinearLayout) findViewById(R.id.layatechs); rdesc = (TextView) findViewById(R.id.txtrdesc); pbar = (ProgressBar)
                         * findViewById(R.id.rprogbar); pbartxt = (TextView) findViewById(R.id.txtrprogress);
                         *
                         * btnrtechs = new ArrayList<Button>(); btnatechs = new ArrayList<Button>(); rtechdesc = new ArrayList<String>(); atechdesc = new ArrayList<String>(); rtechindex = new ArrayList<Integer>();
                         * atechindex = new ArrayList<Integer>();
                         *
/

                     btns = new Button[4];
                     // btns[0] = (Button) findViewById(R.id.capmob);
                     // btns[1] = (Button) findViewById(R.id.crsmob);
                     // btns[2] = (Button) findViewById(R.id.bmbmob);
                     // btns[3] = (Button) findViewById(R.id.fgtmob);

                     btns[0] = (Button) findViewById(R.id.rmob);
                     btns[1] = (Button) findViewById(R.id.roff);
                     btns[2] = (Button) findViewById(R.id.rdef);
                     btns[3] = (Button) findViewById(R.id.rcon);
                     /*
                         * sk = new SeekBar[4]; sk[0] = (SeekBar) findViewById(R.id.rmobprog); sk[1] = (SeekBar) findViewById(R.id.roffprog); sk[2] = (SeekBar) findViewById(R.id.rdefprog); sk[3] = (SeekBar)
                         * findViewById(R.id.rconprog);
                         *
/
                     tx = new TextView[4];
                     tx[0] = (TextView) findViewById(R.id.txtspdbonus);
                     tx[1] = (TextView) findViewById(R.id.txtoffbonus);
                     tx[2] = (TextView) findViewById(R.id.txtdefbonus);
                     tx[3] = (TextView) findViewById(R.id.txtconbonus);

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

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

                     /*
                         *
                         * btns[4] = (Button) findViewById(R.id.capoff); btns[5] = (Button) findViewById(R.id.crsoff); btns[6] = (Button) findViewById(R.id.bmboff); btns[7] = (Button) findViewById(R.id.fgtoff);
                         *
                         * btns[8] = (Button) findViewById(R.id.capdef); btns[9] = (Button) findViewById(R.id.crsdef); btns[10] = (Button) findViewById(R.id.bmbdef); btns[11] = (Button) findViewById(R.id.fgtdef);
                         *
                         * btns[12] = (Button) findViewById(R.id.capcon); btns[13] = (Button) findViewById(R.id.crscon); btns[14] = (Button) findViewById(R.id.bmbcon); btns[15] = (Button) findViewById(R.id.fgtcon);
                         *
/
                     for (int i = 0; i < 4; i++)
                     {
                                     btns[i].setId(i);
                                     tx[i].setTypeface(font);
                                     // sk[i].setId(i);
                                     // sk[i].setEnabled(true);
                                     // sk[i].setAlpha(1);
                                     // if (Build.VERSION.SDK_INT >= 16)
                                     // {
                                     // sk[i].getThumb().setAlpha(0);
                                     // }
                                     // sk[i].setOnTouchListener(new View.OnTouchListener()
                                     // {
                                     //
                                     // @Override
                                     // public boolean onTouch(View v, MotionEvent event)
                                     // {
                                     // return true;
                                     // }
                                     // });
                                     btns[i].setEnabled(false);
                                     btns[i].setActivated(false);
                                     btns[i].setOnClickListener(new View.OnClickListener()
                                     {
                                                     @Override
                                                     public void
onClick(View v)
                                                     {
                                                                     // can actually use a hex value to tell the server what we
                                                                     // are doing since there
                                                                     // are only 16 values!
                                                                     String rtype = "";

                                                                     final int myid = v.getId();

                                                                     if (myid == currentrid)
                                                                     {
                                                                                     // don't do anything....
                                                                                     return;
                                                                     }

                                                                     if (v.isActivated())
                                                                     {
                                                                                     // ie we are touching a button that is already switched on...return as well....
                                                                                     // no point doing this one...
                                                                                     return;
                                                                     }

                                                                     runOnUiThread(new Runnable()
                                                                     {

                                                                                     @Override
                                                                                     public void
run()
                                                                                     {
                                                                                                     // TODO Auto-generated method stub
                                                                                                     for (int kk = 0; kk < 4; kk++)
                                                                                                     {
                                                                                                                     btns[kk].setActivated(false);
                                                                                                                     // btns[kk].setEnabled(false);
                                                                                                     }
                                                                                                     // btns[myid].setEnabled(true);
                                                                                                     btns[myid].setActivated(true);
                                                                                     }
                                                                     })
;

                                                                     if (v.getId() < 10)
                                                                     {
                                                                                     rtype = Integer.toString(v.getId());
                                                                     }
                                                                     else
                                                                     {
                                                                                     if (v.getId() == 10)
                                                                                     {
                                                                                                     rtype = "A";
                                                                                     }
                                                                                     if (v.getId() == 11)
                                                                                     {
                                                                                                     rtype = "B";
                                                                                     }
                                                                                     if (v.getId() == 12)
                                                                                     {
                                                                                                     rtype = "C";
                                                                                     }
                                                                                     if (v.getId() == 13)
                                                                                     {
                                                                                                     rtype = "D";
                                                                                     }
                                                                                     if (v.getId() == 14)
                                                                                     {
                                                                                                     rtype = "E";
                                                                                     }
                                                                                     if (v.getId() == 15)
                                                                                     {
                                                                                                     rtype = "F";
                                                                                     }
                                                                     }
                                                                     if (!rtype.equals(""))
                                                                     {
                                                                                     // update research settings......
                                                                                     updateresearchsettings2(rtype);
                                                                     }
                                                     }
                                     });
                     }

                     setreceiver();

                     currentresearch = getprefs("gameresearchiddata_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid), "none");
                     if (!currentresearch.equals("none"))
                     {
                                     currentrid = Integer.decode(currentresearch);
                                     // highlight the relevant one.....
                                     if (currentrid >= 0 && currentrid < 4)// 16)
                                     {
                                                     btns[currentrid].setEnabled(true);
                                                     btns[currentrid].setActivated(true);
                                     }
                     }
                     requestlist();
     }

     /*
         * @SuppressLint("InlinedApi") private void updateresearchsettings(int researchid, int researchmode) { Intent serviceintent = new Intent(ResearchActivity.this, UploadService.class); if
         * (Build.VERSION.SDK_INT >= 12) { serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); } serviceintent.setAction("com.lloydm.geosword.common.UploadService.research.update");
         * serviceintent.putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid); serviceintent.putExtra("com.lloydm.geosword.common.UploadService.researchid", researchid);
         * serviceintent.putExtra("com.lloydm.geosword.common.UploadService.researchmode", researchmode); serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);
         * startService(serviceintent); }
         *
/
     @SuppressLint("InlinedApi")
     private void updateresearchsettings2(String rtype)
     {
                     Intent serviceintent = new Intent(ResearchActivity.this, UploadService.class);
                     if (Build.VERSION.SDK_INT >= 12)
                     {
                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                     }
                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.research.update2");

                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.researchid", rtype);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);
                     startService(serviceintent);
     }

     @SuppressLint("InlinedApi")
     private void requestlist()
     {
                     /*
                         * atechs.removeAllViews(); rtechs.removeAllViews(); btnatechs.clear(); btnrtechs.clear();
                         *
/
                     if (waiting)
                     {
                                     newtoast("Please wait....trying to talk to database");
                                     return;
                     }
                     waiting = true;

                     Intent serviceintent = new Intent(ResearchActivity.this, UploadService.class);
                     if (Build.VERSION.SDK_INT >= 12)
                     {
                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                     }
                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.research.load2");

                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);
                     startService(serviceintent);
                     // newtoast("Contacting database");

     }

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

     @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...
                                                     onChangeActivity(1);
                                     }
                     }
     }

     @Override
     public void onChangeActivity(int id)
     {
                     super.onChangeActivity(id);
                     if (id == 1)
                     {
                                     goingback = false;
                                     startActivity(new Intent(this, InGameFrontMenuActivity.class).putExtra("com.lloydm.geosword.common.UploadService.gameid", gameid).putExtra("com.lloydm.geosword.common.UploadService.playerid", playerid).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid));
                                     finish();
                     }
     }

     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.ResearchActivity.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)
                                                                     {
                                                                                     researchinfo = intent.getStringExtra("com.lloydm.geosword.activities.ResearchActivity.researchinfo");
                                                                                     // new version...much simpler.......
                                                                                     final String rtype = researchinfo.split(":")[0];
                                                                                     try
                                                                                     {
                                                                                                     final int mobval = Integer.decode(researchinfo.split(":")[1]);
                                                                                                     final int offval = Integer.decode(researchinfo.split(":")[2]);
                                                                                                     final int defval = Integer.decode(researchinfo.split(":")[3]);
                                                                                                     final int ecoval = Integer.decode(researchinfo.split(":")[4]);
                                                                                                     runOnUiThread(new Runnable()
                                                                                                     {
                                                                                                                     @Override
                                                                                                                     public void
run()
                                                                                                                     {
                                                                                                                                     // sk[0].setProgress(mobval);
                                                                                                                                     // sk[1].setProgress(offval);
                                                                                                                                     // sk[2].setProgress(defval);
                                                                                                                                     // sk[3].setProgress(ecoval);
                                                                                                                                     Log.i(TAG, "mobval:" + mobval + " offval:" + offval + " defval:" + defval + " ecoval:" + ecoval);

                                                                                                                                     // now we need to update the % in the tx as well....
                                                                                                                                     Integer vv = (int) (mobval * 0.35f);
                                                                                                                                     String rr = Integer.toString(vv);
                                                                                                                                     tx[0].setText("+" + rr + "%");

                                                                                                                                     vv = (int) (offval * 2.5f);
                                                                                                                                     rr = Integer.toString(vv);
                                                                                                                                     tx[1].setText("+" + rr + "%");

                                                                                                                                     vv = (int) (defval * 3.25f);
                                                                                                                                     rr = Integer.toString(vv);
                                                                                                                                     tx[2].setText("+" + rr + "%");

                                                                                                                                     vv = (int) (ecoval * 0.4f);
                                                                                                                                     rr = Integer.toString(vv);
                                                                                                                                     tx[3].setText("+" + rr + "%");

                                                                                                                     }
                                                                                                     });
                                                                                     }
                                                                                     catch (Exception e)
                                                                                     {
                                                                                                     Log.e(TAG, "error in result from web re research values");
                                                                                     }
                                                                                     final int id = Integer.decode(rtype);
                                                                                     if (id != -1)
                                                                                     {
                                                                                                     runOnUiThread(new Runnable()
                                                                                                     {
                                                                                                                     @Override
                                                                                                                     public void
run()
                                                                                                                     {
                                                                                                                                     for (int i = 0; i < 4; i++)// 16; i++)
                                                                                                                                     {
                                                                                                                                                     btns[i].setEnabled(true);
                                                                                                                                                     if (i != id)
                                                                                                                                                     {
                                                                                                                                                                     btns[i].setActivated(false);
                                                                                                                                                     }

                                                                                                                                     }
                                                                                                                                     btns[id].setActivated(true);// .setEnabled(false);
                                                                                                                                     // btns[id].setEnabled(false);
                                                                                                                                     currentrid = id;
                                                                                                                                     saveprefs("gameresearchiddata_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid), Integer.toString(id));

                                                                                                                     }
                                                                                                     });
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     runOnUiThread(new Runnable()
                                                                                                     {

                                                                                                                     @Override
                                                                                                                     public void
run()
                                                                                                                     {
                                                                                                                                     for (int i = 0; i < 4; i++)
                                                                                                                                     {
                                                                                                                                                     btns[i].setEnabled(true);
                                                                                                                                                     btns[i].setActivated(false);
                                                                                                                                     }

                                                                                                                     }
                                                                                                     })
;

                                                                                     }
                                                                                     /*
                                                                                         * final String[] rarray = researchinfo.split(":"); runOnUiThread(new Runnable(){
                                                                                         *
                                                                                         * @Override public void run() {
                                                                                         *
                                                                                         * if(rarray!=null) { int size = rarray.length; for(int i=0;i<size;i++) { String[] fields = rarray[i].split("\\|"); if(fields!=null) { if(fields.length>=3) {
                                                                                         * if(!fields[3].equals("N")) { rtechdesc.add(fields[9]); rtechindex.add(Integer.decode(fields[0]));
                                                                                         *
                                                                                         *
                                                                                         * Button btn = new Button(ResearchActivity.this); btn.setText(fields[8]); btn.setTextColor(Color.rgb(224, 224, 224)); btn.setBackgroundResource(R.drawable.menu_button);
                                                                                         * btn.setId(rtechdesc.size()-1); btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                                                                                         *
                                                                                         * @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus) { try { String dtxt = (String) rtechdesc.get(v.getId()); rdesc.setText(dtxt);
                                                                                         * //pbar.setProgress(100); //pbartxt.setText("....Completed Research...."); } catch(Exception e) {
                                                                                         *
                                                                                         * } } } })
; btn.setOnClickListener(new View.OnClickListener() {
                                                                                         *
                                                                                         * @Override public void onClick(View v) { try { String dtxt = (String) rtechdesc.get(v.getId()); rdesc.setText(dtxt); //pbar.setProgress(100);
                                                                                         * //pbartxt.setText("....Completed Research...."); } catch(Exception e) {
                                                                                         *
                                                                                         * } } })
; rtechs.addView(btn); btnrtechs.add(btn); } else { atechdesc.add(fields[9]); atechindex.add(Integer.decode(fields[0]));
                                                                                         *
                                                                                         * Button btn = new Button(ResearchActivity.this); btn.setText(fields[8]); btn.setTextColor(Color.rgb(224, 224, 224)); btn.setBackgroundResource(R.drawable.menu_button);
                                                                                         * btn.setId(atechdesc.size()-1); boolean started = false; if(Integer.decode(fields[5])>0) { started = true; } if(started) { float pct = 1f; try { pct = 100.0f
                                                                                         * *((float)(Integer.decode(fields[12])-Integer.decode (fields [5])))/((float)(1+Integer.decode(fields[6])-Integer .decode(fields[5]))); if(pct<1) { pct=1; } } catch(Exception
                                                                                         * e)
{ Log.e(TAG,"Error in research info result"); //error!?! } pbar.setProgress((int)pct); pbartxt.setText("....Researching...."); btn.setTypeface(Typeface.DEFAULT,
                                                                                         * Typeface.ITALIC)
; btn.setTextColor(Color.RED); } else { btn.setTextColor(Color.WHITE); btn.setTypeface(Typeface.DEFAULT, Typeface.NORMAL); } //final boolean canstartresearch
                                                                                         * = !started; final int geindex = Integer.decode(fields[0]); btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                                                                                         *
                                                                                         * @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus) { try { String dtxt = (String) atechdesc.get(v.getId()); rdesc.setText(dtxt); } catch(Exception
                                                                                         * e)
{
                                                                                         *
                                                                                         * }
                                                                                         *
                                                                                         * } } })
; btn.setOnClickListener(new View.OnClickListener() {
                                                                                         *
                                                                                         * @Override public void onClick(View v) { try { String dtxt = (String) atechdesc.get(v.getId()); rdesc.setText(dtxt); } catch(Exception e) {
                                                                                         *
                                                                                         * } } })
; btn.setOnLongClickListener(new View.OnLongClickListener() {
                                                                                         *
                                                                                         * @Override public boolean onLongClick(View v) { //cancel researching this tech.... try { String dtxt = (String) atechdesc.get(v.getId()); rdesc.setText(dtxt); }
                                                                                         * catch(Exception e) {
                                                                                         *
                                                                                         * } int flag = 0; //research. if(((Button)v).getTextColors ().getDefaultColor()==Color.RED) { flag = 1; } updateresearchsettings(geindex,flag);
                                                                                         *
                                                                                         * return true; } })
; atechs.addView(btn); btnatechs.add(btn); } } } } } }})
;
                                                                                         */
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     int reason = intent.getIntExtra("com.lloydm.geosword.activities.ResearchActivity.authenticationerror", -1);
                                                                                     if (reason == UploadService.LOGIN_NOT_FOUND)
                                                                                     {
                                                                                                     // show the authdialog.....
                                                                                                     login();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     // newtoast("Something went wrong during the upload process.");
                                                                                                     Log.i(TAG, "error in research talking to database - probably user too quick");
                                                                                     }
                                                                     }
                                                                     // reset waiting flag.....
                                                     }
                                     };
                                     registerReceiver(loadreceiver, new IntentFilter("com.lloydm.geosword.activities.ResearchActivity.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.ResearchActivity.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)
                                                                     {
                                                                                     final int id = intent.getIntExtra("com.lloydm.geosword.activities.ResearchActivity.researchid", -1);
                                                                                     if (id != -1)
                                                                                     {
                                                                                                     if (id != currentrid)
                                                                                                     {
                                                                                                                     currentrid = id;
                                                                                                                     runOnUiThread(new Runnable()
                                                                                                                     {
                                                                                                                                     @Override
                                                                                                                                     public void
run()
                                                                                                                                     {
                                                                                                                                                     btns[id].setActivated(true);

                                                                                                                                                     saveprefs("gameresearchiddata_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid), Integer.toString(id));
                                                                                                                                                     for (int i = 0; i < 4; i++)// 16; i++)
                                                                                                                                                     {
                                                                                                                                                                     if (i != id)
                                                                                                                                                                     {
                                                                                                                                                                                     btns[i].setActivated(false);
                                                                                                                                                                     }
                                                                                                                                                     }

                                                                                                                                     }
                                                                                                                     })
;
                                                                                                     }
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     // no change.....should never happen...
                                                                                     }
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     /*
                                                                                         * boolean valid = intent.getBooleanExtra( "com.lloydm.geosword.activities.ResearchActivity.researchupdated" ,false); if(valid) { newtoast("Research Updated"); goingback =
                                                                                         * true; waiting = false; //T - update interface int rid= intent.getIntExtra( "com.lloydm.geosword.activities.ResearchActivity.researchid" ,-1); int mid= intent.getIntExtra(
                                                                                         * "com.lloydm.geosword.activities.ResearchActivity.mode" ,-1)
; if(researchinfo!=null) { final String[] rarray = researchinfo.split(":"); if(rarray!=null) { int rsize =
                                                                                         * rarray.length; for(int i=0;i<rsize;i++) { String[] fields = rarray[i].split("\\|"); if(fields!=null) { if(fields.length>=1) { if(Integer.decode(fields[0])!=rid) { continue;
                                                                                         * } int jj =atechindex.indexOf(rid);//rtechindex.add(Integer.decode (fields[0])); if(mid==0) { //started researching pbar.setProgress(1);
                                                                                         * pbartxt.setText("....Researching...."); ((Button)btnatechs .get(jj)).setTypeface(Typeface.DEFAULT, Typeface.ITALIC); ((Button)btnatechs.get(jj)).setTextColor(Color.RED); }
                                                                                         * else { //canceled research pbar.setProgress(0); pbartxt.setText("...Research Cancelled..."); ((Button) btnatechs.get(jj)).setTextColor(Color.WHITE); ((Button
                                                                                         * )btnatechs.get(jj)).setTypeface(Typeface.DEFAULT, Typeface.NORMAL); } } } } } } } else { newtoast("Research Not Updated"); goingback = true; waiting = false;
                                                                                         *
                                                                                         * //F - do nothing... }
                                                                                         */
                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     int reason = intent.getIntExtra("com.lloydm.geosword.activities.ResearchActivity.authenticationerror", -1);
                                                                                     if (reason == UploadService.LOGIN_NOT_FOUND)
                                                                                     {
                                                                                                     // show the authdialog.....
                                                                                                     login();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     newtoast("Something went wrong during the upload process.");
                                                                                     }
                                                                     }
                                                                     // reset waiting flag.....
                                                                     // either way we want to enable everything again....
                                                                     runOnUiThread(new Runnable()
                                                                     {
                                                                                     @Override
                                                                                     public void
run()
                                                                                     {
                                                                                                     for (int i = 0; i < 4; i++)// 16; i++)
                                                                                                     {
                                                                                                                     btns[i].setEnabled(true);
                                                                                                     }
                                                                                     }
                                                                     })
;

                                                     }
                                     };
                                     registerReceiver(updatereceiver, new IntentFilter("com.lloydm.geosword.activities.ResearchActivity.update"));
                     }
     }
}