﻿using System;
using System.Windows;
using CustomFrameHelpers;
using MapInfo.Types;

namespace MapInfo.MiPro.Samples.SimpleInteractiveCustomFrameExample
{
	public class InteractiveCustomFrameControl : CrossAppDomainCustomFrame, IAddInCustomFrame
	{
		[NonSerialized] public ICustomFrameControl WrapperControl;
		private bool _active;
		private DimensionedValue _frameHeight;
		private DimensionedValue _frameWidth;
		private double _scale;
		private DimensionedValue _xCoord;
		private DimensionedValue _yCoord;
		private string _brushClause;
		private string _borderBrushClause;
		private int _zIndex;
		private string _frameName;
		
		public FrameworkElement ControlContent { get; set; }

		public InteractiveCustomFrameControl()
		{
			BrushClause = "";
			PenBorderClause = "";
			FrameName = "InteractiveCustomFrame";
			XCoord = new DimensionedValue(0, PaperUnits.Inches);
			YCoord = new DimensionedValue(0, PaperUnits.Inches);
		}													

		public bool CanResize
		{
			get { return true; }
		}

		public bool CanActivate
		{
			get { return true; }
		}

		public bool Active
		{
			get { return _active; }
			set
			{
				if (_active != value)
				{
					if (value == false && WrapperControl != null && ControlContent != null)
					{
						WrapperControl.SetInactiveContent(new InactiveLayoutContent(ControlContent));
					}

					_active = value;
				}
			}
		}

		public bool PreserveAspect
		{
			get { return true; }
		}

		public bool CanRemoveContent
		{
			get { return false; }
		}

		public DimensionedValue FrameHeight
		{
			get { return _frameHeight; }
			set
			{
				if (value != null)
				{
					_frameHeight = UnitConversion.ConvertUnits(value, PaperUnits.Pixels);
				}
				else
				{
					_frameHeight = new DimensionedValue(0, PaperUnits.Pixels);
				}

				var content = ((InteractiveCustomFrameControlContent) ControlContent);

				if (content != null)
				{
					content.FrameHeight = _frameHeight.Value;
					content.UpdateDimensions();
					content.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
					content.UpdateLayout();
					if (WrapperControl != null)
					{
						WrapperControl.SetInactiveContent(new InactiveLayoutContent(ControlContent));
					}
				}
			}
		}

		public DimensionedValue FrameWidth
		{
			get { return _frameWidth; }
			set
			{
				if (value != null)
				{
					_frameWidth = UnitConversion.ConvertUnits(value, PaperUnits.Pixels);
				}
				else
				{
					_frameWidth = new DimensionedValue(0, PaperUnits.Pixels);
				}

				var content = ((InteractiveCustomFrameControlContent) ControlContent);

				if (content != null)
				{
					content.FrameWidth = _frameWidth.Value;
					content.UpdateDimensions();
					content.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
					content.UpdateLayout();
					if (WrapperControl != null)
					{
						WrapperControl.SetInactiveContent(new InactiveLayoutContent(ControlContent));
					}
				}
			}
		}

		public DimensionedValue XCoord
		{
			get { return _xCoord; }
			set
			{
				if (value != null)
				{
					_xCoord = UnitConversion.ConvertUnits(value, PaperUnits.Pixels);
				}
				else
				{
					XCoord = new DimensionedValue(0, PaperUnits.Pixels);
				}
			}
		}

		public DimensionedValue YCoord
		{
			get { return _yCoord; }
			set
			{
				if (value != null)
				{
					_yCoord = UnitConversion.ConvertUnits(value, PaperUnits.Pixels);
				}
				else
				{
					YCoord = new DimensionedValue(0, PaperUnits.Pixels);
				}
			}
		}

		public double LayoutPageScale
		{
			get { return _scale; }
			set
			{
				_scale = value;
				var content = ((InteractiveCustomFrameControlContent) ControlContent);

				if (content != null)
				{
					content.Scale = _scale;
					content.UpdateDimensions();
					content.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
					content.UpdateLayout();
					if (WrapperControl != null)
					{
						WrapperControl.SetInactiveContent(new InactiveLayoutContent(ControlContent));
					}
				}
			}
		}

		public string BrushClause
		{
			get { return _brushClause; }
			set { _brushClause = value; }
		}

		public string PenBorderClause
		{
			get { return _borderBrushClause; }
			set { _borderBrushClause = value; }
		}

		public int ZIndex
		{
			get { return _zIndex; }
			set { _zIndex = value; }
		}

		public string AddinMBXFileName
		{
			get { return Helper.AssemblyDirectory + "\\SimpleInteractiveCustomFrameExample.mbx"; }
		}

		public string FrameName
		{
			get { return _frameName; }
			set { _frameName = value; }
		}

		/// <summary>
		/// This property of the interface is not currently used.
		/// </summary>
		public string Name
		{
			get { throw new NotImplementedException(); }
		} 

		public InactiveLayoutContent PrintContent(double printScale)
		{
			double tempScale = LayoutPageScale;
			LayoutPageScale = printScale;
			var printContent = new InactiveLayoutContent(ControlContent);
			LayoutPageScale = tempScale;
			return printContent;
		}

		public void Serialize(string fileName)
		{
			AddInCustomFrameSerializer.Serialize(this, fileName);
		}

		public new virtual void Dispose()
		{
			WrapperControl.Dispose();
		}
	}
}