using UnityEngine;
using System.Collections;

public class LayerSliceEffect_Movement : LayerSliceEffect
{
    public enum MovementTypes
    {
        ToRight,
        ToLeft,
        ToUp,
        ToDown
    }

    public MovementTypes MovementType;
    public bool Firula = false;

    private const float TIME = 0.75f;
    private const float FIRULA_POSITION_MODIFIER = 0.05f;
    private const float FIRULA_TIME_MODIFIER = 0.15f;
    private const float FIRULA_TIME = TIME * FIRULA_TIME_MODIFIER;
	private const int SKIP_FRAME_COUNT = 0;

    private bool mOriginalFirulaValue;
    private float mElapsedTime = 0.0f;
    private float mWidth, mHeight;
    private float mTargetTime;
    private Vector3 mInitialPosition, mFinalPosition, mFirulaPosition;
    private Vector3 mWidthIncVector, mHeightIncVector;
	private int mSkippedFrames = 0;

    protected override void Awake()
    {
        mOriginalFirulaValue = Firula;
        mWidth = transform.parent.camera.orthographicSize * 1.333334f;
        mWidthIncVector = new Vector3(1, 0, 0) * mWidth;
        mHeight = transform.parent.camera.orthographicSize * 2.0f;
        mHeightIncVector = new Vector3(0, 1, 0) * mHeight;
    }

    public override void Init(bool removing)
    {
        Firula = mOriginalFirulaValue;
        mElapsedTime = 0.0f;
        mTargetTime = TIME;
        float distance;
        mFinalPosition = mOriginalPosition;
        switch (MovementType)
        {
            case MovementTypes.ToLeft:
                mInitialPosition = mOriginalPosition + mWidthIncVector;
                distance = Mathf.Abs(mFinalPosition.x - mInitialPosition.x) * FIRULA_POSITION_MODIFIER;
                mFirulaPosition = new Vector3(mFinalPosition.x - distance, mFinalPosition.y, mFinalPosition.z);
                break;
            case MovementTypes.ToRight:
                mInitialPosition = mOriginalPosition - mWidthIncVector;
                distance = Mathf.Abs(mFinalPosition.x - mInitialPosition.x) * FIRULA_POSITION_MODIFIER;
                mFirulaPosition = new Vector3(mFinalPosition.x + distance, mFinalPosition.y, mFinalPosition.z);
                break;
            case MovementTypes.ToUp:
                mInitialPosition = mOriginalPosition - mHeightIncVector;
                distance = Mathf.Abs(mFinalPosition.y - mInitialPosition.y) * FIRULA_POSITION_MODIFIER;
                mFirulaPosition = new Vector3(mFinalPosition.x, mFinalPosition.y + distance, mFinalPosition.z);
                break;
            case MovementTypes.ToDown:
                mInitialPosition = mOriginalPosition + mHeightIncVector;
                distance = Mathf.Abs(mFinalPosition.y - mInitialPosition.y) * FIRULA_POSITION_MODIFIER;
                mFirulaPosition = new Vector3(mFinalPosition.x, mFinalPosition.y - distance, mFinalPosition.z);
                break;
            default:
                Util.AssertTrue(false, "Unimplemented movement transition type: " + MovementType.ToString());
                break;
        }
        if(removing)
        {
            Vector3 temp = mInitialPosition;
            mInitialPosition = mFinalPosition;
            mFinalPosition = temp;
            Firula = false;
        }
        SetPosition(0.0f);
    }

    public override bool CustomUpdate(float deltaTime)
    {
        mElapsedTime += deltaTime;
        if (mElapsedTime >= mTargetTime)
        {
            if (Firula)
            {
                Firula = false;
                mElapsedTime = 0.0f;
                mTargetTime = FIRULA_TIME;
                mInitialPosition = mFirulaPosition;
                return false;
            }
            else
            {
                SetPosition(1.0f);
                return true;
            }
        }
        else
        {
            float normalizedElapsedTime = mElapsedTime / mTargetTime;
			if(mSkippedFrames < SKIP_FRAME_COUNT)
			{
            	mSkippedFrames++;
			}
			else
			{
				SetPosition(normalizedElapsedTime);
				mSkippedFrames = 0;
			}
            return false;
        }
    }

    private void SetPosition(float normalizedElapsedTime)
    {
        if (Firula)
        {
            transform.position = Vector3.Lerp(mInitialPosition, mFirulaPosition, normalizedElapsedTime);
        }
        else
        {
            transform.position = Vector3.Lerp(mInitialPosition, mFinalPosition, normalizedElapsedTime);
        }
    }
}