using CPF.Mac.Foundation;
using CPF.Mac.ObjCRuntime;
using System;
using System.ComponentModel;

namespace CPF.Mac.AppKit
{
	[Register("NSActionCell", true)]
	public class NSActionCell : NSCell
	{
		private static readonly IntPtr selTargetHandle = Selector.GetHandle("target");

		private static readonly IntPtr selSetTarget_Handle = Selector.GetHandle("setTarget:");

		private static readonly IntPtr selActionHandle = Selector.GetHandle("action");

		private static readonly IntPtr selSetAction_Handle = Selector.GetHandle("setAction:");

		private static readonly IntPtr selTagHandle = Selector.GetHandle("tag");

		private static readonly IntPtr selSetTag_Handle = Selector.GetHandle("setTag:");

		private static readonly IntPtr selInitTextCell_Handle = Selector.GetHandle("initTextCell:");

		private static readonly IntPtr selInitImageCell_Handle = Selector.GetHandle("initImageCell:");

		private static readonly IntPtr class_ptr = Class.GetHandle("NSActionCell");

		private object __mt_Target_var;

		public override IntPtr ClassHandle => class_ptr;

		public new virtual NSObject Target
		{
			[Export("target")]
			get
			{
				NSApplication.EnsureUIThread();
				return (NSObject)(__mt_Target_var = ((!IsDirectBinding) ? Runtime.GetNSObject(Messaging.IntPtr_objc_msgSendSuper(base.SuperHandle, selTargetHandle)) : Runtime.GetNSObject(Messaging.IntPtr_objc_msgSend(base.Handle, selTargetHandle))));
			}
			[Export("setTarget:")]
			set
			{
				NSApplication.EnsureUIThread();
				if (IsDirectBinding)
				{
					Messaging.void_objc_msgSend_IntPtr(base.Handle, selSetTarget_Handle, value?.Handle ?? IntPtr.Zero);
				}
				else
				{
					Messaging.void_objc_msgSendSuper_IntPtr(base.SuperHandle, selSetTarget_Handle, value?.Handle ?? IntPtr.Zero);
				}
				__mt_Target_var = value;
			}
		}

		public new virtual Selector Action
		{
			[Export("action")]
			get
			{
				NSApplication.EnsureUIThread();
				if (IsDirectBinding)
				{
					return Selector.FromHandle(Messaging.IntPtr_objc_msgSend(base.Handle, selActionHandle));
				}
				return Selector.FromHandle(Messaging.IntPtr_objc_msgSendSuper(base.SuperHandle, selActionHandle));
			}
			[Export("setAction:")]
			set
			{
				NSApplication.EnsureUIThread();
				if (IsDirectBinding)
				{
					Messaging.void_objc_msgSend_IntPtr(base.Handle, selSetAction_Handle, (value == null) ? IntPtr.Zero : value.Handle);
				}
				else
				{
					Messaging.void_objc_msgSendSuper_IntPtr(base.SuperHandle, selSetAction_Handle, (value == null) ? IntPtr.Zero : value.Handle);
				}
			}
		}

		public new virtual long Tag
		{
			[Export("tag")]
			get
			{
				NSApplication.EnsureUIThread();
				if (IsDirectBinding)
				{
					return Messaging.Int64_objc_msgSend(base.Handle, selTagHandle);
				}
				return Messaging.Int64_objc_msgSendSuper(base.SuperHandle, selTagHandle);
			}
			[Export("setTag:")]
			set
			{
				NSApplication.EnsureUIThread();
				if (IsDirectBinding)
				{
					Messaging.void_objc_msgSend_Int64(base.Handle, selSetTag_Handle, value);
				}
				else
				{
					Messaging.void_objc_msgSendSuper_Int64(base.SuperHandle, selSetTag_Handle, value);
				}
			}
		}

		public event EventHandler Activated
		{
			add
			{
				Target = ActionDispatcher.SetupAction(Target, value);
				Action = ActionDispatcher.Action;
			}
			remove
			{
				ActionDispatcher.RemoveAction(Target, value);
			}
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		[Export("init")]
		public NSActionCell()
			: base(NSObjectFlag.Empty)
		{
			if (IsDirectBinding)
			{
				base.Handle = Messaging.IntPtr_objc_msgSend(base.Handle, Selector.Init);
			}
			else
			{
				base.Handle = Messaging.IntPtr_objc_msgSendSuper(base.SuperHandle, Selector.Init);
			}
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		[Export("initWithCoder:")]
		public NSActionCell(NSCoder coder)
			: base(NSObjectFlag.Empty)
		{
			if (IsDirectBinding)
			{
				base.Handle = Messaging.IntPtr_objc_msgSend_IntPtr(base.Handle, Selector.InitWithCoder, coder.Handle);
			}
			else
			{
				base.Handle = Messaging.IntPtr_objc_msgSendSuper_IntPtr(base.SuperHandle, Selector.InitWithCoder, coder.Handle);
			}
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		public NSActionCell(NSObjectFlag t)
			: base(t)
		{
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		public NSActionCell(IntPtr handle)
			: base(handle)
		{
		}

		[Export("initTextCell:")]
		public NSActionCell(string aString)
			: base(NSObjectFlag.Empty)
		{
			NSApplication.EnsureUIThread();
			if (aString == null)
			{
				throw new ArgumentNullException("aString");
			}
			IntPtr intPtr = NSString.CreateNative(aString);
			if (IsDirectBinding)
			{
				base.Handle = Messaging.IntPtr_objc_msgSend_IntPtr(base.Handle, selInitTextCell_Handle, intPtr);
			}
			else
			{
				base.Handle = Messaging.IntPtr_objc_msgSendSuper_IntPtr(base.SuperHandle, selInitTextCell_Handle, intPtr);
			}
			NSString.ReleaseNative(intPtr);
		}

		[Export("initImageCell:")]
		public NSActionCell(NSImage image)
			: base(NSObjectFlag.Empty)
		{
			NSApplication.EnsureUIThread();
			if (image == null)
			{
				throw new ArgumentNullException("image");
			}
			if (IsDirectBinding)
			{
				base.Handle = Messaging.IntPtr_objc_msgSend_IntPtr(base.Handle, selInitImageCell_Handle, image.Handle);
			}
			else
			{
				base.Handle = Messaging.IntPtr_objc_msgSendSuper_IntPtr(base.SuperHandle, selInitImageCell_Handle, image.Handle);
			}
		}

		protected override void Dispose(bool disposing)
		{
			base.Dispose(disposing);
			if (base.Handle == IntPtr.Zero)
			{
				__mt_Target_var = null;
			}
		}
	}
}
