using UnityEngine;
using System.Collections;

public class GameLock: MonoBehaviour {


	[SerializeField] private bool locked;
	[SerializeField] private bool usable;
	[SerializeField] private string keyName;
	// Use this for initialization
	void Start () {
		locked = true;
		usable = true;
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void setUnlocked()
	{
		locked = false;
	}
	public void setLocked()
	{
		locked = true;
	}
	public void flipLock()
	{
		locked = !locked;
	}

	public void interact(Hero hero)
	{
		if (usable && keyName != ""){
			if (this.locked) {
				if (hero.hasKey(keyName)) {
				    UnlockLoseKey(hero);
					usable = false;
				}
			}
			else {
				LockGainKey(hero);
				usable = false;
			}

		}
		else if (usable && keyName == "")
		{
			this.flipLock();
			usable = false;
		}
	}
	private void LockGainKey(Hero hero)
	{
		if (locked == false){
			this.locked = true;
			if (hero.hasKey(keyName))
				hero.keys[keyName]+= 1;
			else
				hero.keys.Add (keyName,1);
		}
	}
	private void UnlockLoseKey(Hero hero)
	{
		if (this.locked == true) {
			if (hero.hasKey (keyName)) {
				hero.keys[keyName]-= 1;
				this.locked = true;
			}
		}
	}
}
