﻿/*****************************************************************************
*       Copyright 1985, 2020 Precisely.
*       All rights reserved.
*
*       This document contains unpublished, confidential, and proprietary
*       information of Precisely. No disclosure or use of any portion of
*       the contents of this document may be made without the express 
*       written consent of Precisely.
*****************************************************************************/
using System;
using System.AddIn.Pipeline;
using MapInfo.Events;
using MapInfo.MiPro.Samples.MapInfoDataAccessLibraryExamples.View;
using MapInfo.MiPro.Samples.MapInfoDataAccessLibraryExamples.ViewModel;
using MapInfo.Types;

// ReSharper disable CheckNamespace
namespace MapInfo.MiPro.Samples.MapInfoDataAccessLibraryExamples
// ReSharper restore CheckNamespace
{
	public class MapInfoDataAccessLibraryExamplesAddIn
	{
		private IMapInfoPro _mapInfoApplication;
		private SampleCodeItemsView _sampleCodeWindow;

		/// <summary>
		/// Instance MapInfoPro events interface.
		/// </summary>
		private IMapInfoEvents MapInfoEvent;

		public IWindowInfo MapInfoWindowDetails { get; set; }

		public IWindowInfo RunningMBApplications { get; set; }

		public void Initialize(IMapInfoPro mapInfoApplication, string mbxname)
		{
			//Always do this
			UriParser.Register(new GenericUriParser(GenericUriParserOptions.GenericAuthority), "pack", -1);

			_mapInfoApplication = mapInfoApplication;
			ThisApplication = _mapInfoApplication.GetMapBasicApplication(mbxname);

			MapInfoEvent = MapInfoEvents.InitializeEvents(_mapInfoApplication, ThisApplication);
			SubscribeToMapInfoEvents();

			CreateUserControl("SampleCode"); // window to try sample code
		}

		private void SubscribeToMapInfoEvents()
		{
			MapInfoEvent.PreviewWindowClosing += PreWindowClosedInMapInfoPro;
		}

		private void UnsubscribeToMapInfoEvents()
		{
			MapInfoEvent.PreviewWindowClosing -= PreWindowClosedInMapInfoPro;
		}

		private void PreWindowClosedInMapInfoPro(object sender, IWindowInfo e)
		{
			if (String.Compare(e.Name, "MapInfoDataAccessLibraryExamples", StringComparison.InvariantCultureIgnoreCase) == 0)
			{
				_sampleCodeWindow = null;
			}
			
		}

		public void OnEndApplication(object sender)
		{
			if (ThisApplication != null)
			{
				ThisApplication.EndApplication();
			}
		}

		private void CreateUserControl(object parameter)
		{
			var userControlType = parameter as String;

			if (userControlType == null) return;

			if (String.Compare(userControlType, "SampleCode", StringComparison.InvariantCultureIgnoreCase) == 0)
			{
				var bCreated = false;

				if (_sampleCodeWindow == null)
				{
					_sampleCodeWindow = new SampleCodeItemsView(_mapInfoApplication);
					bCreated = true;
				}

				var vm = _sampleCodeWindow.DataContext as SampleCodeItemViewModel;
				if (vm == null) return;

				if (bCreated)
				{
					var userControl =
						new NativeHandleContractInsulator(FrameworkElementAdapters.ViewToContractAdapter(_sampleCodeWindow))
						{
							Name = "MapInfoDataAccessLibraryExamples",
						};

					vm.WindowInfo =
						_mapInfoApplication.DockingManager.DockUserControl(userControl, "MapInfoDataAccessLibrary Examples", vm.Properties);
					_mapInfoApplication.DockingManager.SetDockingManagerProperty(vm.WindowInfo, "State", "Float");
					_mapInfoApplication.DockingManager.ActiveWindow = vm.WindowInfo;
				}
			}
		}

		public void ShowWindow()
		{
			CreateUserControl("SampleCode");
		}

		public void Unload()
		{
			if (_sampleCodeWindow != null)
			{
				var vm = _sampleCodeWindow.DataContext as SampleCodeItemViewModel;
				var winInfo = vm?.WindowInfo;

				if (winInfo != null)
				{
					winInfo.Close();
					_sampleCodeWindow = null;
				}
			}

			UnsubscribeToMapInfoEvents();
		}

		public IMapBasicApplication ThisApplication { get; set; }

		public static Uri PathToUri(string uri)
		{
			try
			{
				return new Uri(uri, UriKind.RelativeOrAbsolute);
			}
			catch (Exception)
			{
				return null;
			}
		}
	}
}
