using UnityEngine;
using System.Collections;

public class Tower : MonoBehaviour
{
    public GameObject[] PlayerTowerPiecePrefabs;
    public GameObject[] PlayerTowerTopPiecePrefabs;
    public float TowerPieceXVariance = 0.1f;

    //private TowerPiece[] _towerPieces;

    private ArrayList _towerPieces;
    private Wizard _wizard;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void Init(int startingHealth, GameObject[] wizardPrefabs)
    {
        Transform lastTowerPeiceTransform = transform;
        float lastTowerPieceHeight = 0f;
        float zDepth = 0f;

        _towerPieces = new ArrayList(startingHealth);

        for(int i = 0; i < startingHealth - 1; i++)
        {
            int towerPieceIndex = Random.Range(0, PlayerTowerPiecePrefabs.Length);
            var towerPiece = Instantiate(PlayerTowerPiecePrefabs[towerPieceIndex]).GetComponent<TowerPiece>();
            towerPiece.transform.SetParent(lastTowerPeiceTransform, false);
            towerPiece.transform.localPosition = new Vector3(Random.Range(-TowerPieceXVariance, TowerPieceXVariance), lastTowerPieceHeight, zDepth);

            _towerPieces.Add(towerPiece);
            lastTowerPeiceTransform = towerPiece.transform;
            lastTowerPieceHeight = towerPiece.height;
            zDepth += 0.01f;
        }

        int topTowerPieceIndex = Random.Range(0, PlayerTowerTopPiecePrefabs.Length);
        var topTowerPiece = Instantiate(PlayerTowerTopPiecePrefabs[topTowerPieceIndex]).GetComponent<TowerPiece>();
        topTowerPiece.transform.SetParent(lastTowerPeiceTransform, false);
        topTowerPiece.transform.localPosition = new Vector3(Random.Range(-TowerPieceXVariance, TowerPieceXVariance), lastTowerPieceHeight, zDepth);
        lastTowerPeiceTransform = topTowerPiece.transform;
        lastTowerPieceHeight = topTowerPiece.height;
        zDepth += 0.01f;

        _towerPieces.Add(topTowerPiece);

        int wizardIndex = Random.Range(0, wizardPrefabs.Length);
        _wizard = Instantiate(wizardPrefabs[wizardIndex]).GetComponent<Wizard>();
        _wizard.transform.SetParent(lastTowerPeiceTransform, false);
        _wizard.transform.localPosition = new Vector3(Random.Range(-TowerPieceXVariance, TowerPieceXVariance), lastTowerPieceHeight, zDepth);
    }

    public void DestroyPiece(int pieceIndex)
    {
        var dyingPiece = (TowerPiece)_towerPieces[pieceIndex];

        if(pieceIndex < _towerPieces.Count - 1)
        {
            var nextPiece = (TowerPiece)_towerPieces[pieceIndex + 1];

            nextPiece.transform.SetParent(dyingPiece.transform.parent);
            nextPiece.StartFalling(dyingPiece.transform.localPosition);
        }

        dyingPiece.SetState(TowerPiece.State.Dying);
        _towerPieces.Remove(dyingPiece);

        _wizard.SetState(_towerPieces.Count > 0 ? Wizard.State.Hurt : Wizard.State.Dying);
    }

    public void DestroyTower()
    {
        while(_towerPieces.Count > 0)
        {
            DestroyPiece(0);
        }
    }

    public Wizard GetWizard()
    {
        return _wizard;
    }
}
