using UnityEngine;
using System.Collections;

public class AllyPurpleonian : AllyController {

    public MeshFilter purpleLaserQuad;

    private float fTimeUntilNextLightning = 5.0f;
    private float fActiveLightningTimer = 1.0f;
    private float fDamageTimer = 0.2f;
    private GameObject lightningTarget;
    public GameObject laserBone;

	// Use this for initialization
    protected override void Start()
	{
        base.Start();
        purpleLaserQuad = GetComponentInChildren<MeshFilter>();
	}
	
	// Update is called once per frame
	public override void Update () 
    {
        base.Update();

        fTimeUntilNextLightning -= Time.deltaTime;

	    if( fTimeUntilNextLightning < 0.0f )
        {
            if( lightningTarget == null )
            {
                lightningTarget = PickSomethingToShootAt();

            }
            fActiveLightningTimer -= Time.deltaTime;

            if( fActiveLightningTimer > 0.0f )
            {
                //Draw Lazar
                if (CurrentState == AllyState.attached)
                {
                    DrawLaserBetweenSelfAnd(lightningTarget);
                    fDamageTimer -= Time.deltaTime;
                    if (fDamageTimer <= 0.0f)
                    {
                        PulseDamageOnTarget();
                        fDamageTimer = 0.2f;
                    }
                }
            }
            else
            {
                //Hide lazar
                //drop target
                HideLaser();
                lightningTarget = null;
                fTimeUntilNextLightning = 3.0f;
                fActiveLightningTimer = 0.8f;
            }
        }
	}

    void DrawLaserBetweenSelfAnd(GameObject target)
    {
        if (target == null)
            return;

        MeshRenderer mr = purpleLaserQuad.GetComponent<MeshRenderer>();
        mr.enabled = true;

        Vector3 vLightningDelta = target.transform.position - laserBone.transform.position;
        float fDist = vLightningDelta.magnitude;

        Vector3 lightningPos = laserBone.transform.position;
        vLightningDelta.Normalize();
        lightningPos += vLightningDelta * fDist * 0.5f;
        purpleLaserQuad.transform.position = lightningPos;
        purpleLaserQuad.transform.right = vLightningDelta;

        Vector3 pissss =
            purpleLaserQuad.transform.localScale;

        pissss.x = fDist;
        pissss.y = 0.1f + Random.value * 0.2f;

        purpleLaserQuad.transform.localScale = pissss;

        Material lightningMaterial = purpleLaserQuad.GetComponent<MeshRenderer>().material;
        lightningMaterial.mainTextureScale = new Vector2(fDist / 4, 1);
        Vector2 vPants = lightningMaterial.mainTextureOffset;
        vPants.x += Time.smoothDeltaTime * Random.value * -12.05f;
        lightningMaterial.mainTextureOffset = vPants;

    }

    void PulseDamageOnTarget()
    {
        if (lightningTarget != null)
        {
            playerScript.Speakerbox.PlayOneShot( playerScript.wavPurpleRay);

            EnemyController ecs = lightningTarget.GetComponent<EnemyController>();
            if (ecs != null)
            {
                ecs.TakeDamage(1);
            }
        }
    }

    void HideLaser()
    {
        MeshRenderer mr = purpleLaserQuad.GetComponent<MeshRenderer>();
        mr.enabled = false;
    }

    GameObject PickSomethingToShootAt()
    {
        GameObject[] stuffs = Object.FindObjectsOfType<GameObject>();
        int iTries = 100;
        GameObject target = null;
        int iStuffLemf = stuffs.Length;

        while (target == null && iTries > 0)
        {
            iTries--;
            target = stuffs[Random.Range(0, iStuffLemf)];

            if (target.layer != LayerMask.NameToLayer("Monster") && 
                target.layer != LayerMask.NameToLayer("Cage") )
            {
                target = null;
                continue;
            }

            if (target.transform.position.x > 14 ||
                target.transform.position.y < -5.0f)
            {
                target = null;
                continue;
            }

            EnemyController ecs = target.GetComponent<EnemyController>();
            if (ecs == null)
            {
                target = null;
                continue;
            }
            
        }

        if (target != null)
        {
            GameObject impactLaser = Instantiate(Resources.Load("LaserImpact")) as GameObject;
            impactLaser.transform.position = target.transform.position;
            impactLaser.transform.parent = target.transform;
        }
        return target;
    }
}
