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

public class LDLandmarkDestroyingComponent : MonoBehaviour
{
    private LDGameActor myLandmarkTarget;
    private LDHuntAndKillActionComponent myActionComponent;
    private LDGameActor myOwningActor;
    private LDBeamProjectile myDeathRay;

    //beems
    public GameObject Prefab_ProjectileDeathRay;
    public float RangeForLandmarkDestroyingBeam;

    private bool bFoomingLandmark;
    private float fTimeUntilLandmarkFooms;

    private GameManager theGame;

    public AudioClip clip_BeamStart;
    public AudioClip clip_Success;

	// Use this for initialization
	void Start ()
	{
        theGame = GameManager.GetGame();
	    
	    myActionComponent = GetComponent<LDHuntAndKillActionComponent>();
	    myOwningActor = myActionComponent.myOwningActor;

        GameObject go = Instantiate(Prefab_ProjectileDeathRay) as GameObject;
        go.transform.parent = transform;

	    myDeathRay = go.GetComponent<LDBeamProjectile>();
        myDeathRay.DisableBeam();

	}
	
	// Update is called once per frame
	void Update () 
    {
        if (myLandmarkTarget == null || myLandmarkTarget.IsDead())
        {
            bFoomingLandmark = false;
            myLandmarkTarget = myActionComponent.GetLandmarkTarget();
        }

        //if I have a landmark target
        if (myLandmarkTarget != null)
        {
            //am I in foom mode?
            if (bFoomingLandmark)
            {
                TM_MovementComponent tmc = myOwningActor.GetComponent<TM_MovementComponent>();
                if (tmc != null)
                {
                    tmc.StopAllMotion();
                }
                fTimeUntilLandmarkFooms -= Time.deltaTime;
                if (fTimeUntilLandmarkFooms <= 0f)
                {
                    DestroyLandmark();
                }
            }
            else
            {
                //if not
                Vector2 vToTarget = myLandmarkTarget.GetTargetingPosition() - myOwningActor.GetTargetingPosition();
                if (vToTarget.sqrMagnitude < RangeForLandmarkDestroyingBeam * RangeForLandmarkDestroyingBeam)
                {
                    CommencePrimaryFoomnition();
                }
            }
        }
	}

    void CommencePrimaryFoomnition()
    {
        bFoomingLandmark = true;
        fTimeUntilLandmarkFooms = 5.0f;
        myDeathRay.EnableBeam(myOwningActor, myLandmarkTarget);
        TM_AudioManager.PlayClip(clip_BeamStart);
    }

    void DestroyLandmark()
    {
        myDeathRay.DisableBeam();
        bFoomingLandmark = false;

        theGame.theWorld.ChangeAreaToTerrain( theGame.theWorld.GetIndexForTileVector( myLandmarkTarget.vWorldPos), 1, ETerrainType.empty_space);

        //update UVs
        theGame.theWorld.ConvertAllBottomEdgeTilesToRock();
        theGame.UpdateWorldFromTileChange();

        myLandmarkTarget.TakeDamage(10000,myOwningActor);

        //kill everything nearby that isn't the player
        List <LDCombatComponent> targets = theGame.theAIManager.GetTargetsInArea(50, myOwningActor.Faction, 3.0f, myLandmarkTarget.vWorldPos, true);

        if (targets != null)
        {
            targets.RemoveAll(a => a.gameObject == theGame.thePlayer.gameObject);
            for (int t = 0; t < targets.Count; t++)
            {
                LDGameActor sucka = targets[t].myOwningActor;
                if (sucka != null)
                {
                    sucka.TakeDamage(10000, myOwningActor);
                }
            }
        }

        myLandmarkTarget = null;
        TM_AudioManager.PlayClip(clip_Success);

    }
}
