using Cryville.Crtr.Components;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Cryville.Crtr.Event {
	public abstract class ContainerHandler : IDisposable {
		/// <summary>
		/// Prehandling <see cref="ContainerState"/>, prehandling the events.
		/// </summary>
		public ContainerState ps;

		/// <summary>
		/// Backward <see cref="ContainerState"/>, disposing events at the backward clipping plane, firing anchor state and temporary state.
		/// </summary>
		public ContainerState bs;

		/// <summary>
		/// Current <see cref="ContainerState"/>, handling events occuring at the current time.
		/// </summary>
		public ContainerState cs;

		/// <summary>
		/// Anchor <see cref="ContainerState"/>, computing the start position of the temporary state.
		/// </summary>
		public ContainerState ns;

		/// <summary>
		/// Temporary <see cref="ContainerState"/>, rendering events between the clipping planes.
		/// </summary>
		public ContainerState ts;

		/// <summary>
		/// <see cref="GameObject"/> group, the <see cref="Transform"/> containing all the generated elements in the <see cref="ContainerHandler"/>.
		/// </summary>
		public Transform gogroup;

		public readonly Dictionary<string, Anchor> Anchors = new Dictionary<string, Anchor>();
		public Transform a_head;
		public Transform a_tail;
		public Vector3 Position {
			get;
			protected set;
		}
		public Quaternion Rotation {
			get;
			protected set;
		}
		public bool Alive {
			get;
			private set;
		}
		public bool Awoken {
			get;
			private set;
		}
		public bool Disposed {
			get;
			private set;
		}

		public EventContainer Container {
			get { return cs.Container; }
		}
		
		public ContainerHandler() { }
		public abstract string TypeName {
			get;
		}
		public virtual void PreInit() {
			gogroup = new GameObject(TypeName + ":" + Container.GetHashCode().ToString()).transform;
			if (cs.Parent != null)
				gogroup.SetParent(cs.Parent.Handler.gogroup, false);
			a_head = new GameObject("::head").transform;
			a_head.SetParent(gogroup, false);
			Anchors.Add("head", new Anchor() { Transform = a_head });
			a_tail = new GameObject("::tail").transform;
			a_tail.SetParent(gogroup, false);
			Anchors.Add("tail", new Anchor() { Transform = a_tail });
		}
		/// <summary>
		/// Called upon initialization of <see cref="cs" />.
		/// </summary>
		public virtual void Init() {
			cs.skinContainer.MatchStatic(cs);
			foreach (var i in gogroup.GetComponentsInChildren<SkinComponent>())
				i.Init();
		}
		public virtual void PostInit() {
			gogroup.gameObject.SetActive(false);
		}
		public virtual void Dispose() {
			if (Disposed) return;
			Disposed = true;
			if (gogroup)
				GameObject.Destroy(gogroup.gameObject);
				// gogroup.gameObject.SetActive(false);
			Alive = false;
		}
		protected virtual void PreAwake(ContainerState s) {
			if (gogroup) gogroup.gameObject.SetActive(true);
			Awoken = true; Alive = true;
			OpenAnchor("head");
		}
		protected virtual void Awake(ContainerState s) {
			CloseAnchor("head");
		}
		protected virtual void GetPosition(ContainerState s) { }
		public virtual void StartUpdate(ContainerState s) {
			if (s.CloneType >= 2 && s.CloneType < 16) {
				PreAwake(s);
				Awake(s);
			}
		}
		public virtual void Update(ContainerState s, StampedEvent ev) {
			bool flag = !Awoken && s.CloneType >= 2 && s.CloneType < 16;
			if (flag) PreAwake(s);
			if (Awoken && s.CloneType <= 2) if (gogroup) cs.skinContainer.MatchDynamic(s);
			if (flag) Awake(s);
		}
		public virtual void ExUpdate(ContainerState s, StampedEvent ev) { }
		public virtual void MotionUpdate(byte ct, Chart.Motion ev) { }
		public virtual void EndUpdate(ContainerState s) {
			if (s.CloneType < 16) {
				Awoken = false;
				if (gogroup && s.CloneType <= 2) cs.skinContainer.MatchDynamic(s);
			}
		}
		public virtual void Anchor() { }
		protected void OpenAnchor(string name) {
			if (Anchors.ContainsKey(name)) Anchors[name].Open();
		}
		protected void CloseAnchor(string name) {
			if (Anchors.ContainsKey(name)) Anchors[name].Close();
		}
	}
}
