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.ProgressBar;
import android.widget.TextView;

import com.lloydm.geosword.J3DMini;
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 ShipActivity extends StandardGameMenuActivity
{
     private final static String TAG = "com.lloydm.geosword.activities.ShipActivity";

     private J3DMini j3dmini = null;

     private int gameid = -1;
     private int playerid = -1;
     private int shipid = -1;
     private int shiptypeid = -1;
     // private int armamentid = -1;
     private int fleetid = -1;
     private int teamid = -1;

     private int fromai = 0;

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

     private String shipinfo = "";
     private String weapinfo = "";

     private String currentshipinfo = "";
     private String currentweapinfo = "";

     private TextView txtshiptype = null;
     private TextView txthull = null;
     private TextView txtvel = null;
     private TextView txtaccel = null;
     private TextView txtfire = null;

     private TextView txtshiptext = null;

     private ProgressBar progwheel = null;
     private String[] defstrings = null;

     private int mcvel = 0;
     private int mcaccel = 0;
     private int mcfire = 0;
     private int mcfire2 = 0;

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

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

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

                     int meshid = 0;
                     if (shiptypeid == 2)
                     {
                                     meshid = 1;
                     }
                     if (shiptypeid == 3)
                     {
                                     meshid = 2;
                     }
                     if (shiptypeid == 4)
                     {
                                     meshid = 3;
                     }
                     if (teamid == 2)
                     {
                                     meshid += 4;
                     }
                     if (teamid == 3)
                     {
                                     meshid += 8;
                     }
                     if (teamid == 4)
                     {
                                     meshid += 12;
                     }
                     if (teamid == 5)
                     {
                                     meshid += 16;
                     }

                     j3dmini = new J3DMini(assetmgr, meshid);
                     j3dmini.init3D(this, 1);
                     try
                     {
                                     if (j3dmini.glview != null)
                                     {
                                                     j3dmini.glview.setZOrderOnTop(true);
                                     }
                     }
                     catch (Exception ee)
                     {
                                     Log.i(TAG, "Error z ordering 3d");
                     }

     }

     @Override
     public void setup()
     {
                     setContentView(R.layout.activity_shipdetail);
                     mainview = (View) findViewById(R.layout.activity_shipdetail);
                     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);
                     shipid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.shipid", -1);
                     shiptypeid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.shiptypeid", -1);
                     fleetid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.fleetid", -1);
                     teamid = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.teamid", -1);
                     currentshipinfo = getprefs("gamedata_shipactinfo_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid) + "_" + Integer.toString(fleetid) + "_" + Integer.toString(shipid) + "_" + Integer.toString(shiptypeid), "none");
                     currentweapinfo = getprefs("gamedata_weapactinfo_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid) + "_" + Integer.toString(fleetid) + "_" + Integer.toString(shipid) + "_" + Integer.toString(shiptypeid), "none");
                     fromai = getIntent().getIntExtra("com.lloydm.geosword.common.UploadService.fromai", 0);


                     txtshiptype = (TextView) findViewById(R.id.txtshiptype);
                     txthull = (TextView) findViewById(R.id.txthull);

                     txtvel = (TextView) findViewById(R.id.txtmaxvel);
                     txtaccel = (TextView) findViewById(R.id.txtmaxaccel);
                     txtfire = (TextView) findViewById(R.id.txttotfire);

                     txtshiptext = (TextView) findViewById(R.id.txtshiptext);

                     if (teamid == 1)
                     {
                                     if (shiptypeid == 1)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd11));
                                     }
                                     if (shiptypeid == 2)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd12));
                                     }
                                     if (shiptypeid == 3)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd13));
                                     }
                                     if (shiptypeid == 4)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd14));
                                     }
                     }
                     if (teamid == 2)
                     {
                                     if (shiptypeid == 1)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd21));
                                     }
                                     if (shiptypeid == 2)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd22));
                                     }
                                     if (shiptypeid == 3)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd23));
                                     }
                                     if (shiptypeid == 4)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd24));
                                     }

                     }
                     if (teamid == 3)
                     {
                                     if (shiptypeid == 1)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd31));
                                     }
                                     if (shiptypeid == 2)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd32));
                                     }
                                     if (shiptypeid == 3)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd33));
                                     }
                                     if (shiptypeid == 4)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd34));
                                     }

                     }
                     if (teamid == 4)
                     {
                                     if (shiptypeid == 1)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd41));
                                     }
                                     if (shiptypeid == 2)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd42));
                                     }
                                     if (shiptypeid == 3)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd43));
                                     }
                                     if (shiptypeid == 4)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd44));
                                     }

                     }
                     if (teamid == 5)
                     {
                                     if (shiptypeid == 1)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd51));
                                     }
                                     if (shiptypeid == 2)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd52));
                                     }
                                     if (shiptypeid == 3)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd53));
                                     }
                                     if (shiptypeid == 4)
                                     {
                                                     txtshiptext.setText(getString(R.string.sd54));
                                     }
                     }
                     txtshiptype.setTypeface(font);
                     txthull.setTypeface(font);
                     txtvel.setTypeface(font);
                     txtaccel.setTypeface(font);
                     txtfire.setTypeface(font);

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

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

                     defstrings = new String[8];
                     defstrings[0] = txtshiptype.getText().toString();
                     defstrings[1] = txthull.getText().toString();
                     defstrings[2] = txtvel.getText().toString();
                     defstrings[3] = txtaccel.getText().toString();
                     defstrings[4] = txtfire.getText().toString();

                     progwheel = (ProgressBar) findViewById(R.id.progressbar5);
                     if (!worldcampaign)
                     {
                                     progwheel.setVisibility(View.VISIBLE);
                     }
                     else
                     {
                                     progwheel.setVisibility(View.INVISIBLE);
                     }

                     try
                     {
                                     j3dmini.glview.setAlpha(1.0f);
                     }
                     catch (Exception e)
                     {
                                     Log.e(TAG, "error setting glview alpha");

                     }

                     if (!worldcampaign)
                     {
                                     setreceiver();
                     }

                     if (!currentshipinfo.equals("none") && !currentweapinfo.equals("none") && !worldcampaign)
                     {
                                     progwheel.setVisibility(View.INVISIBLE);
                                     final String sinfo = currentshipinfo;
                                     final String winfo = currentweapinfo;
                                     mcvel = 0;
                                     mcfire = 0;
                                     mcaccel = 0;
                                     String buildprog = sinfo.substring(sinfo.indexOf("progress") + 9, sinfo.indexOf("aigroupid") - 1);
                                     String hullstr = sinfo.substring(sinfo.indexOf("defaulthp=") + 10, sinfo.indexOf("defaultbuildtime") - 1);
                                     String shipname = sinfo.substring(sinfo.indexOf("name") + 5, sinfo.indexOf("sizetype") - 1);
                                     String maxvel = sinfo.substring(sinfo.indexOf("maxvelocity") + 12, sinfo.indexOf("minvelocity") - 1);
                                     String maxaccel = sinfo.substring(sinfo.indexOf("maxacceleration") + 16, sinfo.indexOf("minacceleration") - 1);
                                     if (buildprog.contains("."))
                                     {
                                                     try
                                                     {
                                                                     buildprog = buildprog.substring(0, buildprog.indexOf(".") - 1);
                                                     }
                                                     catch (Exception ff)
                                                     {
                                                     }
                                     }
                                     float fullhp = Float.parseFloat(hullstr);
                                     hullstr = Integer.toString((int) fullhp);

                                     try
                                     {
                                                     Log.i(TAG, "maxvel:" + maxvel);
                                                     mcvel = (int) (25f * Float.parseFloat(maxvel));
                                                     maxvel = Integer.toString(mcvel);
                                                     Log.i(TAG, "calced maxvel:" + maxvel);
                                     }
                                     catch (Exception ff)
                                     {
                                                     Log.e(TAG, "error reading maxvel");
                                     }
                                     try
                                     {
                                                     Log.i(TAG, "maxaccel:" + maxaccel);
                                                     mcaccel = (int) (1000f * Float.parseFloat(maxaccel));
                                                     maxaccel = Integer.toString(mcaccel);
                                                     Log.i(TAG, "calced maxaccel:" + maxaccel);
                                     }
                                     catch (Exception ff)
                                     {
                                                     Log.e(TAG, "error reading maxaccel");

                                     }

                                     txthull.setText(defstrings[1] + hullstr);// chp + "/" + hullstr);
                                     txtshiptype.setText(shipname);// defstrings[0] + shipname);
                                     txtvel.setText(defstrings[2] + maxvel);
                                     txtaccel.setText(defstrings[3] + maxaccel);

                                     String[] weapons = winfo.split(":");
                                     if (weapons != null)
                                     {
                                                     mcfire = 0;
                                                     for (int i = 0; i < weapons.length; i++)
                                                     {
                                                                     String[] fields = weapons[i].split("\\|");

                                                                     if (fields != null)
                                                                     {
                                                                                     if (fields.length >= 16)
                                                                                     {

                                                                                                     try
                                                                                                     {
                                                                                                                     float firepower = 0;
                                                                                                                     float dg = Float.parseFloat(fields[12]);
                                                                                                                     float pd = Float.parseFloat(fields[15]);
                                                                                                                     float cd = Float.parseFloat(fields[13]);
                                                                                                                     Log.i(TAG, "dg:" + dg + " pd:" + pd + " cd:" + cd);
                                                                                                                     if (pd <= 0)
                                                                                                                     {
                                                                                                                                     pd = 1;
                                                                                                                     }
                                                                                                                     if (cd <= 0)
                                                                                                                     {
                                                                                                                                     cd = 1;
                                                                                                                     }
                                                                                                                     if (dg <= 0)
                                                                                                                     {
                                                                                                                                     dg = 1;
                                                                                                                     }
                                                                                                                     float nums = 10f / (pd + cd); // expect a value between 500 max and 50 min (possibly)
                                                                                                                     firepower = dg * nums;
                                                                                                                     mcfire += firepower;
                                                                                                                     Log.i(TAG, "mcfire:" + mcfire);
                                                                                                     }
                                                                                                     catch (Exception fgfgh)
                                                                                                     {
                                                                                                                     Log.w(TAG, "could not parse weapon info");
                                                                                                     }
                                                                                     }
                                                                     }
                                                     }
                                                     if (mcfire2 == 0)
                                                     {
                                                                     txtfire.setText(defstrings[4] + mcfire);
                                                     }
                                     }
                     }
                     if (!worldcampaign)
                     {
                                     requestlist();
                     }
     }

     /*
         * @SuppressLint("InlinedApi") private void updateshipsettings() { Intent serviceintent = new Intent(ShipActivity.this, UploadService.class); if (Build.VERSION.SDK_INT >= 12) {
         * serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); } serviceintent.setAction("com.lloydm.geosword.common.UploadService.shipdetails.update");
         * serviceintent.putExtra("com.lloydm.geosword.common.UploadService.shipid", shipid); serviceintent.putExtra("com.lloydm.geosword.common.UploadService.shiptypeid", shiptypeid);
         * serviceintent.putExtra("com.lloydm.geosword.common.UploadService.armamentid", armamentid); serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);
         * startService(serviceintent); }
         *
/
     @SuppressLint("InlinedApi")
     private void requestlist()
     {
                     if (worldcampaign)
                     {
                                     return;
                     }
                     if (waiting)
                     {
                                     newtoast("Please wait....trying to talk to database");
                                     return;
                     }
                     waiting = true;
                     Intent serviceintent = new Intent(ShipActivity.this, UploadService.class);
                     if (Build.VERSION.SDK_INT >= 12)
                     {
                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                     }
                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.shipdetails.load");

                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.shipid", shipid);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.shiptypeid", shiptypeid);
                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.uniqueid", uid);
                     startService(serviceintent);
     }

     @Override
     protected void onResume()
     {
                     super.onResume();
                     if (j3dmini != null)
                     {
                                     j3dmini.Resume();
                     }
                     if (!worldcampaign)
                     {
                                     setreceiver();
                                     if (isloggedin() && loginstatuschanged())
                                     {
                                                     requestlist();
                                     }
                     }
                     setuphelp(R.string.htt_shipdetail, R.string.htxt_shipdetail, R.string.htt_shipdetail);
     }

     @Override
     protected void onPause()
     {
                     super.onPause();
                     if (j3dmini != null)
                     {
                                     j3dmini.Pause();
                     }
                     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;
                                     if (fromai == 0)
                                     {
                                                     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));
                                     }
                                     else
                                     {
                                                     startActivity(new Intent(this, ModifyAIActivity.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.fleetid", fleetid).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid).putExtra("com.lloydm.geosword.common.UploadService.shiptypeid", shiptypeid));
                                     }
                                     finish();
                     }
                     if (id == 11)
                     {
                                     goingback = false;
                                     startActivity(new Intent(this, InspectFleetActivity.class).putExtra("com.lloydm.geosword.common.UploadService.teamid", teamid).putExtra("com.lloydm.geosword.activities.PreGameLobbyActivity.worldcampaign", true));

                                     finish();
                     }
     }

     private void
setreceiver()
     {
                     if (loadreceiver == null && worldcampaign == false)
                     {
                                     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.ShipActivity.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)
                                                                     {
                                                                                     runOnUiThread(new Runnable()
                                                                                     {

                                                                                                     @Override
                                                                                                     public void
run()
                                                                                                     {
                                                                                                                     progwheel.setVisibility(View.INVISIBLE);
                                                                                                     }
                                                                                     });

                                                                                     shipinfo = intent.getStringExtra("com.lloydm.geosword.activities.ShipActivity.shipinfo");
                                                                                     weapinfo = intent.getStringExtra("com.lloydm.geosword.activities.ShipActivity.weaponinfo");
                                                                                     final String sinfo = shipinfo;
                                                                                     final String winfo = weapinfo;

                                                                                     if (sinfo.equals(currentshipinfo) && winfo.equals(currentweapinfo))
                                                                                     {
                                                                                                     // don't need to do anything
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     // when we implement modules this will have to
                                                                                                     // change.... but for now okay...
                                                                                                     // if either current is null then we need to rebuild
                                                                                                     // the lot from scratch....
                                                                                                     // otherwise we just update the values.....
                                                                                                     runOnUiThread(new Runnable()
                                                                                                     {
                                                                                                                     @Override
                                                                                                                     public void
run()
                                                                                                                     {
                                                                                                                                     try
                                                                                                                                     {
                                                                                                                                                     j3dmini.glview.setAlpha(1.0f);
                                                                                                                                     }
                                                                                                                                     catch (Exception e)
                                                                                                                                     {
                                                                                                                                                     Log.e(TAG, "error setting glview alpha");
                                                                                                                                     }
                                                                                                                                     String buildprog = sinfo.substring(sinfo.indexOf("progress") + 9, sinfo.indexOf("aigroupid") - 1);
                                                                                                                                     // String chp = sinfo.substring(sinfo.indexOf("hp=") + 3, sinfo.indexOf("progress") - 1);
                                                                                                                                     String hullstr = sinfo.substring(sinfo.indexOf("defaulthp=") + 10, sinfo.indexOf("defaultbuildtime") - 1);
                                                                                                                                     String shipname = sinfo.substring(sinfo.indexOf("name") + 5, sinfo.indexOf("sizetype") - 1);
                                                                                                                                     String maxvel = sinfo.substring(sinfo.indexOf("maxvelocity") + 12, sinfo.indexOf("minvelocity") - 1);
                                                                                                                                     String maxaccel = sinfo.substring(sinfo.indexOf("maxacceleration") + 16, sinfo.indexOf("minacceleration") - 1);
                                                                                                                                     // String cputime = sinfo.substring(sinfo.indexOf("defaultcputime") + 15, sinfo.length() - 1);
                                                                                                                                     if (buildprog.contains("."))
                                                                                                                                     {
                                                                                                                                                     try
                                                                                                                                                     {
                                                                                                                                                                     buildprog = buildprog.substring(0, buildprog.indexOf(".") - 1);
                                                                                                                                                     }
                                                                                                                                                     catch (Exception ff)
                                                                                                                                                     {
                                                                                                                                                                     // buildprog = "Unknown";
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                                     mcvel = 0;
                                                                                                                                     mcaccel = 0;
                                                                                                                                     float fullhp = Float.parseFloat(hullstr);
                                                                                                                                     hullstr = Integer.toString((int) fullhp);
                                                                                                                                     try
                                                                                                                                     {
                                                                                                                                                     Log.i(TAG, "maxvel:" + maxvel);
                                                                                                                                                     mcvel = (int) (25f * Float.parseFloat(maxvel));
                                                                                                                                                     maxvel = Integer.toString(mcvel);
                                                                                                                                                     Log.i(TAG, "calced maxvel:" + maxvel);
                                                                                                                                     }
                                                                                                                                     catch (Exception ff)
                                                                                                                                     {
                                                                                                                                                     Log.e(TAG, "error reading maxvel");
                                                                                                                                     }
                                                                                                                                     try
                                                                                                                                     {
                                                                                                                                                     Log.i(TAG, "maxaccel:" + maxaccel);
                                                                                                                                                     mcaccel = (int) (1000f * Float.parseFloat(maxaccel));
                                                                                                                                                     maxaccel = Integer.toString(mcaccel);
                                                                                                                                                     Log.i(TAG, "calced maxaccel:" + maxaccel);

                                                                                                                                     }
                                                                                                                                     catch (Exception ff)
                                                                                                                                     {
                                                                                                                                                     Log.e(TAG, "error getting maxaccel");

                                                                                                                                     }

                                                                                                                                     txthull.setText(defstrings[1] + hullstr);// + chp + "/" + hullstr);
                                                                                                                                     txtshiptype.setText(shipname);// defstrings[0] + shipname);
                                                                                                                                     txtvel.setText(defstrings[2] + maxvel);
                                                                                                                                     txtaccel.setText(defstrings[3] + maxaccel);
                                                                                                                                     String[] weapons = winfo.split(":");
                                                                                                                                     if (weapons != null)
                                                                                                                                     {
                                                                                                                                                     mcfire2 = 0;
                                                                                                                                                     for (int i = 0; i < weapons.length; i++)
                                                                                                                                                     {
                                                                                                                                                                     String[] fields = weapons[i].split("\\|");

                                                                                                                                                                     if (fields != null)
                                                                                                                                                                     {
                                                                                                                                                                                     if (fields.length >= 16)
                                                                                                                                                                                     {

                                                                                                                                                                                                     try
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     float firepower = 0;
                                                                                                                                                                                                                     float dg = Float.parseFloat(fields[12]);
                                                                                                                                                                                                                     float pd = Float.parseFloat(fields[15]);
                                                                                                                                                                                                                     float cd = Float.parseFloat(fields[13]);
                                                                                                                                                                                                                     Log.i(TAG, "dg:" + dg + " pd:" + pd + " cd:" + cd);
                                                                                                                                                                                                                     if (pd <= 0)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     pd = 1;
                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     if (cd <= 0)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     cd = 1;
                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     if (dg <= 0)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     dg = 1;
                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     float nums = 10f / (pd + cd); // expect a value between 500 max and 50 min (possibly)
                                                                                                                                                                                                                     firepower = dg * nums;
                                                                                                                                                                                                                     mcfire2 += firepower;
                                                                                                                                                                                                                     Log.i(TAG, "mcfire2:" + mcfire2);

                                                                                                                                                                                                     }
                                                                                                                                                                                                     catch (Exception fgfgh)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     Log.w(TAG, "could not parse weapon info");
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                                     txtfire.setText(defstrings[4] + mcfire2);
                                                                                                                                     }
                                                                                                                                     saveprefs("gamedata_shipactinfo_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid) + "_" + Integer.toString(fleetid) + "_" + Integer.toString(shipid) + "_" + Integer.toString(shiptypeid), sinfo);
                                                                                                                                     saveprefs("gamedata_weapactinfo_" + Integer.toString(gameid) + "_" + Integer.toString(playerid) + "_" + Integer.toString(teamid) + "_" + Integer.toString(fleetid) + "_" + Integer.toString(shipid) + "_" + Integer.toString(shiptypeid), winfo);
                                                                                                                     }
                                                                                                     })
;
                                                                                     }
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     if (tuid == uid)
                                                                                     {
                                                                                                     runOnUiThread(new Runnable()
                                                                                                     {

                                                                                                                     @Override
                                                                                                                     public void
run()
                                                                                                                     {
                                                                                                                                     progwheel.setVisibility(View.INVISIBLE);
                                                                                                                     }
                                                                                                     })
;
                                                                                     }
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     int reason
=
intent.getIntExtra("com.lloydm.geosword.activities.ShipActivity.authenticationerror", -1);
                                                                                     if (reason == UploadService.LOGIN_NOT_FOUND)
                                                                                     {
                                                                                                     // show the authdialog.....
                                                                                                     login();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     if (!worldcampaign)
                                                                                                     {
                                                                                                                     newtoast("Something went wrong during the upload process.");
                                                                                                     }
                                                                                     }
                                                                     }
                                                                     // reset waiting flag.....
                                                     }
                                     };
                                     registerReceiver(loadreceiver, new IntentFilter("com.lloydm.geosword.activities.ShipActivity.load"));
                     }
                     if (updatereceiver == null && worldcampaign == false) // doesn't happen yet.....
                     {
                                     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.ShipActivity.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)
                                                                     {
                                                                                     // newtoast("Yep....all good");
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     int reason = intent.getIntExtra("com.lloydm.geosword.activities.ShipActivity.authenticationerror", -1);
                                                                                     if (reason == UploadService.LOGIN_NOT_FOUND)
                                                                                     {
                                                                                                     // show the authdialog.....
                                                                                                     login();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     if (!worldcampaign)
                                                                                                     {
                                                                                                                     newtoast("Something went wrong during the upload process.");
                                                                                                     }

                                                                                     }
                                                                     }
                                                                     // reset waiting flag.....
                                                     }
                                     };
                                     registerReceiver(updatereceiver, new IntentFilter("com.lloydm.geosword.activities.ShipActivity.update"));
                     }
     }

}