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

namespace Cryville.Crtr {
	class GroupHandler : ContainerHandler {
		public ChartHandler ch;

		SquareMatrix matFrame;
		List<ContainerState> tracks;

		public GroupHandler(Chart.Group tg, ChartHandler ch) : base() {
			this.ch = ch;
		}
		
		public override string TypeName {
			get {
				return "group";
			}
		}

		public override void PreInit() {
			base.PreInit();
			tracks = (
				from c in cs.Children
				where c.Value.Container is Chart.Track
				select c.Value
			).ToList();
			matFrame = SquareMatrix.WithPolynomialCoefficients(tracks.Count);
		}

		public ColumnVector<Vector3> GetCurrentFrame(Func<ContainerState, Vector3> func) {
			var vl = from t in tracks select func(t);
			return matFrame.Eliminate(
				new ColumnVector<Vector3>(vl.ToArray()),
				new Vector3Operator()
			);
		}
	}

	class Vector3Operator : IVectorOperator<Vector3> {
		public Vector3 Add(Vector3 lhs, Vector3 rhs) {
			return lhs + rhs;
		}

		public Vector3 ScalarMultiply(float lhs, Vector3 rhs) {
			return lhs * rhs;
		}
	}
}
