﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;

public class T2AnimMono : MonoBehaviour
{
    [HideInInspector]
    public string loadPath;
    [HideInInspector]
    public string savePath;
    public T2Animation anim;
}

[Serializable]
public class T2Animation
{
    public int version; //always 2
    public T2AnimationBlock[] blocks;

    public void SortBlocks()
    {
        if (blocks.Length > 0)
        {
            Array.Sort(blocks, delegate (T2AnimationBlock x, T2AnimationBlock y) { return x.animID.CompareTo(y.animID); });
            for (int i = 0; i < blocks.Length; i++)
            {
                if (blocks[0].animID >= 0)
                    break;
                ArrayUtility.Add(ref blocks, blocks[0]);
                ArrayUtility.RemoveAt(ref blocks, 0);
            }
        }
    }
}

[Serializable]
public class T2AnimationBlock
{
    public int animID; //each anim ID has a special purpose and is used for special behaviors
    public int frameCount;
    public int translationsLength;
    public int rotationsLength;
    public float frameScale;
    public Int16 blendLength;
    public Int16 blendStart;
    public Int16 blendEnd;
    public float animSpeed;
    public Int16 loopFrame;

    public T2AnimNodeIndex[] nodeIndexes; //should have same size as skinnedmesh target nodes
    public T2AnimInitalNode[] initalNodes; //should have same size as skinnedmesh target nodes
    public float[] yawOffsets; //should have same size as framecount
    public Vector3[] translations; //array of translations(vector3) for each frame
    public Vector4[] rotations; //array of rotations(vector4) for each frame
    public T2AnimKeyFrameAction[] keyFrameActions;

    public void CopyFrom(T2AnimationBlock other)
    {
        animID = other.animID;
        frameCount = other.frameCount;
        translationsLength = other.translationsLength;
        rotationsLength = other.rotationsLength;
        frameScale = other.frameScale;
        blendLength = other.blendLength;
        blendStart = other.blendStart;
        blendEnd = other.blendEnd;
        animSpeed = other.animSpeed;
        loopFrame = other.loopFrame;
        Array.Resize(ref nodeIndexes, other.nodeIndexes.Length);
        for (int i = 0; i < nodeIndexes.Length; i++)
        {
            nodeIndexes[i] = new T2AnimNodeIndex();
            nodeIndexes[i].translationIndex = other.nodeIndexes[i].translationIndex;
            nodeIndexes[i].rotationIndex = other.nodeIndexes[i].rotationIndex;
        }
        Array.Resize(ref initalNodes, other.initalNodes.Length);
        for (int i = 0; i < initalNodes.Length; i++)
        {
            initalNodes[i] = new T2AnimInitalNode();
            initalNodes[i].translation = other.initalNodes[i].translation;
            initalNodes[i].rotation = other.initalNodes[i].rotation;
        }
        Array.Resize(ref yawOffsets, other.yawOffsets.Length);
        for (int i = 0; i < yawOffsets.Length; i++)
        {
            yawOffsets[i] = other.yawOffsets[i];
        }
        Array.Resize(ref translations, other.translations.Length);
        for (int i = 0; i < translations.Length; i++)
        {
            translations[i] = other.translations[i];
        }
        Array.Resize(ref rotations, other.rotations.Length);
        for (int i = 0; i < rotations.Length; i++)
        {
            rotations[i] = other.rotations[i];
        }
        Array.Resize(ref keyFrameActions, other.keyFrameActions.Length);
        for (int i = 0; i < keyFrameActions.Length; i++)
        {
            keyFrameActions[i] = new T2AnimKeyFrameAction();
            keyFrameActions[i].frame = other.keyFrameActions[i].frame;
            keyFrameActions[i].eventType = other.keyFrameActions[i].eventType;
            keyFrameActions[i].node = other.keyFrameActions[i].node;
            keyFrameActions[i].offset = other.keyFrameActions[i].offset;
            for (int j = 0; j < keyFrameActions[i].args.Length; j++)
            {
                keyFrameActions[i].args[j] = other.keyFrameActions[i].args[j];
            }
        }
    }
}

[Serializable]
public class T2AnimNodeIndex
{
    public Int16 translationIndex;
    public Int16 rotationIndex;
}

[Serializable]
public class T2AnimInitalNode
{
    public Vector3 translation;
    public Vector4 rotation;
}

[Serializable]
public class T2AnimKeyFrameAction
{
    public Int16 frame; //which frame this event occurs on
    public Int16 eventType;
    public Int16 node; //which bone this event is associated with
    public Vector3 offset; //local-space offset vector
    public float[] args = new float[8]; //8 floats
}
