﻿/*****************************************************************************
*       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.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Markup;
using System.Xml;
using MapInfo.MiPro.Samples.LayoutScaleBarAddIn.Scalebars;
using MapInfo.MiPro.Samples.LayoutScaleBarAddIn.Views.Scalebars;

namespace MapInfo.MiPro.Samples.LayoutScaleBarAddIn
{
	public class ScaleBarFrameControlSerializer
	{
		public static bool Serialize(ScaleBarFrameControl addInCustomFrame, string fileFullName)
		{
			SerializeToXml(addInCustomFrame, fileFullName);
	
			return true;
		}

		public static ScaleBarFrameControlInfo Deserialize(string fileFullName)
		{
			return DeserializefromXml(fileFullName);

		}

		//Notice in this method, we are getting the 'ScaleBarFrameControlInfo' from the customFrame
		//Rather than serializing the frame directly.  This should be considered a 'best practice' for 
		//serialization when it comes to developing add-ins for MiPro.  While it is possible to serialize 
		//the entire frame object with the .NET serialization tools, you can run into issues if your XAML code 
		//employs any code-behind, has named objects, or a class employs any processing logic in setter methods that 
		//affect or change other public properties when set.  By creating simple classes that store just the properties necessary
		//to 'Reconstruct' the object when loading the custom frame back from the Workspace, the .NET serialization API can be used
		//quite effectively.  You are of course free to implement your own serialization/deserialization logic as well.
		private static void SerializeToXml(ScaleBarFrameControl addInCustomFrame, string fileFullName)
		{
			var sb = new StringBuilder();
			var writer = XmlWriter.Create(sb, new XmlWriterSettings
			{
				Indent = true,
				ConformanceLevel = ConformanceLevel.Fragment,
				OmitXmlDeclaration = true
			});
			var mgr = new XamlDesignerSerializationManager(writer) {XamlWriterMode = XamlWriterMode.Expression};

			var serializableInfo = addInCustomFrame.GetScaleBarFrameControlInfo();

			XamlWriter.Save(serializableInfo, mgr);

			using (TextWriter fileWriter = File.CreateText(fileFullName))
			{
				fileWriter.Write(sb.ToString());
			}
		}

		private static ScaleBarFrameControlInfo DeserializefromXml(string fileFullName)
		{
			ScaleBarFrameControlInfo frameControlInfo = null;
			try
			{
				using (TextReader fileReader = File.OpenText(fileFullName))
				{
					{
						string contents = fileReader.ReadToEnd();
						var stringReader = new StringReader(contents);
						var xmlReader = XmlReader.Create(stringReader);
						frameControlInfo = (ScaleBarFrameControlInfo)XamlReader.Load(xmlReader);
					}
				}
			}
			catch(Exception ex)
			{
				Debug.WriteLine($"There was an exception while Deserializing the Layout Scalebar Addin Custom Frame Contents: {ex.Message}");
				
			}
			return frameControlInfo;
		}



	}	

}
