﻿using System;
using System.Drawing;
using System.IO;
using OKHOSTING.UI.Controls;

namespace OKHOSTING.UI.Net4.WPF.Controls
{
	public class Image : System.Windows.Controls.Image, IImage
	{
		Uri _Uri;

		/// <summary>
		/// Loads the image from a Url
		/// <para xml:lang="es">
		/// Carga la imagen desde un Url.
		/// </para>
		/// </summary>
		public Uri Uri
		{
			get
			{
				return _Uri;
			}
			set
			{
				_Uri = value;

				if (_Uri == null)
				{
					Source = null;
					return;
				}

				var bitmap = new System.Windows.Media.Imaging.BitmapImage(_Uri);
				Source = bitmap;
			}
		}

		byte[] _Bytes;

		/// <summary>
		/// Loads the image from an array of bytes
		/// <para xml:lang="es">
		/// Carga la imagen desde un arreglo de bytes
		/// </para>
		/// </summary>
		public byte[] Bytes
		{
			get
			{
				return _Bytes;
			}
			set
			{
				_Bytes = value;

				if (_Bytes == null)
				{
					Source = null;
					return;
				}

				using (var memory = new MemoryStream(_Bytes))
				{
					var bitmap = new System.Windows.Media.Imaging.BitmapImage();
					bitmap.BeginInit();
					bitmap.StreamSource = memory;
					bitmap.EndInit();
					
					Source = bitmap;
				}
			}
		}

		public void Dispose()
		{
		}

		#region IControl

		/// <summary>
		/// Unique Id
		/// </summary>
		Guid IControl.Id { get; set; }

		bool IControl.Visible
		{
			get
			{
				return base.Visibility == System.Windows.Visibility.Visible;
			}
			set
			{
				if (value)
				{
					base.Visibility = System.Windows.Visibility.Visible;
				}
				else
				{
					base.Visibility = System.Windows.Visibility.Hidden;
				}
			}
		}

		bool IControl.Enabled
		{
			get
			{
				return base.IsEnabled;
			}
			set
			{
				base.IsEnabled = value;
			}
		}

		double? IControl.Width
		{
			get
			{
				return base.Width;
			}
			set
			{
				if (value.HasValue)
				{
					base.Width = value.Value;
				}
			}
		}

		double? IControl.Height
		{
			get
			{
				return base.Height;
			}
			set
			{
				if (value.HasValue)
				{
					base.Height = value.Value;
				}
			}
		}

		Thickness IControl.Margin
		{
			get
			{
				return Platform.Parse(base.Margin);
			}
			set
			{
				base.Margin = Platform.Parse(value);
			}
		}

		/// <summary>
		/// Space that this control will set between its content and its border
		/// <para xml:lang="es">
		/// Espacio que este control se establecerá entre su contenido y su borde
		/// </para>
		/// </summary>
		Thickness IControl.Padding
		{
			get;
			set;
		}

		Color IControl.BackgroundColor
		{
			get
			{
				return Color.Transparent;
				//throw new NotImplementedException();
				//return Platform.Parse(((System.Windows.Media.SolidColorBrush) base.Background).Color);
			}
			set
			{
				//base.Background = new System.Windows.Media.SolidColorBrush(Platform.Parse(value));
			}
		}

		Color IControl.BorderColor
		{
			get; set;
		}

		Thickness IControl.BorderWidth
		{
			get; set;
		}

		HorizontalAlignment IControl.HorizontalAlignment
		{
			get
			{
				return Platform.Parse(base.HorizontalAlignment);
			}
			set
			{
				base.HorizontalAlignment = Platform.Parse(value);
			}
		}

		VerticalAlignment IControl.VerticalAlignment
		{
			get
			{
				return Platform.Parse(base.VerticalAlignment);
			}
			set
			{
				base.VerticalAlignment = Platform.Parse(value);
			}
		}

		/// <summary>
		/// Gets or sets a list of classes that define a control's style. 
		/// Exactly the same concept as in CSS. 
		/// </summary>
		string IControl.CssClass { get; set; }

		/// <summary>
		/// Control that contains this control, like a grid, or stack
		/// </summary>
		IControl IControl.Parent
		{
			get
			{
				return (IControl) base.Parent;
			}
		}

		object ICloneable.Clone()
		{
			return MemberwiseClone();
		}

		#endregion
	}
}