using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Random = UnityEngine.Random;

public class BossNose : EnemyController
{
    public List<GameObject> attachedGuns;

    private Vector3 bossDest;
    private float fTimeTillNewDest;

    public GameObject ParticleOnTurretsFromDamageTransfer;

    public string[] ShipNameList;
    public string strShipName;

    public void AttachGunToMe(GameObject newGun)
    {
        if( attachedGuns == null )
            attachedGuns = new List<GameObject>();

        if (attachedGuns.Contains(newGun))
            return;

        attachedGuns.Add(newGun);
        newGun.transform.parent = transform;

        if (mcs == null)
        {
            mcs = GameObject.Find("MasterController").GetComponent<MasterControllerScript>();
        }

        EnemyController ec = newGun.GetComponent<EnemyController>();
        if (mcs.iWhatWaveIsIt > 10)
        {
            ec.SetMaxHealth( ec.Health + (mcs.iWhatWaveIsIt/5)*3);
        }

        strShipName = ShipNameList[Random.Range(0, ShipNameList.Count())];
    }

    public override void UpdateMotion()
    {
        fTimeTillNewDest -= Time.deltaTime;
        if (fTimeTillNewDest < 0.0f)
        {
            fTimeTillNewDest = 1.0f + Random.value*4.0f;

            bossDest.x = -1.0f + Random.value * 9.0f;
            bossDest.y = -1.0f + Random.value * 2.5f;
        }

        Vector3 vDir = bossDest - transform.position;
        vDir.Normalize();

        Vector3 vLoc = transform.position;
        vLoc += vDir*2.0f*Time.deltaTime;
        transform.position = vLoc;
    }

    public override void Update()
    {
        base.Update();

        for(int t=0; t < attachedGuns.Count; t++)
            if( attachedGuns[t] == null)
                attachedGuns.RemoveAt(t);

        if (attachedGuns.Count == 0)
        {
            for (int t = 0; t < 20; t++)
            {
                GameObject go = Instantiate(ParticleOnTurretsFromDamageTransfer);
                Vector3 vMyPos = transform.position;
                vMyPos.x += Random.value*3.0f;
                vMyPos.y += -2.0f + Random.value*4.0f;
                go.transform.position = vMyPos;
                Destroy(go, 2.0f);
            }

            //bug, but it rules
            Died();

            if (mcs.ActiveBoss != null)
            {
                mcs.AddScore(50000 + mcs.iWhatWaveIsIt * 1000);
                mcs.playerScript.Speakerbox.PlayOneShot( mcs.playerScript.wavBossCrushed);
            }

            mcs.ActiveBoss = null;
        }


    }

    public override void TakeDamage(Projectile p)
    {
        TakeDamage(p.DamageOnHit);
    }

    public override void TakeDamage(float fDamage)
    {
        if (attachedGuns == null ||
            attachedGuns.Count == 0)
            return;

        //find a gun to damage!
        GameObject victimGun = attachedGuns[Random.Range(0, attachedGuns.Count)];

        if (victimGun != null)
        {
            EnemyController ec = victimGun.GetComponent<EnemyController>();
            ec.TakeDamage(fDamage);

            GameObject go = Instantiate(ParticleOnTurretsFromDamageTransfer);
            go.transform.position = ec.transform.position;
            Destroy(go, 2.0f);

            if (ec.bHandledDead)
                attachedGuns.Remove(ec.gameObject);
        }
    }
}
