package com.lloydm.geosword.activities;

import java.util.Locale;

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

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 JoinOnlineGameActivity extends StandardGameMenuActivity
{
     // /Note that upon "joining" in this section the user has only 'reserved' a
     // spot for 1 minute...they haven't actually
     // joined the war. But once they hit "enter" in the lobby itself then yes -
     // they are fully committed.
     // This will help with managing people quitting the lobby etc if they don't
     // like the look of the situation.

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

     private int gameid = -1;
     private volatile boolean goingback = true;
     private volatile boolean waiting = false;

     private UploadCompleteBroadcastReceiver joinreceiver = null;

     private Button btnjoin = null;
     private EditText txtpass = null;
     private EditText txtgamename = null;
     private String[] teaminfo = null;

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

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

                     bvideolooped = true;
                     bvidresid = R.raw.grnmov;
                     bvideo = (SurfaceView) findViewById(R.id.surfacevidjoingame);

                     setreceiver();

                     txtpass = (EditText) findViewById(R.id.txtpassword);
                     txtgamename = (EditText) findViewById(R.id.txtgamename);
                     txtpass.setTypeface(font);
                     txtgamename.setTypeface(font);

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

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

                     btnjoin = (Button) findViewById(R.id.btn_join);
                     btnjoin.setOnClickListener(new View.OnClickListener()
                     {

                                     @SuppressLint("InlinedApi")

                                     @Override
                                     public void onClick(View v)
                                     {
                                                     // send a broadcast to refresh the list......

                                                     if (txtpass.getText().toString().equals("") || txtgamename.getText().toString().equals(""))
                                                     {
                                                                     newtoast("Please enter a name and password for the game");
                                                                     return;
                                                     }
                                                     if (waiting)
                                                     {
                                                                     newtoast("Please wait....trying to talk to database");
                                                                     return;
                                                     }
                                                     waiting = true;
                                                     // allowedtosort = false;
                                                     Intent serviceintent = new Intent(JoinOnlineGameActivity.this, UploadService.class);
                                                     if (Build.VERSION.SDK_INT >= 12)
                                                     {
                                                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                                     }
                                                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.joingame");
                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.gamename", txtgamename.getText().toString());
                                                     serviceintent.putExtra("com.lloydm.geosword.common.UploadService.gamepassword", txtpass.getText().toString().toUpperCase(Locale.ENGLISH));
                                                     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....
                     setuphelp(R.string.htt_join, R.string.htxt_join, R.string.htt_join);
     }

     @Override
     protected void onPause()
     {
                     super.onPause();
                     if (joinreceiver != null)
                     {
                                     unregisterReceiver(joinreceiver);
                     }
                     joinreceiver = 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)
                     {
                                     startActivity(new Intent(this, PreGameLobbyActivity.class).putExtra("com.lloydm.geosword.activities.PreGameLobbyActivity.gameid", gameid).putExtra("com.lloydm.geosword.activities.PreGameLobbyActivity.teaminfo", teaminfo));
                                     finish();
                     }
                     if (id == 0)
                     {
                                     startActivity(new Intent(this, MainMenuActivity.class));
                     }
     }

     private void setreceiver()
     {
                     if (joinreceiver == null)
                     {
                                     joinreceiver = 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.JoinOnlineGameActivity.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;

                                                                     }

                                                                     gameid = intent.getIntExtra("com.lloydm.geosword.activities.JoinOnlineGameActivity.gameid", -1);
                                                                     int reasoncode = intent.getIntExtra("com.lloydm.geosword.activities.JoinOnlineGameActivity.failurereasoncode", -1);

                                                                     if (success && gameid != -1)
                                                                     {
                                                                                     teaminfo = intent.getStringArrayExtra("com.lloydm.geosword.activities.JoinOnlineGameActivity.teaminfo");
                                                                                     // uploaded successfully....therefore we should load the
                                                                                     // next activity....
                                                                                     // and finish this one......
                                                                                     goingback = false;
                                                                                     onChangeActivity(gameid);

                                                                     }
                                                                     else
                                                                     {
                                                                                     // failure...inform the user and stay here.....
                                                                                     goingback = true;
                                                                                     if (reasoncode != -1)
                                                                                     {
                                                                                                     // switch(reasoncode)
                                                                                                     // {
                                                                                                     // default:
                                                                                                     // break;
                                                                                                     // }
                                                                                                     int reason = intent.getIntExtra("com.lloydm.geosword.activities.JoinOnlineGameActivity.authenticationerror", -1);
                                                                                                     if (reason == UploadService.LOGIN_NOT_FOUND)
                                                                                                     {
                                                                                                                     // show the authdialog.....
                                                                                                                     login();
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     if (reasoncode == UploadService.JOINERROR_GAMEFULL)
                                                                                                                     {
                                                                                                                                     newtoast("Sorry - game has filled up. No more spots left.");
                                                                                                                     }
                                                                                                                     if (reasoncode == UploadService.JOINERROR_BADPASS)
                                                                                                                     {
                                                                                                                                     newtoast("Incorrect Password for Game.");
                                                                                                                     }
                                                                                                                     if (reasoncode == UploadService.JOINERROR_CONTINUE)
                                                                                                                     {
                                                                                                                                     newtoast("Already in game. Choose continue instead");
                                                                                                                     }
                                                                                                                     if (reasoncode == 0)
                                                                                                                     {
                                                                                                                                     // error in database response.....
                                                                                                                                     newtoast("Unspecified network failure");
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     newtoast("Unspecified network error");
                                                                                     }
                                                                     }
                                                                     // reset waiting flag.....
                                                                     waiting = false;
                                                                     // allowedtosort = true;
                                                     }
                                     };
                                     registerReceiver(joinreceiver, new IntentFilter("com.lloydm.geosword.activities.JoinOnlineGameActivity.joingame"));
                     }

     }
}