package com.example.lessonThree;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class MainGameView extends SurfaceView implements SurfaceHolder.Callback 
{
    class MainGameThread extends Thread 
    {
    	private SpaceCreature _spaceCreature;
    	
        private int _canvasHeight = 1;
        private int _canvasWidth = 1;

        /** Indicate whether the surface has been created & is ready to draw */
        private boolean _run = false;

        /** Handle to the surface manager object we interact with */
        private SurfaceHolder _surfaceHolder;

        public MainGameThread(SurfaceHolder surfaceHolder, Context context, Handler handler) 
        {
        	_spaceCreature = new SpaceCreature();
        	
            // get handles to some important objects
            _surfaceHolder = surfaceHolder;
        }

        /**
         * Starts the game, setting parameters for the current difficulty.
         */
        public void doStart() 
        {
            synchronized (_surfaceHolder) 
            {
            }
        }

        @Override
        public void run() 
        {
            while (_run) 
            {
                _spaceCreature.DoSomething();
                
            	Canvas c = null;
                try 
                {
                    c = _surfaceHolder.lockCanvas(null);
                    synchronized (_surfaceHolder) 
                    {
                        doDraw(c);
                    }
                } 
                finally 
                {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) 
                    {
                        _surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }

        public void setRunning(boolean b) 
        {
            _run = b;
        }

        /* Callback invoked when the surface dimensions change. */
        public void setSurfaceSize(int width, int height) 
        {
            // synchronized to make sure these all change atomically
            synchronized (_surfaceHolder) 
            {
                _canvasWidth = width;
                _canvasHeight = height;
            }
        }

        private void doDraw(Canvas canvas) 
        {
           	// MIX MY PAINT
        	Paint backgroundPaint = new Paint();
            backgroundPaint.setColor(Color.rgb(0, 0, 0));        	
        	
            ShapeDrawable alienBody = new ShapeDrawable();
            alienBody.getPaint().setColor(backgroundPaint.getColor());	
            // 1st Number = left corner
            // 2nd Number = top corner
            // 3rd Number = right corner
            // 4th Number = bottom corner
            alienBody.setBounds(0, 0, _canvasWidth, _canvasHeight);        
            alienBody.draw(canvas); 
                        
        	_spaceCreature.Draw(canvas);
        }

    }


    /** The thread that actually draws the animation */
    private MainGameThread _thread;

    public MainGameView(Context context) 
    {
    	super(context);    	

        // register our interest in hearing about changes to our surface
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);

        // create thread only; it's started in surfaceCreated()
        _thread = new MainGameThread(holder, context, new Handler() 
        {
            @Override
            public void handleMessage(Message m) 
            {
            }
        });

        setFocusable(true); // make sure we get key events
    }

    /**
     * Fetches the animation thread corresponding to this MainGameView.
     * 
     * @return the animation thread
     */
    public MainGameThread getThread() 
    {
        return _thread;
    }

    /**
     * Standard window-focus override. Notice focus lost so we can pause on
     * focus lost. e.g. user switches to take a call.
     */
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) 
    {
        if (!hasWindowFocus) 
        	_thread.setRunning(false);
    }


    /* Callback invoked when the surface dimensions change. */
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
    {
        _thread.setSurfaceSize(width, height);
    }

    /*
     * Callback invoked when the Surface has been created and is ready to be
     * used.
     */
    public void surfaceCreated(SurfaceHolder holder) 
    {
        // start the thread here so that we don't busy-wait in run()
        // waiting for the surface to be created
        _thread.setRunning(true);
        _thread.start();
    }

    /*
     * Callback invoked when the Surface has been destroyed and must no longer
     * be touched. WARNING: after this method returns, the Surface/Canvas must
     * never be touched again!
     */
    public void surfaceDestroyed(SurfaceHolder holder) 
    {
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        _thread.setRunning(false);
        while (retry) 
        {
            try 
            {
                _thread.join();
                retry = false;
            } 
            catch (InterruptedException e) 
            {
            }
        }
    }
}