using Cryville.Common.Pdt;
using Cryville.Crtr.Components;
using Cryville.Crtr.Event;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Cryville.Crtr {
	public class SkinSelectors {
		readonly SkinSelector[] selectors;
		readonly string[] annotations;

		public SkinSelectors(IEnumerable<SkinSelector> s, IEnumerable<string> a) {
			selectors = s.ToArray();
			annotations = a.ToArray();
		}
		public override string ToString() {
			if (selectors.Length == 0) return "";
			bool flag = false;
			string r = "";
			foreach (var a in annotations) {
				if (flag) r += " " + a;
				else { r += a; flag = true; }
			}
			foreach (var s in selectors) {
				if (flag) r += " " + s.ToString();
				else { r += s.ToString(); flag = true; }
			}
			return r;
		}
		public void Optimize(PdtEvaluatorBase etor) {
			for (int i = 0; i < selectors.Length; i++) {
				selectors[i].Optimize(etor);
			}
		}
		public Transform MatchStatic(ContainerState h, Transform anchor = null) {
			foreach (var s in selectors) {
				anchor = s.Match(h, anchor);
				if (anchor == null) return null;
			}
			return anchor;
		}
		public bool IsUpdatable(ContainerState h) {
			foreach (var s in selectors)
				if (!s.IsUpdatable(h)) return false;
			return true;
		}
		public Transform MatchDynamic(ContainerState h, SkinElement e, Transform old_target, Transform anchor = null) {
			if (!e.IsDynamic) return null;
			foreach (var s in selectors) {
				anchor = s.Match(h, anchor, old_target);
				if (anchor == null) return null;
			}
			return anchor;
		}
	}

	public abstract class SkinSelector {
		protected SkinSelector() { }
		public abstract bool IsStatic {
			get;
		}
		public virtual void Optimize(PdtEvaluatorBase etor) { }
		public abstract Transform Match(ContainerState h, Transform a, Transform ot = null);
		public virtual bool IsUpdatable(ContainerState h) {
			return true;
		}
		public class CreateObject : SkinSelector {
			public CreateObject() { }
			public override bool IsStatic {
				get { return true; }
			}
			public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
				if (ot != null)
					return ot;
				var obj = new GameObject {
					name = "__obj__"
				};
				obj.transform.SetParent(a, false);
				obj.AddComponent<TransformInterface>();
				return obj.transform;
			}
		}
		public class Anchor : SkinSelector {
			public string Name { get; private set; }
			public Anchor(string name) {
				Name = name;
			}
			public override bool IsStatic {
				get { return true; }
			}

			public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
				return h.Handler.Anchors[Name].Transform;
			}
			public override bool IsUpdatable(ContainerState h) {
				return h.Handler.Anchors[Name].Opened;
			}
		}
		public class Property : SkinSelector {
			readonly PdtExpression _exp;
			readonly PdtOperator _op;
			bool _flag;
			public Property(PdtExpression exp) {
				_exp = exp;
				_op = new PropOp.Boolean(v => _flag = v);
			}
			public override void Optimize(PdtEvaluatorBase etor) {
				etor.Optimize(_exp);
			}
			public override bool IsStatic {
				get { throw new NotImplementedException(); }
			}
			public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
				ChartPlayer.etor.ContextTransform = a;
				ChartPlayer.etor.ContextEvent = h.Container;
				ChartPlayer.etor.Evaluate(_op, _exp);
				ChartPlayer.etor.ContextEvent = null;
				ChartPlayer.etor.ContextTransform = null;
				return _flag ? a : null;
			}
		}
		public class State : SkinSelector {
			public State(string state) { }
			public override bool IsStatic {
				get { return false; }
			}
			public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
				return null; // TODO
			}
		}
		public class ElementType : SkinSelector {
			readonly string _type;
			public ElementType(string type) { _type = type; }
			public override bool IsStatic {
				get { return true; }
			}
			public override Transform Match(ContainerState h, Transform a, Transform ot = null) {
				return h.Handler.TypeName == _type ? a : null;
			}
		}
	}
}
