package com.lloydm.geosword.wallpaper;

//adding wallpaper functionality to the game....very simple hopefully but we will see.......
import java.io.File;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;

import com.lloydm.geosword1.R;

public class MyWallpaperService extends WallpaperService
{
     private static final String TAG = "com.lloydm.geosword.wallpaper.MyWallpaperService";

     @Override
     public Engine onCreateEngine()
     {

                     return new MyWallpaperEngine();
     }

     private class MyWallpaperEngine extends Engine implements SurfaceHolder.Callback
     {
                     private SurfaceHolder msh = null;
                     private Bitmap bmp = null;
                     private Handler mh = null;
                     private volatile boolean firsttime = true;
                     private volatile boolean surfok = false;
                     private int ff = 0;
                     private int gg = 0;
                     private int jj = 0;
                     private Resources res = null;
                     private File[] files = null;
                     private File[] imgfiles = null;

                     @SuppressLint("HandlerLeak")
                     @Override
                     public void onCreate(SurfaceHolder sh)
                     {
                                     res = getResources();
                                     setOffsetNotificationsEnabled(false);
                                     Log.i(TAG, "wallpaper creating");
                                     readpicturefolder();
                                     // check if surface is okay....
                                     try
                                     {
                                                     if (sh != null)
                                                     {
                                                                     Log.i(TAG, "surface holder not null");
                                                     }
                                                     else
                                                     {
                                                                     Log.w(TAG, "surface holder null");

                                                     }
                                                     Log.i(TAG, "sf value?");
                                                     Surface sf = sh.getSurface();
                                                     if (sf.isValid())
                                                     {
                                                                     Log.i(TAG, "surfok");
                                                                     surfok = true;
                                                     }
                                                     else
                                                     {
                                                                     Log.i(TAG, "surf not ok");
                                                                     surfok = false;
                                                     }
                                     }
                                     catch (Exception ef)
                                     {
                                                     Log.w(TAG, "error with surfaceholder");
                                     }

                                     mh = new Handler()
                                     {
                                                     public void handleMessage(Message msg)
                                                     {
                                                                     if (msg.what == 1)
                                                                     {
                                                                                     try
                                                                                     {
                                                                                                     msh = getSurfaceHolder();
                                                                                                     Surface sf = msh.getSurface();
                                                                                                     if (sf.isValid())
                                                                                                     {
                                                                                                                     surfok = true;
                                                                                                                     Log.i(TAG, "all good");
                                                                                                     }
                                                                                                     else
                                                                                                     {
                                                                                                                     surfok = false;
                                                                                                                     Log.i(TAG, "not all good");
                                                                                                     }
                                                                                     }
                                                                                     catch (Exception e)
                                                                                     {
                                                                                                     Log.w(TAG, "error getting surface holder");
                                                                                     }

                                                                                     if (isVisible())
                                                                                     {
                                                                                                     Log.i(TAG, "isvis");
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     Log.w(TAG, "notvis");
                                                                                                     try
                                                                                                     {
                                                                                                                     if (bmp != null)
                                                                                                                     {
                                                                                                                                     bmp.recycle();
                                                                                                                                     bmp = null;
                                                                                                                     }
                                                                                                     }
                                                                                                     catch (Exception e)
                                                                                                     {
                                                                                                                     Log.w(TAG, "problem freeing bitmap");

                                                                                                     }
                                                                                     }

                                                                                     if (jj % 4 == 0)
                                                                                     {
                                                                                                     // recheck the folder......
                                                                                                     readpicturefolder();
                                                                                     }

                                                                                     if (surfok)
                                                                                     {
                                                                                                     Log.i(TAG, "surfok");
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     Log.w(TAG, "surfnotok");
                                                                                     }

                                                                                     if (isVisible() && surfok)
                                                                                     {
                                                                                                     try
                                                                                                     {
                                                                                                                     Log.i(TAG, "getting surfaceholder");
                                                                                                                     msh = getSurfaceHolder();
                                                                                                                     Canvas cnv = null;
                                                                                                                     try
                                                                                                                     {
                                                                                                                                     Log.i(TAG, "getting canvas");
                                                                                                                                     cnv = msh.lockCanvas();
                                                                                                                                     final int w = cnv.getWidth();
                                                                                                                                     final int h = cnv.getHeight();

                                                                                                                                     if (bmp != null)
                                                                                                                                     {
                                                                                                                                                     Log.i(TAG, "recycling bitmap");
                                                                                                                                                     try
                                                                                                                                                     {
                                                                                                                                                                     bmp.recycle();
                                                                                                                                                                     bmp = null;
                                                                                                                                                     }
                                                                                                                                                     catch (Exception fge)
                                                                                                                                                     {
                                                                                                                                                                     Log.e(TAG, "error recycling");
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                                     if (gg != 0)
                                                                                                                                     {
                                                                                                                                                     if (ff >= 0 && ff < gg)
                                                                                                                                                     {
                                                                                                                                                                     Log.i(TAG, "getting bitmap from folder");
                                                                                                                                                                     try
                                                                                                                                                                     {
                                                                                                                                                                                     bmp = BitmapFactory.decodeFile(imgfiles[ff].getAbsolutePath());
                                                                                                                                                                     }
                                                                                                                                                                     catch (Exception efg)
                                                                                                                                                                     {
                                                                                                                                                                                     Log.e(TAG, "error reading bitmap");
                                                                                                                                                                     }
                                                                                                                                                                     Log.i(TAG, "bitmap gotten from folder");
                                                                                                                                                     }
                                                                                                                                                     else
                                                                                                                                                     {
                                                                                                                                                                     if ((ff % 3) == 0)
                                                                                                                                                                     {
                                                                                                                                                                                     Log.i(TAG, "getting bitmap 1");
                                                                                                                                                                                     bmp = BitmapFactory.decodeResource(res, R.drawable.back1);
                                                                                                                                                                                     Log.i(TAG, "bitmap 1 gotten");
                                                                                                                                                                     }
                                                                                                                                                                     if ((ff % 3) == 1)
                                                                                                                                                                     {
                                                                                                                                                                                     Log.i(TAG, "getting bmp 2");
                                                                                                                                                                                     bmp = BitmapFactory.decodeResource(res, R.drawable.mapbk1);
                                                                                                                                                                                     Log.i(TAG, "bmp2 gotten");
                                                                                                                                                                     }
                                                                                                                                                                     if ((ff % 3) == 2)
                                                                                                                                                                     {
                                                                                                                                                                                     Log.i(TAG, "getting bmp3");
                                                                                                                                                                                     bmp = BitmapFactory.decodeResource(res, R.drawable.loading1bk);
                                                                                                                                                                                     Log.i(TAG, "bmp 3 gotten");
                                                                                                                                                                     }
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     if (ff == 0)
                                                                                                                                                     {
                                                                                                                                                                     Log.i(TAG, "getting bitmap 1");
                                                                                                                                                                     bmp = BitmapFactory.decodeResource(res, R.drawable.back1);

                                                                                                                                                                     Log.i(TAG, "bitmap 1 gotten");
                                                                                                                                                     }
                                                                                                                                                     if (ff == 1)
                                                                                                                                                     {
                                                                                                                                                                     Log.i(TAG, "getting bmp 2");
                                                                                                                                                                     bmp = BitmapFactory.decodeResource(res, R.drawable.mapbk1);
                                                                                                                                                                     Log.i(TAG, "bmp2 gotten");
                                                                                                                                                     }
                                                                                                                                                     if (ff == 2)
                                                                                                                                                     {
                                                                                                                                                                     Log.i(TAG, "getting bmp3");
                                                                                                                                                                     bmp = BitmapFactory.decodeResource(res, R.drawable.loading1bk);
                                                                                                                                                                     Log.i(TAG, "bmp 3 gotten");
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                                     ff++;
                                                                                                                                     if (gg != 0)
                                                                                                                                     {
                                                                                                                                                     ff %= (gg + 3);
                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     ff %= 3;
                                                                                                                                     }
                                                                                                                                     if (bmp != null)
                                                                                                                                     {
                                                                                                                                                     Log.i(TAG, "setting bitmap");
                                                                                                                                                     if ((bmp.getWidth() > bmp.getHeight() && w < h))
                                                                                                                                                     {
                                                                                                                                                                     // rotate it 90 degrees.....
                                                                                                                                                                     Log.i(TAG, "rotating bitmap");
                                                                                                                                                                     Matrix mt = new Matrix();
                                                                                                                                                                     mt.setRotate(90, w / 2, w / 2);
                                                                                                                                                                     if (w != bmp.getHeight())
                                                                                                                                                                     {
                                                                                                                                                                                     mt.postScale((float) w / (float) (bmp.getHeight()), (float) h / (float) bmp.getWidth());
                                                                                                                                                                                     mt.postTranslate(-w / 2, 0);
                                                                                                                                                                     }
                                                                                                                                                                     cnv.drawBitmap(bmp, mt, null);
                                                                                                                                                     }
                                                                                                                                                     else
                                                                                                                                                     {
                                                                                                                                                                     Log.i(TAG, "not rotated");
                                                                                                                                                                     cnv.drawBitmap(bmp, null, new Rect(0, 0, w, h), null);
                                                                                                                                                     }
                                                                                                                                                     firsttime = false;
                                                                                                                                     }
                                                                                                                                     else
                                                                                                                                     {
                                                                                                                                                     Log.i(TAG, "drawing black instead");
                                                                                                                                                     cnv.drawARGB(255, 0, 0, 0);
                                                                                                                                                     firsttime = true;
                                                                                                                                     }
                                                                                                                                     Log.i(TAG, "drawing to canvas");
                                                                                                                     }
                                                                                                                     catch (Exception f)
                                                                                                                     {
                                                                                                                                     Log.w(TAG, "could not lock canvas");
                                                                                                                     }
                                                                                                                     try
                                                                                                                     {
                                                                                                                                     Log.i(TAG, "unlocking canvas");
                                                                                                                                     msh.unlockCanvasAndPost(cnv);
                                                                                                                     }
                                                                                                                     catch (Exception g)
                                                                                                                     {
                                                                                                                                     Log.w(TAG, "could not unlock canvas");
                                                                                                                     }
                                                                                                     }
                                                                                                     catch (Exception e)
                                                                                                     {
                                                                                                                     Log.e(TAG, "error in wallpaper");
                                                                                                     }
                                                                                     }
                                                                                     if (isVisible())
                                                                                     {
                                                                                                     try
                                                                                                     {
                                                                                                                     Log.i(TAG, "sending message again");
                                                                                                                     Message msg2 = new Message();
                                                                                                                     msg2.what = 1;
                                                                                                                     if (firsttime) // a bit quicker on the first time through since we don't want to stay a black screen for too long.
                                                                                                                     {
                                                                                                                                     mh.sendMessageDelayed(msg2, 1500); // send a message to change the surface image.....simply replace with another one for now....
                                                                                                                     }
                                                                                                                     else
                                                                                                                     {
                                                                                                                                     mh.sendMessageDelayed(msg2, 7000); // send a message to change the surface image.....simply replace with another one for now....
                                                                                                                                     jj++;
                                                                                                                     }
                                                                                                     }
                                                                                                     catch (Exception fge)
                                                                                                     {
                                                                                                                     Log.e(TAG, "error sending message");
                                                                                                     }
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                                     try
                                                                                                     {
                                                                                                                     mh.removeCallbacksAndMessages(null);
                                                                                                     }
                                                                                                     catch (Exception fge)
                                                                                                     {
                                                                                                                     Log.w(TAG, "problem stopping mh");
                                                                                                     }
                                                                                     }
                                                                     }
                                                     }
                                     };
                                     Log.i(TAG, "sending message 1");
                                     Message msg = new Message();
                                     msg.what = 1;
                                     mh.sendMessage(msg); // message to start the wallpaper
                                     Log.i(TAG, "sending message 2 - wallpaper should be created");
                     }

                     private void readpicturefolder()
                     {
                                     gg = 0;
                                     imgfiles = null;
                                     try
                                     {
                                                     File picfolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + com.lloydm.geosword.common.Config.SCREENSHOTFOLDER);
                                                     if (!picfolder.exists())
                                                     {
                                                                     picfolder.mkdirs();
                                                     }
                                                     if (picfolder.isDirectory())
                                                     {
                                                                     files = picfolder.listFiles();
                                                                     if (files != null)
                                                                     {
                                                                                     for (int i = 0; i < files.length; i++)
                                                                                     {
                                                                                                     try
                                                                                                     {
                                                                                                                     File fi = new File(files[i].getAbsolutePath());
                                                                                                                     if (fi.exists() && fi.canRead() && fi.isFile())
                                                                                                                     {
                                                                                                                                     String fn = fi.getName();
                                                                                                                                     if (fn.endsWith("png") || fn.endsWith("jpg"))

                                                                                                                                     {
                                                                                                                                                     gg++;
                                                                                                                                     }
                                                                                                                     }
                                                                                                     }
                                                                                                     catch (Exception ffi)
                                                                                                     {
                                                                                                                     Log.w(TAG, "do nothing");
                                                                                                     }
                                                                                     }
                                                                                     if (gg > 0)
                                                                                     {
                                                                                                     imgfiles = new File[gg];
                                                                                                     int j = 0;
                                                                                                     for (int i = 0; i < files.length; i++)
                                                                                                     {
                                                                                                                     try
                                                                                                                     {
                                                                                                                                     File fi = new File(files[i].getAbsolutePath());
                                                                                                                                     if (fi.exists() && fi.canRead() && fi.isFile())
                                                                                                                                     {
                                                                                                                                                     String fn = fi.getName();
                                                                                                                                                     if (fn.endsWith("png") || fn.endsWith("jpg"))

                                                                                                                                                     {
                                                                                                                                                                     if (j < gg)
                                                                                                                                                                     {
                                                                                                                                                                                     imgfiles[j] = new File(fi.getAbsolutePath());
                                                                                                                                                                     }
                                                                                                                                                                     else
                                                                                                                                                                     {
                                                                                                                                                                                     break;
                                                                                                                                                                     }
                                                                                                                                                                     j++;
                                                                                                                                                     }
                                                                                                                                     }
                                                                                                                     }
                                                                                                                     catch (Exception ffi)
                                                                                                                     {
                                                                                                                                     Log.w(TAG, "do nothing");
                                                                                                                     }
                                                                                                     }
                                                                                     }
                                                                     }
                                                     }
                                     }
                                     catch (Exception ffg)
                                     {
                                                     Log.e(TAG, "error reading picture folder");
                                     }
                     }

                     @Override
                     public void onDestroy()
                     {
                                     surfok = false;
                                     try
                                     {
                                                     Log.i(TAG, "wallpaper destroyed");
                                                     mh.removeCallbacksAndMessages(null);
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "error destroying wallpaper");
                                     }

                     }

                     @Override
                     public void surfaceCreated(SurfaceHolder holder)
                     {
                                     Log.i(TAG, "surface created");
                                     surfok = true;
                     }

                     @Override
                     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
                     {
                                     Log.i(TAG, "surface changed");

                                     surfok = true;
                     }

                     @Override
                     public void surfaceDestroyed(SurfaceHolder holder)
                     {
                                     Log.w(TAG, "surface destroyed");
                                     surfok = false;
                     }

                     @Override
                     public void onVisibilityChanged(boolean visible)
                     {
                                     Log.i(TAG, "visibility changed");
                                     if (visible)
                                     {
                                                     Log.i(TAG, "vis on");
                                                     // start us up again if we can.........
                                                     try
                                                     {
                                                                     Message msg2 = new Message();
                                                                     msg2.what = 1;
                                                                     mh.removeCallbacksAndMessages(null);
                                                                     mh.sendMessage(msg2); // send a message to change the surface image.....simply replace with another one for now....
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.e(TAG, "problem restarting");
                                                     }

                                     }
                                     else
                                     {
                                                     Log.i(TAG, "vis off");
                                                     // stop on the next run through.......to free memory etc.......
                                                     try
                                                     {
                                                                     mh.removeCallbacksAndMessages(null);
                                                     }
                                                     catch (Exception fge)
                                                     {
                                                                     Log.w(TAG, "problem stopping mh");
                                                     }
                                                     try
                                                     {
                                                                     if (bmp != null)
                                                                     {
                                                                                     bmp.recycle();
                                                                                     bmp = null;
                                                                     }
                                                     }
                                                     catch (Exception e)
                                                     {
                                                                     Log.w(TAG, "error freeing bitmap");
                                                     }
                                     }
                     }
     }

}