using System.Runtime.InteropServices;
using Decal.Interop.Controls;
using Decal.Interop.Core;
using Decal.Interop.Inject;

namespace Decal.DecalControls
{
    [ComVisible(true)]
    [Guid("CD6556CD-F8D9-4CB0-AB7C-7C33058294E4")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces("Decal.Interop.Inject.IControlEvents\0\0")]
    [ProgId("DecalControls.FixedLayout")]
    public class FixedLayoutImpl : ControlBase, ILayout
    {
        public event IControlEvents_DestroyEventHandler Destroy;

        private IImageCache _background;

        public IImageCache Background { get => _background; set { _background = value; Invalidate(); } }

        public void CreateChild(ref LayerParams @params, ILayer pSink)
        {
            if (_site != null)
                ((ILayerSite)(object)_site).CreateChild(ref @params, pSink);
        }

        public void PositionChild(int nID, ref tagRECT prcNew) { }

        protected override void OnDestroy() => Destroy?.Invoke(ID);
    }

    [ComVisible(true)]
    [Guid("CA121762-31BB-4073-8597-33BAB4BDCAA3")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces("Decal.Interop.Inject.IControlEvents\0\0")]
    [ProgId("DecalControls.BorderLayout")]
    public class BorderLayoutImpl : ControlBase, ILayout
    {
        public event IControlEvents_DestroyEventHandler Destroy;

        private IImageCache _background;

        public IImageCache Background { get => _background; set { _background = value; Invalidate(); } }

        public void CreateEdge(eBorderEdge eEdge, int nSize, ILayer pSink) { }
        public void CreateCenter(ILayer pSink) { }

        protected override void OnDestroy() => Destroy?.Invoke(ID);
    }

    [ComVisible(true)]
    [Guid("5AD04D45-D4BF-4729-8A2E-5D37CF726CAA")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces("Decal.Interop.Inject.IControlEvents\0\0")]
    [ProgId("DecalControls.PageLayout")]
    public class PageLayoutImpl : ControlBase, IPageLayout
    {
        public event IControlEvents_DestroyEventHandler Destroy;

        private IImageCache _background;
        private readonly System.Collections.Generic.List<ILayer> _pages = new System.Collections.Generic.List<ILayer>();
        private int _active;

        // ILayout
        public IImageCache Background { get => _background; set { _background = value; Invalidate(); } }

        // IPageLayoutDisp
        public int Active
        {
            get => _active;
            set
            {
                if (value >= 0 && value < _pages.Count)
                {
                    _active = value;
                    Invalidate();
                }
            }
        }

        public int Count => _pages.Count;

        // IPageLayout
        public void CreatePage(ILayer pChild)
        {
            _pages.Add(pChild);
            Invalidate();
        }

        protected override void OnDestroy() => Destroy?.Invoke(ID);
    }
}
