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

namespace Decal.DecalControls
{
    [ComVisible(true)]
    public abstract class ControlBase : ILayer, IControl
    {
        protected Layer _site;
        private int _id;

        // ILayer
        public void LayerCreate(Layer pSite)
        {
            _site = pSite;
            var site = (ILayerSite)(object)pSite;
            _id = site.ID;
            OnCreate();
        }

        public void LayerDestroy()
        {
            OnDestroy();
            _site = null;
        }

        public tagRECT Position
        {
            get
            {
                if (_site != null)
                    return ((ILayerSite)(object)_site).Position;
                return default;
            }
            set
            {
                if (_site != null)
                    ((ILayerSite)(object)_site).Position = value;
            }
        }

        public void Invalidate()
        {
            if (_site != null)
                ((ILayerSite)(object)_site).Invalidate();
        }

        // IControl
        public void DestroyChild(int nIndex, ePositionType posType) { }
        public int ID => _id;
        public int ChildCount => (_site != null) ? ((ILayerSite)(object)_site).ChildCount : 0;
        public IControl Child => null;

        protected virtual void OnCreate() { }
        protected virtual void OnDestroy() { }
    }
}
