using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Battle : MonoBehaviour {

    private List<Monster> monsters;
    private int attacker;
    private int attackedOne;
    private int winner;
    private bool oneIsDead;
    private Battle battle;
	public static DemonControl container1;
	public static DemonControl container2;
	private BattleChange canvas;
	public static AudioSource audio;
	public static Game game;

	// Use this for initialization
	void Start () {

        oneIsDead = false;
        battle = new Battle(GameMaster.players[0].GetMonsters()[GameMaster.players[0].GetActiveMonster()], GameMaster.players[1].GetMonsters()[GameMaster.players[1].GetActiveMonster()], GameMaster.activePlayer);
        Debug.Log("Battle Start");
        StartCoroutine(FightRoutine());

		game = GameObject.Find ("Skripte").GetComponent<Game> ();
		audio = GameObject.Find ("Skripte").GetComponent<AudioSource> ();
    }


    IEnumerator FightRoutine()
    {
        yield return new WaitForSeconds(2);

        battle.Fight();
        if (battle.oneIsDead) {
           if(GameMaster.players[battle.attackedOne].GetDeadMonsters() >= 3) {
                //One Player has won the whole battle
                winner = battle.attacker;
				GameMaster.winner = battle.attacker;
                Debug.Log(GameMaster.players[battle.attacker].GetName() + "has won.");
                yield return new WaitForSeconds(3);
                Application.LoadLevel("endScreen");
            }
           else {
                //One Monster is dead - switch Monster
				Debug.Log(GameMaster.players[battle.attacker].GetName() + " has killed one of the opponent's monster. " + GameMaster.players[battle.attackedOne].GetName() + ", choose another monster!");
                oneIsDead = false;
				Invoke ("delayedOverlay", 2);
            }
        } else {
            StartCoroutine(FightRoutine());
        }
    }

	void delayedOverlay () {
		GameObject.Find ("selectOverlay" + battle.attackedOne).GetComponent<Canvas> ().enabled = true;
		battle.canvas = new BattleChange (battle.attackedOne, battle);
	}

    Battle(Monster monster1, Monster monster2, int attacker) {
        monsters = new List<Monster>(2);
        monsters.Add(monster1);
        monsters.Add(monster2);
        
		this.canvas = null;
		this.attacker = attacker;

        if(attacker == 1){
            attackedOne = 0;
        } else {
            attackedOne = 1;
        }
    }


    void Fight() {

		container1 = GameObject.Find ("MonsterContainer0").GetComponent<DemonControl>();
		container2 = GameObject.Find ("MonsterContainer1").GetComponent<DemonControl>();

        int randomDexterity = Random.Range(0, 100);
        int randomLuck = Random.Range(0, 100);

        if (randomDexterity > monsters[attackedOne].GetDexterity()) {
            if(randomLuck > monsters[attacker].GetLuck()) {
                monsters[attackedOne].TakeDamage(monsters[attacker].GetAttack());
                Debug.Log(GameMaster.players[attacker].GetName() + "has attacked the other player. Damage Output: " + monsters[attacker].GetAttack());
				if (attackedOne == 0) {
					container1.ChangeAnimation ("block");
					container2.ChangeAnimation ("attack");
				} else {
					container1.ChangeAnimation ("attack");
					container2.ChangeAnimation ("block");
				}
				audio.clip = game.clips [0];
				audio.Play();
            } else {
                Debug.Log("Critical Strike!");
                monsters[attackedOne].TakeDamage(monsters[attacker].GetAttack() * 2);
                Debug.Log(GameMaster.players[attacker].GetName() + "has attacked the other player. Critical Damage Output: " + monsters[attacker].GetAttack() * 2);
				if (attackedOne == 0) {
					container1.ChangeAnimation ("block");
					container2.ChangeAnimation ("attack");
				} else {
					container1.ChangeAnimation ("attack");
					container2.ChangeAnimation ("block");
				}
				audio.clip = game.clips [0];
				audio.Play();
            }
        } else {
            Debug.Log("Missed");
            Debug.Log(GameMaster.players[attackedOne].GetName() + "has dodged the attack.");
			if (attackedOne == 0) {
				container1.ChangeAnimation ("dodge");
				container2.ChangeAnimation ("attack");
			} else {
				container1.ChangeAnimation ("attack");
				container2.ChangeAnimation ("dodge");
			}
			audio.clip = game.clips [1];
			audio.Play();
        }
			
        if (monsters[attackedOne].GetIsDead()){
            winner = attacker;
            oneIsDead = true;
			if (winner == 0) {
				container2.ChangeAnimation ("death");
			} else {
				container1.ChangeAnimation ("death");
			}
			audio.clip = game.clips [2];
			audio.Play();
        } else {
            switchAttacker();
        }

    }


    void switchAttacker() {
        if(attacker == 0){
            attacker = 1;
            attackedOne = 0;
        }
        else {
            attacker = 0;
            attackedOne = 1;
        }
    }

	public void SetNewMonster(Monster monster1, Monster monster2, int attacker) {
		monsters = new List<Monster> (2);
		monsters.Add (monster1);
		monsters.Add (monster2);
		this.attacker = attacker;

		if (attacker == 1) {
			attackedOne = 0;
		} else {
			attackedOne = 1;
		}
	}
}
