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

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 ContinueGameActivity extends StandardGameMenuActivity
{

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

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

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

     private volatile int[] gameslist = null;
     private volatile int[] playerlist = null;
     private volatile int[] teamlist = null;

     private String[] gametitles = null;
     private ArrayAdapter<String> gameadapter = null;

     private UploadCompleteBroadcastReceiver listreceiver = null;

     private Button btncontinue = null;
     private Spinner spngameslist = null;
     private ProgressBar progwheel = null;

     private boolean loginaskedfor = false;

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

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

                     // this may or may not crash....
                     bvidresid = R.raw.contmov;
                     bvideo = (SurfaceView) findViewById(R.id.vidsurfacecontinuegame);

                     setreceiver();

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

                     spngameslist = (Spinner) findViewById(R.id.spnlistofexistingames);
                     spngameslist.setEnabled(false);

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

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

                     btncontinue = (Button) findViewById(R.id.btncontinuegame);
                     btncontinue.setEnabled(false);

                     btncontinue.setOnClickListener(new View.OnClickListener()
                     {
                                     @Override
                                     public void
onClick(View v)
                                     {
                                                     if (gameslist.length <= 0 || playerlist.length <= 0 || teamlist.length <= 0)
                                                     {
                                                                     return;
                                                     }
                                                     try
                                                     {
                                                                     gameid = gameslist[spngameslist.getSelectedItemPosition()];
                                                                     playerid = playerlist[spngameslist.getSelectedItemPosition()];
                                                                     teamid = teamlist[spngameslist.getSelectedItemPosition()];
                                                     }
                                                     catch (Exception e)
                                                     {
                                                     }
                                                     if (gameid != -1 && playerid != -1)
                                                     {
                                                                     onChangeActivity(1);
                                                     }
                                     }
                     });

                     requestlist();
     }

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

                     if (waiting)
                     {
                                     newtoast("Please wait....trying to talk to database");
                                     return;
                     }
                     waiting = true;
                     newtoast("Please wait....retrieving games from server");
                     Intent serviceintent = new Intent(ContinueGameActivity.this, UploadService.class);
                     if (Build.VERSION.SDK_INT >= 12)
                     {
                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                     }
                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.continue.load");

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

     @SuppressLint("InlinedApi")
     private void requestlist(boolean tmp)
     {
                     if (tmp)
                     {
                                     newtoast("Please wait....retrieving games from server");
                                     Intent serviceintent = new Intent(ContinueGameActivity.this, UploadService.class);
                                     if (Build.VERSION.SDK_INT >= 12)
                                     {
                                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                     }
                                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.continue.load");

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

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

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

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

     @Override
     public void onChangeActivity(int id)
     {
                     super.onChangeActivity(id);
                     if (id != 0)
                     {
                                     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();
                     }
                     if (id == 0)
                     {
                                     startActivity(new Intent(this, MainMenuActivity.class));
                     }
     }

     private void
setreceiver()
     {
                     if (listreceiver == null)
                     {
                                     listreceiver = 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.ContinueGameActivity.resultcode", false);
                                                                     if (success)
                                                                     {

                                                                                     // we've got a list....
                                                                                     // and finish this one......
                                                                                     // okay we need to get the information from the data in
                                                                                     // the intent.....
                                                                                     // basically all the data relating to the games we are
                                                                                     // allowed to see.....
                                                                                     final String data = intent.getStringExtra("com.lloydm.geosword.activities.ContinueGameActivity.games");
                                                                                     if (data != null)
                                                                                     {
                                                                                                     if (!data.equals(""))
                                                                                                     {
                                                                                                                     new Thread(new Runnable()
                                                                                                                     {

                                                                                                                                     @Override
                                                                                                                                     public void
run()
                                                                                                                                     {
                                                                                                                                                     String[] games = data.split(":");
                                                                                                                                                     if (games != null)
                                                                                                                                                     {
                                                                                                                                                                     if (games.length > 0)
                                                                                                                                                                     {
                                                                                                                                                                                     gametitles = new String[games.length];
                                                                                                                                                                                     gameslist = new int[games.length];
                                                                                                                                                                                     playerlist = new int[games.length];
                                                                                                                                                                                     teamlist = new int[games.length];
                                                                                                                                                                                     for (int i = 0; i < games.length; i++)
                                                                                                                                                                                     {
                                                                                                                                                                                                     String[] fields = games[i].split("\\|");

                                                                                                                                                                                                     if (fields != null)
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     if (fields.length > 0)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     gametitles[i] = fields[4];
                                                                                                                                                                                                                                     try
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     gameslist[i] = Integer.decode(fields[0]);
                                                                                                                                                                                                                                                     playerlist[i] = Integer.decode(fields[2]);
                                                                                                                                                                                                                                                     teamlist[i] = Integer.decode(fields[1]);
                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                     catch (Exception e)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     // do nothing...
                                                                                                                                                                                                                                     }

                                                                                                                                                                                                                     }
                                                                                                                                                                                                     }
                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                                     if (gametitles != null)
                                                                                                                                                     {
                                                                                                                                                                     if (gametitles.length > 0)
                                                                                                                                                                     {

                                                                                                                                                                                     runOnUiThread(new Runnable()
                                                                                                                                                                                     {

                                                                                                                                                                                                     @Override
                                                                                                                                                                                                     public void
run()
                                                                                                                                                                                                     {
                                                                                                                                                                                                                     progwheel.setVisibility(View.INVISIBLE);
                                                                                                                                                                                                                     if (gameadapter == null)
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     gameadapter = new ArrayAdapter<String>(ContinueGameActivity.this, R.layout.geo_spinner_layout, R.id.geo_spinner_text2, gametitles);

                                                                                                                                                                                                                                     spngameslist.setEnabled(true);
                                                                                                                                                                                                                                     btncontinue.setEnabled(true);

                                                                                                                                                                                                                                     spngameslist.setAdapter(gameadapter);

                                                                                                                                                                                                                     }
                                                                                                                                                                                                                     else
                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                     gameadapter.clear();
                                                                                                                                                                                                                                     for (int i = 0; i < gametitles.length; i++)
                                                                                                                                                                                                                                     {
                                                                                                                                                                                                                                                     gameadapter.add(gametitles[i]);

                                                                                                                                                                                                                                     }
                                                                                                                                                                                                                                     gameadapter.notifyDataSetChanged();
                                                                                                                                                                                                                     }

                                                                                                                                                                                                     }

                                                                                                                                                                                     })
;

                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                                     goingback = true;
                                                                                                                                                     waiting = false;
                                                                                                                                     }
                                                                                                                     }).start();
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     goingback = true;
                                                                                                                     newtoast("No games currently available");
                                                                                                                     runOnUiThread(new Runnable()
                                                                                                                     {

                                                                                                                                     @Override
                                                                                                                                     public void
run()
                                                                                                                                     {
                                                                                                                                                     progwheel.setVisibility(View.INVISIBLE);
                                                                                                                                     }
                                                                                                                     });
                                                                                                     }
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     goingback = true;
                                                                                                     newtoast("No games currently available");
                                                                                                     runOnUiThread(new Runnable()
                                                                                                     {

                                                                                                                     @Override
                                                                                                                     public void
run()
                                                                                                                     {
                                                                                                                                     progwheel.setVisibility(View.INVISIBLE);
                                                                                                                     }
                                                                                                     });
                                                                                     }
                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     goingback = true;
                                                                                     waiting = false;
                                                                                     int reason = intent.getIntExtra("com.lloydm.geosword.activities.ContinueGameActivity.authenticationerror", -1);
                                                                                     if (reason == UploadService.LOGIN_NOT_FOUND)
                                                                                     {
                                                                                                     // show the authdialog.....
                                                                                                     loginaskedfor = true;
                                                                                                     login();
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     newtoast("Something went wrong during the upload process.");

                                                                                     }
                                                                                     runOnUiThread(new Runnable()
                                                                                     {

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

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