﻿//Author - Devin Reimer - http://blog.almostlogical.com
using UnityEngine;
using System.Collections;

public class ImageSequencer : MonoBehaviour
{
    public Texture2D[] imgSequence;
    private Material mat;
    private int imgCount = 0;

    public Animation animation;
    private float animationLength;
    private float timePerFrame;
    private int lastFrame = -1;
   // private Texture2D[] imgSequence;

    public Animation[] animationSyncList;

    public int desiredFrameEditor = 0;
   
    public void Start()
    {
        int numOfImagesInSequence = 334;
        mat = this.renderer.sharedMaterial;
        animation.enabled = true;
        animationLength = animation.clip.length;
        timePerFrame = numOfImagesInSequence / animationLength;

        /*
       
        int imgSequenceStart = 25;
        string imageName;
        string imageNumStr;
        
        imgSequence = new Texture2D[numOfImagesInSequence];
        for (int p = 0; p < numOfImagesInSequence; p++)
        {
            imageName = "HallwaySequence_1_";
            imageNumStr = "" + (imgSequenceStart + p);
            while (imageNumStr.Length < 5)
            {
                imageNumStr = "0" + imageNumStr;
            }
            imageName += imageNumStr;
            //Debug.Log(imageName);
            imgSequence[p] = (Texture2D)Resources.Load(imageName, typeof(Texture2D));
        }
        */
        lastFrame = -1;


        animation["C4D Animation Take"].enabled = true;
        animation["C4D Animation Take"].weight = 1f;
        animation["C4D Animation Take"].speed = 0; //to make the animation pause

        for (int p = 0; p < animationSyncList.Length; p++)
        {
            animationSyncList[p][animationSyncList[p].clip.name].enabled = true;
            animationSyncList[p][animationSyncList[p].clip.name].weight = 1f;
            animationSyncList[p][animationSyncList[p].clip.name].speed = 0;
        }

        //HallwaySequence_1_00025
       // Debug.Log("Length:" + animation.clip.length);
       // Debug.Log(animation["C4D Animation Take"].time);
    }

    public void Update()
    {
        //int currentFrame = (int)(animation["C4D Animation Take"].time * timePerFrame);
        int currentFrame = (int)(Time.timeSinceLevelLoad * timePerFrame);

        if (currentFrame >= (imgSequence.Length * 2)+10)
        {
            Debug.Log("End of Complete Load");
            Application.LoadLevel("Intro");
          //  Application.LoadLevel(Application.loadedLevelName);

        }
        currentFrame = currentFrame % (imgSequence.Length * 2);
        if (currentFrame >= imgSequence.Length)
        {
            //currentFrame= (imgSequence.Length-currentFrame)-1;
            currentFrame = imgSequence.Length + (imgSequence.Length - currentFrame);
        }
        if (currentFrame < imgSequence.Length && currentFrame != lastFrame)
        {

            goToFrame(currentFrame);
            //Debug.Log("Time:" + Time.time + ", CalcTime:" + currentFrame / timePerFrame);
        }

    }
 




    public void goToFrame(int frameNum)
    {
         // mat.mainTexture = images[currentFrame];
        mat.mainTexture = imgSequence[frameNum];
        lastFrame = frameNum;
        //animation["C4D Animation Take"].time = Time.time;

        //animation["C4D Animation Take"].enabled = true;
        //animation["C4D Animation Take"].weight = 1f;
        float animationTime = frameNum / timePerFrame;
        animation["C4D Animation Take"].time = animationTime;

        for (int p = 0; p < animationSyncList.Length; p++)
        {
            if (animationSyncList[p] != null)
            {
                animationSyncList[p][animationSyncList[p].clip.name].time = animationTime;
            }
        }

       // animation["C4D Animation Take"].speed = 0; //to make the animation pause
        //animation["C4D Animation Take"].time = Time.time;
        //animation.Play(PlayMode.StopAll);
    }

}