namespace ZelWiki.Engine.Library
{
    public class PageReference
    {
        public PageReference()
        {
            Name = string.Empty;
            Namespace = string.Empty;
            Navigation = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string Namespace { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string Navigation { get; set; }


        public override bool Equals(object? obj)
        {
            return obj is PageReference other
                   && string.Equals(Navigation, other.Navigation, StringComparison.OrdinalIgnoreCase);
        }

        public override int GetHashCode()
        {
            return Navigation.GetHashCode();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="navigation"></param>
        /// <exception cref="Exception"></exception>
        public PageReference(string name, string navigation)
        {
            var parts = name.Split("::");

            if (parts.Length == 1)
            {
                Name = parts[0].Trim();
            }
            else if (parts.Length == 2)
            {
                Namespace = parts[0].Trim();
                Name = parts[1].Trim();
            }
            else
            {
                throw new Exception($"页面名称无效: {name}");
            }

            Navigation = navigation;
        }
    }
}