using Assets.Scripts.Events;
using UnityEngine;
using System.Collections;
using System;

public class TravelController : MonoBehaviour {
    private float targetSpeed;
    private float _speed = 1;
    private float delay=0;

    public GameObject Inn;
    public int CurrentAct = 0;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        delay -= Time.deltaTime;

        if (delay<0)
        {
            if (Mathf.Abs(targetSpeed - _speed) > 0.001f) SetSpeed(Mathf.Lerp(_speed, targetSpeed, 2 * Time.deltaTime));
        }
	}

    public void SetSpeed(float speed)
    {
        _speed = speed;
        var scrolls = GetComponentsInChildren<TravellingThing>();
        foreach (var s in scrolls)
        {
            s.SetMultiplier(speed);
        }
    }

    public void Stop(float delay)
    {
        this.delay = delay;
        targetSpeed = 0;
    }

    public void Travel(float delay)
    {
        this.delay = delay;
        targetSpeed = 1;
    }

    public void SpawnInn(Action action)
    {
        GameObject go = Instantiate(Inn, new Vector3(13.5f,0.42f,0.5f), Quaternion.identity) as GameObject;
        go.GetComponent<ScrollingSprite>().action = action;
        go.GetComponent<ScrollingSprite>().isEvent = true;
        go.transform.parent = transform;
    }

    public void SetAct(Act act) {
        if (act == Act.Epilogue) {
            act = Act.Act_3;
        }

        if (CurrentAct != (int) act) {
            CurrentAct = (int) act;
            foreach (var scroll in GetComponentsInChildren<ScrollingBg>()) {
                scroll.CreateSprites((int) act,false);
            }
        }
    }
}
