namespace Kit.Helpers.Repository.Xml
{
    public enum UpdateStatuses { Ok = 1, DocumentRowVersionNotChecked = 2, RowVersionNotChecked = 3, Cancel = 4 }

    public abstract class EntityXmlRepository<TKey>
    {
        protected string _elementName;
        protected string _path = "{0}";
        protected string _rootElementName;

        public EntityXmlRepository(string elementName)
        {
            _elementName = elementName;
        }

        public EntityXmlRepository(string path, string elementName)
        {
            _elementName = elementName;
            _path = path;
        }

        public EntityXmlRepository(string rootElementName, string path, string elementName)
        {
            _elementName = elementName;
            _rootElementName = rootElementName;
            _path = path;
        }
    }

    public interface IXmlFilter<TContentKey, TKey>
    {
        TKey GetRootKey();
        TContentKey GetXmlContentKey();
    }

    public class XmlFilter : IXmlFilter<int, Guid>
    {
        public int RootXml { get; set; }
        public Guid RootKey { get; set; }
        public Guid? Id { get; set; }
        public int GetXmlContentKey()
        {
            return this.RootXml;
        }
        public Guid GetRootKey()
        {
            return RootKey;
        }
    }

    public class XmlFilterInt : IXmlFilter<int, int>
    {
        public int RootXml { get; set; }
        public int RootKey { get; set; }
        public int? Id { get; set; }
        public int GetXmlContentKey()
        {
            return this.RootXml;
        }
        public int GetRootKey()
        {
            return RootKey;
        }
    }

    public interface IRootXmlKey<TKey>
    {
        TKey RootId { get; }
    }



    public interface IContentXmlKey<TKey>
    {
        TKey GetContentKey { get; }
    }

    public abstract class XmlSimpleClass<TKey> : IPersistentKey<TKey>, IPersistentXmlKey<TKey>
    {
        public TKey Id { get; set; }
        public TKey ParentId { get; set; }
        public string ElementNameParent { get; set; }
        public string ElementName { get; set; }
        public int RowVersion { get; set; }
        public TKey PersistentKey
        {
            get { return this.Id; }
        }
        public TKey PersistentXmlRootKey
        {
            get { return this.ParentId; }
        }
    }

    public abstract class XmlSimpleClass : XmlSimpleClass<Guid> { }
    public abstract class XmlFilterSimpleDb : IXmlSimpleFilter<int, Guid>
    {
        public int RootXml { get; set; }
        public Guid? Id { get; set; }
        public ISelectContext SelectContext { get; set; }

        public virtual int GetXmlContentKey()
        {
            return this.RootXml;
        }

        public Guid GetKey()
        {
            return Id ?? Guid.Empty;
        }
    }
    public abstract class XmlFilterSimple<TKey, TContentKey> : IXmlSimpleFilter<TContentKey, TKey>
        where TKey: struct
    {
        public TContentKey RootXml { get; set; }
        public TKey? Id { get; set; }
        public ISelectContext SelectContext { get; set; }

        public virtual TContentKey GetXmlContentKey()
        {
            return this.RootXml;
        }

        public TKey GetKey()
        {
            return Id ?? default;
        }
    }

    public abstract class XmlFilterComplex<TKey, TRootKey, TContentKey> : XmlFilterSimple<TKey, TContentKey>, IXmlComplexFilter<TContentKey, TRootKey, TKey>
        where TKey: struct
        where TRootKey : struct
    {
        public TRootKey RootKey { get; set; }

        public virtual TRootKey RootKeyId()
        {
            return RootKey;
        }
    }
}
