﻿/*****************************************************************************
*       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.Collections.Generic;
using System.IO;
using System.Net.Mime;
using System.Windows;
using System.Windows.Input;
using MapInfo.MiPro.Samples.LayoutTemplate.Command;
using MapInfo.MiPro.Samples.LayoutTemplate.Properties;
using MapInfo.Types;

namespace MapInfo.MiPro.Samples.LayoutTemplate.ViewModel
{
	/// <summary>
	/// ViewModel corresponding to SaveTemplateDialog
	/// </summary>
	/// <seealso cref="MapInfo.MiPro.Samples.LayoutTemplate.ViewModel.ViewModelBase" />
	public class SaveTemplateDialogViewModel : ViewModelBase
	{
		private string _name;
		private string _fileName;
		private string _description;
		private ICommand _saveCommand;
		private bool _isClose;
		private ICommand _cancelCommand;
		private ICommand _helpCommand;
		private readonly IMapInfoPro _application;
		private readonly bool _showChooseLocationOption;
		private bool _isOk;

		public bool IsOk
		{
			get { return _isOk; }
			set { value = _isOk; }
		}

		public SaveTemplateDialogViewModel(IMapInfoPro application, bool showChooseLocationOption)
		{
			_showChooseLocationOption = showChooseLocationOption;
			_application = application;
			_isOk = false;
		}

		/// <summary>
		/// "Save To" options list
		/// </summary>
		public List<string> SaveOptionList =>
			!String.IsNullOrEmpty(LayoutTemplateFileManager.Instance.SharedTemplateFolderPath)
				? new List<string>()
				{
					Resource.TemplateGalleryMyTemplates_Label,
					Resource.TemplateGalleryShared_Label,
					Resource.Template_SaveOption_ExportAsZipFile
				}
				: new List<string>()
				{
					Resource.TemplateGalleryMyTemplates_Label,
					Resource.Template_SaveOption_ExportAsZipFile
				};

		private String _selectedSaveOption;
		/// <summary>
		/// Selected Save option. return Empty if not visible
		/// </summary>
		public String SelectedSaveOption
		{
			get
			{
				return (ChooseLocationVisible != Visibility.Visible) ? String.Empty : _selectedSaveOption;
			}
			set { _selectedSaveOption = value; }
		}
		/// <summary>
		/// Selected save to type
		/// </summary>
		/// <returns>if shared selected then return Shared else MyTemplate</returns>
		public TemplateFile.TemplateTypes SelectedSaveOptionType()
		{
			if (_selectedSaveOption == Resource.TemplateGalleryShared_Label)
			{
				return TemplateFile.TemplateTypes.Shared;
			}
			return TemplateFile.TemplateTypes.MyTemplate;
		}
		/// <summary>
		/// "Save To" field visibility property 
		/// </summary>
		public Visibility ChooseLocationVisible => _showChooseLocationOption ? Visibility.Visible : Visibility.Collapsed;
			
		/// <summary>
		/// Gets or sets the name of template. It sets the file name removing all
		/// the invalid file charecters.
		/// </summary>
		/// <value>
		/// The name of file.
		/// </value>
		public string Name
		{
			get { return _name; }
			set
			{
				_name = value;
				// remove any invalid file name charactors
				var name =
					_name.Replace("\\", "")
						.Replace("/", "")
						.Replace(":", "")
						.Replace("*", "")
						.Replace("?", "")
						.Replace("\"", "")
						.Replace("<", "")
						.Replace(">", "")
						.Replace("|", "");

				string trimmedName = name.Trim().Trim('.');

				// remove any spaces and . from beginning and end
				while (name != trimmedName)
				{
					name = trimmedName;
					trimmedName = name.Trim().Trim('.');
				}

				_fileName = trimmedName;
				OnPropertyChanged();
			}
		}

		/// <summary>
		/// Gets or sets the name of the template file.
		/// </summary>
		/// <value>
		/// The name of the file.
		/// </value>
		public string FileName
		{
			get { return _fileName; }
			set
			{
				_fileName = value;
				OnPropertyChanged();
			}
		}

		/// <summary>
		/// Gets or sets the template description.
		/// </summary>
		/// <value>
		/// The template description.
		/// </value>
		public string Description
		{
			get { return _description; }
			set
			{
				_description = value;
				OnPropertyChanged();
			}
		}
		/// <summary>
		/// Get template folder path where related files are stored
		/// </summary>
		public string TemplateFolderPath
		{
			get
			{
				if (SelectedSaveOption == Resource.TemplateGalleryShared_Label)
				{
					return Path.Combine(LayoutTemplateFileManager.Instance.SharedTemplateFolderPath, FileName);
				}
				return Path.Combine(LayoutTemplateFileManager.Instance.MyTemplateFolderPath, FileName);
			}
		}
		/// <summary>
		/// Gets the full template file path.
		/// </summary>
		/// <value>
		/// The full file path.
		/// </value>
		public string FullFilePath
		{
			get
			{
				if (SelectedSaveOption == Resource.TemplateGalleryShared_Label)
				{
					return Path.Combine(LayoutTemplateFileManager.Instance.SharedTemplateFolderPath,
						String.Concat(_fileName, ".", Constant.TemplateFileExtension));

				}
				return Path.Combine(LayoutTemplateFileManager.Instance.MyTemplateFolderPath,
						String.Concat(_fileName, ".", Constant.TemplateFileExtension));
			}
		}

		/// <summary>
		/// Gets the full template file path.
		/// </summary>
		/// <value>
		/// The full file path.
		/// </value>
		public string FileNameWithExtension
		{
			get { return String.Concat(_fileName, ".", Constant.TemplateFileExtension); }
		}

		/// <summary>
		/// Gets or sets a value indicating whether to close the corresponding View.
		/// </summary>
		public bool IsClose
		{
			get { return _isClose; }
			set
			{
				_isClose = value;
				OnPropertyChanged();
			}
		}

		/// <summary>
		/// Gets the save template command.
		/// </summary>
		/// <value>
		/// The save command.
		/// </value>
		public ICommand SaveCommand => _saveCommand ?? (_saveCommand = new RelayCommand(OnSaveCommand));

		/// <summary>
		/// Gets the cancel command.
		/// </summary>
		/// <value>
		/// The cancel command.
		/// </value>
		public ICommand CancelCommand => _cancelCommand ?? (_cancelCommand = new RelayCommand(OnCancelCommand));

		/// <summary>
		/// Called when cancel command is executed.
		/// </summary>
		/// <param name="obj">The object.</param>
		private void OnCancelCommand(object obj)
		{
			
			_fileName = null;
			IsClose = true;
		}

		/// <summary>
		/// Called when save command is executed.
		/// </summary>
		/// <param name="obj">The object.</param>
		private void OnSaveCommand(object obj)
		{
			if (Name.Length > Constant.TemplateNameLength)
			{
				MessageBox.Show(string.Format(Resource.ErrorMessage_ErrorTemplateNameSize,Constant.TemplateNameLength), Resource.MB_IDS_ADDIN_NAME, MessageBoxButton.OK, MessageBoxImage.Information);
				return;
			}
			_isOk = true;
			IsClose = true;
		}
		public ICommand HelpCommand { get { return _helpCommand ?? (_helpCommand = new RelayCommand(OnHelpCommand)); } }
		/// <summary>
		/// Called when Help command is executed
		/// </summary>
		/// <param name="obj">Object</param>
		private void OnHelpCommand(object obj)
		{
			LayoutHelper.ShowHelp(_application);
		}
	}
}
