﻿/*****************************************************************************
*       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.Globalization;
using System.Windows.Controls;
using MapInfo.MiPro.Samples.LayoutTemplate.Properties;

namespace MapInfo.MiPro.Samples.LayoutTemplate.ValidationRule
{
	public class ChooseLocationFieldValidationRule : System.Windows.Controls.ValidationRule
	{
		/// <summary>
		/// Gets or sets the error message.
		/// </summary>
		/// <value>
		/// The error message.
		/// </value>
		public string ErrorMessage { get; set; } = string.Empty;

		/// <summary>
		/// Performs validation checks on a value.
		/// </summary>
		/// <param name="value">The value from the binding target to check.</param>
		/// <param name="cultureInfo">The culture to use in this rule.</param>
		/// <returns>
		/// A <see cref="T:System.Windows.Controls.ValidationResult" /> object.
		/// </returns>
		public override ValidationResult Validate(object value, CultureInfo cultureInfo)
		{
			var str = value as string;
			if (str == null)
			{
				return new ValidationResult(false, ErrorMessage);
			}
			// don't have validation, if string empty
			if (str == string.Empty)
			{
				return new ValidationResult(true, null);
			}
			if (str == Resource.TemplateGalleryMyTemplates_Label 
				|| str == Resource.TemplateGalleryShared_Label
				|| str == Resource.Template_SaveOption_ExportAsZipFile)
			{
				return new ValidationResult(true, null); 
			}
			return new ValidationResult(false, ErrorMessage);
		}
	}
	/// <summary>
	/// Required field validation rule
	/// </summary>
	/// <seealso cref="System.Windows.Controls.ValidationRule" />
	public class RequiredFieldValidationRule : System.Windows.Controls.ValidationRule
	{
		/// <summary>
		/// Gets or sets the error message.
		/// </summary>
		/// <value>
		/// The error message.
		/// </value>
		public string ErrorMessage { get; set; } = string.Empty;

		/// <summary>
		/// Performs validation checks on a value.
		/// </summary>
		/// <param name="value">The value from the binding target to check.</param>
		/// <param name="cultureInfo">The culture to use in this rule.</param>
		/// <returns>
		/// A <see cref="T:System.Windows.Controls.ValidationResult" /> object.
		/// </returns>
		public override ValidationResult Validate(object value, CultureInfo cultureInfo)
		{
			var str = value as string;

			if (string.IsNullOrEmpty(str))
			{
				return new ValidationResult(false, ErrorMessage);
			}

			return new ValidationResult(true, null);
		}
	}
}
