package com.lloydm.geosword.activities;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lloydm.geosword1.R;
import com.lloydm.geosword.common.StandardGameMenuActivity;

public class InviteActivity extends StandardGameMenuActivity
{
     private final static String TAG = "com.lloydm.geosword.activities.InviteActivity";

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

     private String gamekey = "";
     private String gamename = "";
     private boolean goingback = true;

     private Button btnsendinvite = null;

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

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

                     bvidresid = R.raw.mnumov;
                     bvideolooped = true;
                     bvideo = (SurfaceView) findViewById(R.id.surfacevidinvite);

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

                     if (gamekey == null || gamename == null)
                     {
                                     gamekey = "Invalid Key";
                                     TextView txtview2 = (TextView) findViewById(R.id.txtinvitedesc);
                                     txtview2.setText(getString(R.string.invitescreentxterr));
                     }

                     // what is going to be on this screen?
                     // ultimately this should really just provide a method of sending a
                     // message to someone you know...
                     // pick your friends and send a message?
                     // eg perhaps it will start your email client?

                     // hmmm design issues.....

                     // maybe we just show the game key in a big text box for now?

                     // user can share it however they like?

                     TextView txtview = (TextView) findViewById(R.id.txtgamekey);
                     txtview.setText(gamename + "\n" + gamekey);

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

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

                     // button to launch your email client.....?
                     btnsendinvite = (Button) findViewById(R.id.btn_sendinvite);
                     btnsendinvite.setTypeface(font);
                     if (gamekey != null && gamename != null)
                     {
                                     final String emailmsg = "You have been invited to join a game session of Star Dancer, the cinematic space opera for Android mobile.\n\nThe name of the session is:" + gamename + " and the passkey to connect is:" + gamekey + ". \n\n\nDownload the lite version from google play at " + Uri.parse("https://play.google.com/store/apps/details?id=com.lloydm.geoswordlite") + " or the full version from " + Uri.parse("https://play.google.com/store/apps/details?id=com.lloydm.geosword1") + "\n\n";
                                     btnsendinvite.setVisibility(View.VISIBLE);
                                     btnsendinvite.setOnClickListener(new View.OnClickListener()
                                     {
                                                     @Override
                                                     public void onClick(View v)
                                                     {
                                                                     // start the user's email client......
                                                                     Intent sendIntent = new Intent(Intent.ACTION_SEND);
                                                                     sendIntent.putExtra(Intent.EXTRA_SUBJECT, R.string.invitesubject);
                                                                     sendIntent.putExtra(Intent.EXTRA_TEXT, emailmsg);
                                                                     sendIntent.setType("text/plain");

                                                                     String title = getResources().getString(R.string.sendchooser);
                                                                     Intent chooser = Intent.createChooser(sendIntent, title);
                                                                     if (sendIntent.resolveActivity(getPackageManager()) != null)
                                                                     {
                                                                                     goingback = false;
                                                                                     startActivity(chooser);
                                                                     }
                                                     }
                                     });
                     }
     }

     @Override
     protected void onResume()
     {
                     super.onResume();
                     Log.i(TAG, "invite resumed");
                     goingback = true;
                     resumevideo(TAG);
                     setuphelp(R.string.htt_invite, R.string.htxt_invite, R.string.htt_invite);
     }

     @Override
     protected void onPause()
     {
                     super.onPause();
                     Log.i(TAG, "invite paused");
                     if (isFinishing())
                     {
                                     if (goingback)
                                     {
                                                     // we want to start the first ingame menu...
                                                     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();
                     }
     }

}