using UnityEngine;
using System.Collections;

public class CabinDoorScript : MonoBehaviour {

	public AudioClip doorOpen1;
	public AudioClip doorOpen2;
	public AudioClip doorClose1;
	public AudioClip doorClose2;

	public AudioClip doorLocked;

	public int trainCarNumber = 0;
	public bool isLocked = true;

	public bool isTrainLadysDoor = false;

	private float animationSpeed = 0.2f;
	private bool isDoorOpen = false;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
		//if(Input.GetKeyUp(KeyCode.Space)){
		//	interactWithDoor();
		//}
		
	}
	
	public void openDoor(){
		
		gameObject.animation["OpenDoor"].speed = animationSpeed;
		gameObject.animation.Play();

		isDoorOpen = true;

		if(Random.Range(0,2) == 1){
			audio.clip = doorOpen1;
		}else{
			audio.clip = doorOpen2;
		}
		audio.Play();
		
	}
	
	public void closeDoor(){
		
		gameObject.animation["OpenDoor"].speed = -animationSpeed;
		gameObject.animation["OpenDoor"].time = gameObject.animation["OpenDoor"].length;
		gameObject.animation.Play();

		isDoorOpen = false;

		if(Random.Range(0,2) == 1){
			audio.clip = doorClose1;
		}else{
			audio.clip = doorClose2;
		}
		audio.Play();
		
	}
	
	public void interactWithDoor(){
		
		if(!isDoorOpen){

			if(!isLocked){

				openDoor();
				//isDoorOpen = true;
			
			}else{
				//Debug.Log ("door lock sound");
				audio.clip = doorLocked;
				audio.Play ();
			}
			
		}else{

			if(!isLocked){

				closeDoor();
				//isDoorOpen = false;

			}else{

			}
			
		}
		
	}

	public bool isDoorCurrentlyOpen(){
		return isDoorOpen;
	}

	public void lockDoor(){
		isLocked = true;
	}

	public void unlockDoor(){
		isLocked = false;
	}
	
}
