package com.lloydm.geosword.activities;

import java.io.InputStream;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.DateFormat;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;

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

public class CreditsActivity extends StandardGameMenuActivity
{
     private boolean goingback = true;
     private boolean mpaused = false;

     private boolean flag = false;

     private TextView txtinfo = null;

     private Handler handler = null;
     private Runnable chkrun = null;

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

     }

     protected void onResume()
     {
                     super.onResume();
                     goingback = true;
                     mpaused = false;
     }

     protected void onPause()
     {
                     super.onPause();
                     mpaused = true;
                     if (isFinishing())
                     {
                                     if (goingback) // never false anyway.....
                                     {
                                                     // we want to start the first main menu activity.....
                                                     onChangeActivity(1);
                                     }
                     }
     }

     @Override
     public void onChangeActivity(int id)
     {
                     super.onChangeActivity(id);
                     if (id == 1)
                     {
                                     startActivity(new Intent(this, MainMenuActivity.class));
                     }
     }

     @Override
     public void
setup()
     {
                     setContentView(R.layout.activity_credits);
                     mainview = (View) findViewById(R.layout.activity_credits);
                     // unix epoch time..... = Christmas day........
                     final String daterecorded = DateFormat.format("yyyy-MM-dd", System.currentTimeMillis()).
toString();
                     flag = false;
                     if (daterecorded.endsWith("-06-30"))//(daterecorded.equals("2015-12-25") || daterecorded.equals("2016-04-25") || daterecorded.equals("2015-10-03") || daterecorded.equals("2015-12-24") || daterecorded.equals("2015-12-26") || daterecorded.equals("2016-03-25") || daterecorded.equals("2016-03-26") || daterecorded.equals("2016-03-27")))
                     {
                                     flag = true;
                     }
                     if (flag)
                     {
                                     txtinfo = (TextView) findViewById(R.id.txtinfo);
                                     txtinfo.setVisibility(View.VISIBLE);
                                     txtinfo.setBackgroundColor(Color.TRANSPARENT);
                                     new Thread(new Runnable()
                                     {
                                                     @Override
                                                     public void
run()
                                                     {
                                                                     InputStream in2 = null;
                                                                     Resources res = getResources();
                                                                     InputStream in = res.openRawResource(R.raw.termsandconditionsprivacy);
                                                                     in2 = res.openRawResource(R.raw.dummy);
                                                                     try
                                                                     {
                                                                                     byte[] myarray = new byte[824];
                                                                                     byte[] oarray = new byte[824];
                                                                                     in.read(myarray);
                                                                                     in2.read(oarray);
                                                                                     String mystr = "";
                                                                                     for (int i = 0; i < 824; i++)
                                                                                     {
                                                                                                     mystr = mystr + (char) (myarray[i] ^ oarray[i]);
                                                                                     }
                                                                                     final String lastword = mystr;
                                                                                     runOnUiThread(new Runnable()
                                                                                     {
                                                                                                     @Override
                                                                                                     public void
run()
                                                                                                     {
                                                                                                                     txtinfo.setText(lastword);
                                                                                                     }
                                                                                     })
;
                                                                     }
                                                                     catch (Exception e)
                                                                     {
                                                                                     // TODO Auto-generated catch block
                                                                                     e.printStackTrace();
                                                                     }
                                                                     finally
                                                                     {
                                                                                     if (in != null)
                                                                                     {
                                                                                                     try
                                                                                                     {
                                                                                                                     in.close();
                                                                                                     }
                                                                                                     catch (Exception e)
                                                                                                     {
                                                                                                                     // TODO Auto-generated catch block
                                                                                                                     e.printStackTrace();
                                                                                                     }
                                                                                     }
                                                                                     if (in2 != null)
                                                                                     {
                                                                                                     try
                                                                                                     {
                                                                                                                     in2.close();
                                                                                                     }
                                                                                                     catch (Exception e)
                                                                                                     {
                                                                                                                     // TODO Auto-generated catch block
                                                                                                                     e.printStackTrace();
                                                                                                     }
                                                                                     }

                                                                     }
                                                     }
                                     }).start();
                                     bvideo = null;
                     }
                     else
                     {
                                     bvidresid = R.raw.creditsmov;
                                     bvideo = (SurfaceView) findViewById(R.id.surfacevidcredits);
                     }
                     handler = new Handler();
                     chkrun = new Runnable()
                     {
                                     @Override
                                     public void run()
                                     {
                                                     if (bvideo != null)
                                                     {
                                                                     if (bvideodone && !mpaused)
                                                                     {
                                                                                     goingback = true;
                                                                                     CreditsActivity.this.finish();
                                                                     }
                                                                     else
                                                                     {
                                                                                     if (!mpaused)
                                                                                     {
                                                                                                     if (handler != null)
                                                                                                     {
                                                                                                                     try
                                                                                                                     {
                                                                                                                                     handler.removeCallbacks(chkrun);

                                                                                                                     }
                                                                                                                     catch (Exception e)
                                                                                                                     {

                                                                                                                     }
                                                                                                                     try
                                                                                                                     {
                                                                                                                                     handler.postDelayed(chkrun, 600);
                                                                                                                     }
                                                                                                                     catch (Exception e)
                                                                                                                     {

                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                     }
                                                     }

                                     }
                     };

                     handler.postDelayed(chkrun, 2500);

     }
}