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

public class LDBeamProjectile : MonoBehaviour
{
    private MeshRenderer myMeshRenderer;
    private Material myMaterial;

    public float ratioLengthVsHeight = 1.0f;
    public int numBeamSpritesOnMaterial = 2;


    private bool bShouldDraw;
    public LDGameActor targetActor;
    public LDWeaponBeam originWeaponBeam;
    public LDGameActor originBeamActor;

    private GameManager theGame;

	// Use this for initialization
    void Awake()
    {
        myMeshRenderer = GetComponent<MeshRenderer>();
        myMaterial = myMeshRenderer.material;
        myMaterial.mainTextureScale = new Vector2(1.0f, 1.0f / numBeamSpritesOnMaterial);
    }
	void Start ()
	{
        theGame = GameManager.GetGame();
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (bShouldDraw)
        {
            if (!myMeshRenderer.enabled)
            {
                myMeshRenderer.enabled = true;
            }
            else if (Random.value < 0.4)
            {
                myMeshRenderer.enabled = false;
            }

            if (myMeshRenderer.enabled)
            {
                Vector3 vScreenspaceEndpoint = theGame.GetScreenspaceVectorFromGamePosition(targetActor.GetTargetingPosition());
                DrawBeamToPoint(vScreenspaceEndpoint);
            }
        }
	}

    public void EnableBeam(LDWeaponBeam origin, LDGameActor target)
    {
        bShouldDraw = true;
        originBeamActor = null;
        originWeaponBeam = origin;
        targetActor = target;
    }
    public void EnableBeam(LDGameActor originActor, LDGameActor target)
    {
        bShouldDraw = true;
        originWeaponBeam = null;
        originBeamActor = originActor;
        targetActor = target;
    }

    public void DisableBeam()
    {
        bShouldDraw = false;
        myMeshRenderer.enabled = false;
    }

    Vector3 GetBeamOrigin()
    {
        if (originWeaponBeam != null)
        {
            return originWeaponBeam.GetBeamAnchorScreenPosition(); 
        }
        if (originBeamActor != null)
        {
            return originBeamActor.transform.position; // theGame.GetScreenspaceVectorFromGamePosition(originBeamActor.GetTargetingPosition());
        }

        return Vector3.zero;
    }

    void DrawBeamToPoint(Vector3 vScreenspaceEndpoint )
    {
        Vector3 vScreenOrigin = GetBeamOrigin();
        if (vScreenOrigin == Vector3.zero)
        {
            //something is fucky
            DisableBeam();
            return;
        }

        MeshRenderer mr = myMeshRenderer;
        mr.enabled = true;
        mr.sortingOrder = 4;

        //Get the vector between us and them.
        // theGame.GetScreenspaceVectorFromGamePosition(originWeaponBeam.GetBeamWorldPos());
        Vector3 vBeamDelta = vScreenspaceEndpoint - vScreenOrigin;
        vBeamDelta.z = 0f;
        float fDist = vBeamDelta.magnitude;

        //place the quad at the mid point between us and them, and rotate it to face correctly along the vector
        Vector3 beamPos = vScreenOrigin;
        vBeamDelta.Normalize();
        beamPos += vBeamDelta * fDist * 0.5f;
        beamPos.z = -3;

        transform.position = beamPos;
        transform.right = vBeamDelta;

        Vector3 vBeamScale = transform.localScale;
        vBeamScale.x = fDist;
        vBeamScale.z = 0.25f;

        //pulse/ change Y for beam thickness
        transform.localScale = vBeamScale;


        //Scale the texture on the quad and rotate through it
        float fStep = 1.0f / (float)numBeamSpritesOnMaterial;

        myMaterial.mainTextureScale = new Vector2(fDist / ratioLengthVsHeight, fStep);
        Vector2 vPants = myMaterial.mainTextureOffset;
        vPants.y = (float) (Random.Range(0, numBeamSpritesOnMaterial)) * fStep;
        //vPants.x += Time.smoothDeltaTime * Random.value * -12.05f;
        myMaterial.mainTextureOffset = vPants;
        myMaterial.color = Color.white;
    }
}
