using System.Drawing;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
using NodeCanvas.Editor;
#endif
using UnityEditor;

namespace Reset {
	[Category("Reset")]
	[Description("Creates an environment observer unattached from the player, such as for checking from the Camera or another arbitray location.")]
	public class CheckGenericObserver : ConditionTask<Transform>{
		[Space(5)]
		public BBParameter<EnvironmentObserver.CastType> castType;

		public BBParameter<float> length;
		public BBParameter<Vector3> direction;
		public BBParameter<Vector3> offset;
		public BBParameter<LayerMask> ignoreLayers;
		
		public BBParameter<float> width;
		
		public BBParameter<Vector3> size;
		
		public BBParameter<Vector3> rotation;

		public bool drawGizmos;
		public bool drawGizmosOnlyWhenActive;
		
		private EnvironmentObserver observer;

		public BBParameter<RaycastHit> outputHit;

#if UNITY_EDITOR
		protected override void OnTaskInspectorGUI(){
			BBParameterEditor.ParameterField("Cast Type", castType);
			
			BBParameterEditor.ParameterField("Length", length);
			BBParameterEditor.ParameterField("Direction", direction);
			BBParameterEditor.ParameterField("Offset", offset);
			BBParameterEditor.ParameterField("Ignore Layers", ignoreLayers);

			if (castType.value == EnvironmentObserver.CastType.SphereCast || castType.value == EnvironmentObserver.CastType.SphereOverlap) { 
				BBParameterEditor.ParameterField("Width", width);
			}

			if (castType.value == EnvironmentObserver.CastType.BoxCast || castType.value == EnvironmentObserver.CastType.BoxOverlap) {
				BBParameterEditor.ParameterField("Size", size);
			}
			
			if (castType.value != EnvironmentObserver.CastType.Ray) {
				BBParameterEditor.ParameterField("Rotation", rotation);
			}
			
			drawGizmos = EditorGUILayout.Toggle("Draw Gizmos", drawGizmos);
			
			if (drawGizmos) {
				drawGizmosOnlyWhenActive = EditorGUILayout.Toggle("Draw Gizmos Only When Active", drawGizmosOnlyWhenActive);
			}

			BBParameterEditor.ParameterField("Output Hit", outputHit);
		}
#endif
		
		//Return null if init was successfull. Return an error string otherwise
		protected override string OnInit(){
			return null;
		}

		//Called whenever the condition gets enabled.
		protected override void OnEnable() {
			observer = new EnvironmentObserver(){
				castType = castType.value,
				active = true,
				
				length = length.value,
				direction = direction.value,
				offset = offset.value,
				ignoreLayers = ignoreLayers.value,
				
				width = width.value,
				
				size = size.value,
				rotation = rotation.value
			};
		}

		//Called whenever the condition gets disabled.
		protected override void OnDisable() {
			
		}

		//Called once per frame while the condition is active.
		//Return whether the condition is success or failure.
		protected override bool OnCheck() {
			bool check = observer.Evaluate(agent.gameObject);
			
			if (drawGizmos) {
				observer.DrawObserverGizmo(agent.gameObject, true);
			}

			if (check && !outputHit.isNoneOrNull) {
				outputHit.value = observer.hit;
			}
			return check;
		}
	}
}