package com.lloydm.geosword.activities;

import java.io.File;

import com.lloydm.geosword1.R;
import com.lloydm.geosword.common.Config;
import com.lloydm.geosword.common.MenuMusicService;
import com.lloydm.geosword.common.StandardGameMenuActivity;
import com.lloydm.geosword.common.UploadCompleteBroadcastReceiver;
import com.lloydm.geosword.common.UploadService;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.SurfaceView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;
import android.widget.Toast;

public class MainMenuActivity extends StandardGameMenuActivity
{
     private Button[] imgswitchers;

     private Button btntoggleaudio = null;

     private Button btnmainhelp = null;

     private boolean istablet = false;

     private ScrollView scrreplay = null;

     private UploadCompleteBroadcastReceiver loadreceiver = null;

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

     @Override
     public void setup()
     {

                     setContentView(R.layout.activity_mainmenu);
                     mainview = (View) findViewById(R.layout.activity_mainmenu);
                     // logmemoryusage();

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

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

                     scrreplay = (ScrollView) findViewById(R.id.scrollreplay1);
                     imgswitchers = new Button[5];

                     imgswitchers[0] = (Button) findViewById(R.id.btn_new_game);
                     imgswitchers[1] = (Button) findViewById(R.id.btn_join_game);
                     imgswitchers[2] = (Button) findViewById(R.id.btn_cont_game);
                     imgswitchers[3] = (Button) findViewById(R.id.btn_options_game);
                     imgswitchers[4] = (Button) findViewById(R.id.btn_stats);
                     btntoggleaudio = (Button) findViewById(R.id.btntoggleaudio);
                     // check whether it should be activated or not....based on shared prefs....
                     String audiostatus = getprefs("com.lloydm.geosword.toggleaudio", "1");
                     if (audiostatus.equals("1"))
                     {
                                     btntoggleaudio.setActivated(true);
                     }
                     else
                     {
                                     btntoggleaudio.setActivated(false);
                     }

                     btntoggleaudio.setOnClickListener(new View.OnClickListener()
                     {
                                     @Override
                                     public void
onClick(View v)
                                     {
                                                     runOnUiThread(new Runnable()
                                                     {

                                                                     @Override
                                                                     public void
run()
                                                                     {
                                                                                     if (btntoggleaudio.isActivated())
                                                                                     {
                                                                                                     btntoggleaudio.setActivated(false);
                                                                                                     saveprefs("com.lloydm.geosword.toggleaudio", "0");
                                                                                                     // mute the audio.....send a broadcast....
                                                                                                     Intent serviceintent = new Intent(MainMenuActivity.this, MenuMusicService.class);
                                                                                                     if (Build.VERSION.SDK_INT >= 12)
                                                                                                     {
                                                                                                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                                                                                     }
                                                                                                     serviceintent.setAction("com.lloydm.geosword.common.MenuMusicService.muteon");

                                                                                                     startService(serviceintent);
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     btntoggleaudio.setActivated(true);
                                                                                                     saveprefs("com.lloydm.geosword.toggleaudio", "1");
                                                                                                     // unmute the audio....send a broadcast.....
                                                                                                     Intent serviceintent = new Intent(MainMenuActivity.this, MenuMusicService.class);
                                                                                                     if (Build.VERSION.SDK_INT >= 12)
                                                                                                     {
                                                                                                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                                                                                                     }
                                                                                                     serviceintent.setAction("com.lloydm.geosword.common.MenuMusicService.muteoff");

                                                                                                     startService(serviceintent);
                                                                                     }

                                                                     }
                                                     });
                                     }
                     });

                     imgswitchers[3].setTypeface(font);
                     imgswitchers[4].setTypeface(font);

                     // imgswitchers[3].setEnabled(false);
                     for (int i = 0; i < 5; i++) // for now...
                     {
                                     imgswitchers[i].setId(i);
                                     imgswitchers[i].setOnClickListener(new View.OnClickListener()
                                     {
                                                     @Override
                                                     public void
onClick(View v)
                                                     {
                                                                     int id = v.getId();
                                                                     switch (id)
                                                                     {
                                                                     case 0:
                                                                                     // go to create game screen.....
                                                                                     txt = "Create game...";
                                                                                     onChangeActivity(0);
                                                                                     break;

                                                                     case 1:
                                                                                     // go to join game screen....

                                                                                     txt = "Join game...";
                                                                                     onChangeActivity(1);
                                                                                     break;

                                                                     case 2:
                                                                                     // go to continue game screen....
                                                                                     // startActivity(new Intent((Context)
                                                                                     // MainMenuActivity.this,ContinueGameActivity.class));

                                                                                     txt = "Continue game...";
                                                                                     onChangeActivity(2);
                                                                                     break;

                                                                     case 3:
                                                                                     // go to options screen......
                                                                                     // startActivity(new Intent((Context)
                                                                                     // MainMenuActivity.this,OptionsActivity.class));

                                                                                     txt = "Options game..."; // credits now....
                                                                                     onChangeActivity(3);
                                                                                     break;
                                                                     case 4:
                                                                                     onChangeActivity(5);
                                                                                     break;
                                                                     default:

                                                                                     break;
                                                                     }
                                                                     // newtoast();
                                                     }
                                     });
                     }

                     // get the battles on the device.....
                     btnmainhelp = (Button) findViewById(R.id.btnmainhelp);
                     btnmainhelp.setTypeface(font);
                     btnmainhelp.setOnClickListener(new View.OnClickListener()
                     {

                                     @Override
                                     public void
onClick(View v)
                                     {
                                                     onChangeActivity(7);

                                     }
                     });

                     bvidresid = R.raw.mainmov;
                     bvideo = (SurfaceView) findViewById(R.id.vidmainmenu);
                     bvideolooped = true;
                     bhidevidonfinish = false;
                     getdevicebattles();

     }

