using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightGame
{
    /*
        A game object with a static image
    */
    public class GameObject : BaseObject
    {
        protected Rectangle rect = null;
        protected ImageBrush imageBrush = null;
        protected Point position = new Point();
        protected Point dimensions = new Point();
        protected int zLayer = 0;

        public int ZLayer
        {
            get
            {
                return zLayer;
            }
        }

        public Point Dimensions
        {
            get
            {
                return dimensions;
            }
        }
        
        public Point Position
        {
            set
            {
                position = value;

                // make sure we are only moving in whole pixels (i.e. integer values)
                // this stops the display from looking fuzzy
                rect.SetValue(Canvas.LeftProperty, Math.Round(position.X));
                rect.SetValue(Canvas.TopProperty, Math.Round(position.Y));
            }
            get
            {
                return position;
            }
        }

        public GameObject()
        {

        }

        public void startupGameObject(Point dimensions, string image, int zLayer)
        {
            base.startupBaseObject();

            imageBrush = null;
            position.X = 0;
            position.Y = 0;
            this.dimensions = dimensions;
            this.zLayer = zLayer;

            rect = new Rectangle()
            {
                Height = dimensions.Y,
                Width = dimensions.X
            };
            rect.SetValue(Canvas.LeftProperty, 0.0d);
            rect.SetValue(Canvas.TopProperty, 0.0d);
            rect.Tag = this;

            bool inserted = false;
            for (int i = 0; i < (SilverlightGame.App.Current.RootVisual as Page).LayoutRoot.Children.Count; ++i)
            {
                Rectangle childRect = (SilverlightGame.App.Current.RootVisual as Page).LayoutRoot.Children[i] as Rectangle;
                if (childRect != null)
                {
                    GameObject gameObject = childRect.Tag as GameObject;
                    if (gameObject != null)
                    {
                        if (gameObject.ZLayer > zLayer)
                        {
                            (SilverlightGame.App.Current.RootVisual as Page).LayoutRoot.Children.Insert(i, rect);
                            inserted = true;
                            break;
                        }
                    }
                }
            }

            if (!inserted)
                (SilverlightGame.App.Current.RootVisual as Page).LayoutRoot.Children.Add(rect);
          
            prepareImage(image);
        }

        public override void shutdown()
        {
            (SilverlightGame.App.Current.RootVisual as Page).LayoutRoot.Children.Remove(rect);
            rect = null;
            imageBrush = null;
            base.shutdown();
        }

        virtual protected void prepareImage(string image)
        {
            imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
            imageBrush.ImageSource = ResourceHelper.GetBitmap(image);
            rect.Fill = imageBrush;
        }

        protected void offscreenCheck()
        {
            if (Position.X > (SilverlightGame.App.Current.RootVisual as Page).Width ||
                Position.X < -dimensions.X ||
                Position.Y > (SilverlightGame.App.Current.RootVisual as Page).Height ||
                Position.Y < -dimensions.Y)
            {
                shutdown();
            }

            // resize the rectangles so the game object doesn't appear to go off the screen
            if (Position.Y > (SilverlightGame.App.Current.RootVisual as Page).Height - dimensions.Y)
            {
                double height = (SilverlightGame.App.Current.RootVisual as Page).Height - Position.Y;
                if (height <= 0)
                    shutdown();
                else
                    rect.Height = height;
            }

            if (Position.X > (SilverlightGame.App.Current.RootVisual as Page).Width - dimensions.X)
            {
                double width = (SilverlightGame.App.Current.RootVisual as Page).Width - Position.X;    
                if (width <= 0)
                    shutdown();
                else
                    rect.Width = width;
            }
        }
    }
}