     private void getdevicebattles()
     {
                     LinearLayout lay = (LinearLayout) findViewById(R.id.laybattles2);
                     if (lay != null)
                     {
                                     if (lay.getContentDescription().equals("tablet"))
                                     {
                                                     istablet = true;
                                     }
                     }

                     // look at the user's device.....
                     String outputpath = Config.getStorageFolder(MainMenuActivity.this);
                     File folder1 = new File(outputpath + Config.DOWNLOADEDREPLAYFOLDER);
                     File folder2 = new File(outputpath + Config.REPLAYFOLDER);
                     File[] flist1 = null;
                     Log.i(TAG, "folders");
                     for (int j = 0; j < 2; j++)
                     {
                                     File folder = null;
                                     if (folder1.isDirectory() && j == 0)
                                     {
                                                     folder = folder1;
                                     }
                                     if (folder2.isDirectory() && j == 1)
                                     {
                                                     folder = folder2;
                                     }
                                     if (folder == null)
                                     {
                                                     Log.i(TAG, "folder is null:" + folder1.getAbsolutePath());

                                                     continue;
                                     }
                                     if (folder.isDirectory())
                                     {
                                                     Log.i(TAG, "folders2");
                                                     // get the files within it...that have extension EP4.....
                                                     File[] filelist = folder.listFiles();
                                                     if (j == 0)
                                                     {
                                                                     flist1 = folder.listFiles();
                                                     }
                                                     if (filelist != null)
                                                     {
                                                                     for (int i = 0; i < filelist.length; i++)
                                                                     {
                                                                                     boolean skipit = false;
                                                                                     File f = filelist[i];
                                                                                     if (j == 1)
                                                                                     {
                                                                                                     if (flist1 != null)
                                                                                                     {
                                                                                                                     for (int ii = 0; ii < flist1.length; ii++)
                                                                                                                     {
                                                                                                                                     File ff = flist1[ii];
                                                                                                                                     if (ff != null)
                                                                                                                                     {
                                                                                                                                                     if (ff.isFile() && f.isFile())
                                                                                                                                                     {
                                                                                                                                                                     if (ff.canRead() && f.canRead())
                                                                                                                                                                     {
                                                                                                                                                                                     if (ff.length() == f.length())
                                                                                                                                                                                     {
                                                                                                                                                                                                     // very, very, very likely the same replay....so don't include...
                                                                                                                                                                                                     // skip it...
                                                                                                                                                                                                     skipit = true;
                                                                                                                                                                                                     break;
                                                                                                                                                                                     }
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                     }

                                                                                     if (skipit)
                                                                                     {
                                                                                                     continue;
                                                                                     }

                                                                                     // check the extension....
                                                                                     // also get the file date....no other info really needed.....
                                                                                     if (f.getName().endsWith(".EP4"))

                                                                                     {
                                                                                                     // right sort of file.....
                                                                                                     // Calendar cal = Calendar.getInstance();
                                                                                                     // cal.setTimeInMillis(f.lastModified());
                                                                                                     // DateFormat.format(, inTimeInMillis)
                                                                                                     // DateFormat.format(DateFormat.getDateInstance(),f.lastModified());
                                                                                                     final String daterecorded = DateFormat.format("yyyy-MM-dd HH:mm", f.lastModified()).toString();

                                                                                                     TextView txt = new TextView(MainMenuActivity.this);
                                                                                                     txt.setSingleLine();
                                                                                                     txt.setEllipsize(TruncateAt.MARQUEE);
                                                                                                     txt.setTypeface(font);
                                                                                                     txt.setTextColor(Color.rgb(192, 192, 192));
                                                                                                     txt.setBackgroundColor(Color.rgb(48, 48, 80));
                                                                                                     txt.setText("Replay - " + daterecorded);
                                                                                                     txt.setTextSize(18);
                                                                                                     txt.setContentDescription(f.getAbsolutePath());

                                                                                                     txt.setFocusable(true);
                                                                                                     txt.setFocusableInTouchMode(true);
                                                                                                     txt.setSelected(true);
                                                                                                     txt.setHorizontallyScrolling(true);
                                                                                                     txt.setMarqueeRepeatLimit(-1);

                                                                                                     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                                                                                                     if (istablet)
                                                                                                     {
                                                                                                                     params.topMargin = 30;
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     params.topMargin = 60;
                                                                                                     }
                                                                                                     params.leftMargin = 2;
                                                                                                     txt.setLayoutParams(params);
                                                                                                     txt.setOnClickListener(new View.OnClickListener()
                                                                                                     {
                                                                                                                     @Override
                                                                                                                     public void
onClick(View v)
                                                                                                                     {
                                                                                                                                     // watch the battle!
                                                                                                                                     startActivity(new Intent(MainMenuActivity.this, TestActivity.class).putExtra("com.lloydm.geosword.activities.TestActivity.replayfile", v.getContentDescription()));
                                                                                                                                     finish();
                                                                                                                     }
                                                                                                     });
                                                                                                     scrreplay.setVisibility(View.VISIBLE);
                                                                                                     if (txt != null)
                                                                                                     {
                                                                                                                     if (txt.getText().toString().contains("Replay"))
                                                                                                                     {
                                                                                                                                     lay.addView(txt);
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                     }
                                                     }

                                     }

                     }

     }

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

     @Override
     protected void onResume()
     {
                     super.onResume();
                     // reload all images..
                     // start service to check if server is alive.....
                     setreceiver();
                     Intent serviceintent = new Intent(MainMenuActivity.this, UploadService.class);
                     if (Build.VERSION.SDK_INT >= 12)
                     {
                                     serviceintent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                     }
                     serviceintent.setAction("com.lloydm.geosword.common.UploadService.isserveralive");

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

     @Override
     public void onChangeActivity(int id)
     {
                     super.onChangeActivity(id);
                     logmemoryusage();
                     if (id == 0)
                     {
                                     startActivity(new Intent(this, CreateOnlineGameActivity.class));
                                     finish();
                     }

                     if (id == 1)
                     {
                                     startActivity(new Intent(this, JoinOnlineGameActivity.class));
                                     finish();
                     }

                     if (id == 2)
                     {
                                     startActivity(new Intent(this, ContinueGameActivity.class));
                                     finish();
                     }

                     if (id == 3)
                     {
                                     Intent mintent = new Intent(this, CreditsActivity.class);
                                     startActivity(mintent);
                                     finish();
                     }
                     if (id == 4)
                     {
                                     startActivity(new Intent(this, TestActivity.class).putExtra("com.lloydm.geosword.activities.TestActivity.screenviewmode", 1));

                                     finish();
                     }
                     if (id == 5)
                     {
                                     startActivity(new Intent(this, GeekyStatsActivity.class));
                                     finish();
                     }
                     if (id == 7)
                     {
                                     startActivity(new Intent(this, HelpVideoActivity.class));
                                     finish();
                     }

     }

     private void
setreceiver()
     {
                     if (loadreceiver == null)
                     {
                                     loadreceiver = new UploadCompleteBroadcastReceiver()
                                     {
                                                     @Override
                                                     public void
onReceive(Context context, Intent intent)
                                                     {
                                                                     // the intent will contain the response from the
                                                                     // server.....either true or false....very simple - that's
                                                                     // all we care
                                                                     // about when we send information to the server...and
                                                                     // possibly an error code...
                                                                     boolean success = intent.getBooleanExtra("com.lloydm.geosword.activities.MainMenuActivity.serverdeadresult", 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;
                                                                     }
                                                                     if (success)
                                                                     {
                                                                                     // server is dead...not really a success but it is in the context of the call coming back!
                                                                                     runOnUiThread(new Runnable()
                                                                                     {
                                                                                                     @Override
                                                                                                     public void
run()
                                                                                                     {
                                                                                                                     Toast.makeText(MainMenuActivity.this, R.string.networkunavailable, Toast.LENGTH_SHORT).show();
                                                                                                     }
                                                                                     });
                                                                     }
                                                     }
                                     };
                                     registerReceiver(loadreceiver, new IntentFilter("com.lloydm.geosword.activities.MainMenuActivity.serverdead"));
                     }
     }
}