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

public class LDCameraManager : MonoBehaviour
{
    public int pxPerTile;

    private int iOldScreenHeight = 0;
    private int iOldScreenWidth = 0;
    public Vector2 StartWorldPos;
    private Vector2 vWorldPos;
    private Vector2 vMeshSize;

    public LDGameActor focusActor;

    public float fCameraFloatSharpness = 0.05f;

    public Rect rectCameraBounds;


    private Vector3 vBaseTransformPosition;
    public float fScreenshake = 0f;

	// Use this for initialization
	void Start ()
	{
	    vWorldPos = StartWorldPos;
		SetOrtho();
	}
	
	// Update is called once per frame
	void Update () 
    {
        if (iOldScreenHeight != Screen.height ||
            iOldScreenWidth != Screen.width)
        {
            SetOrtho();
        }

        if (focusActor != null)
        {
            SetPositionBasedOnFocusActor();
        }
        else
        {

        }

        if (fScreenshake > 0f)
        {
            fScreenshake -= Time.deltaTime;
            Vector3 vShakePos = vBaseTransformPosition +(Vector3)(Random.insideUnitCircle) * Random.Range(0.1f, 0.3f);
            vShakePos = QuantizeVectorForPixelPerfection(vShakePos);
            transform.position = vShakePos;
        }
        else
        {
            transform.position = vBaseTransformPosition;
        }

        Color backColor = Color.black;

        backColor.r = Mathf.Lerp(0f, 0.25f, vWorldPos.magnitude / 300.0f);
        Camera.main.backgroundColor = backColor;

    }

    public void SetTransformBase(Vector3 vPos)
    {
        Camera.main.transform.position = vPos;
        vBaseTransformPosition = vPos;
    }


    public Vector2 GetWorldPosition()
    {
        return vWorldPos;
    }

    void SetOrtho()
    {
        float fDesiredOrtho = 0f;

        //how many pixels tall is the screen?
        float fHeight = Screen.height;
        fHeight *= 0.5f;
        fDesiredOrtho = fHeight / (float)pxPerTile;

        Camera.main.orthographicSize = fDesiredOrtho;

        iOldScreenHeight = Screen.height;
        iOldScreenWidth = Screen.width;

        rectCameraBounds = new Rect();
        rectCameraBounds.xMin = 15.0f;
        rectCameraBounds.yMin = 15.0f;
        rectCameraBounds.xMax = 185.0f;
        rectCameraBounds.yMax = 185.0f;

    }

    void SetPositionBasedOnFocusActor()
    {
        //Move towards thing we're tracking
        Vector2 vDelta = focusActor.GetCameraFocusPosition() - vWorldPos;
        Vector2 vOldPos = vWorldPos;
        vWorldPos += vDelta * fCameraFloatSharpness;

        //but stay in bounds
        if (vWorldPos.x < rectCameraBounds.xMin || vWorldPos.x > rectCameraBounds.xMax)
        {
            vWorldPos.x = vOldPos.x;
        }

        if (vWorldPos.y < rectCameraBounds.yMin || vWorldPos.y > rectCameraBounds.yMax)
        {
            vWorldPos.y = vOldPos.y;
        }

    }

    public void SetMeshSizeInTiles(int iWide, int iTall)
    {
        vMeshSize = new Vector2(iWide, iTall);
    }

    public Vector3 QuantizeVectorForPixelPerfection( Vector3 vNewPos )
    {
        vNewPos.x *= pxPerTile;
        vNewPos.x = Mathf.Floor(vNewPos.x);
        vNewPos.x /= pxPerTile;

        vNewPos.y *= pxPerTile;
        vNewPos.y = Mathf.Floor(vNewPos.y);
        vNewPos.y /= pxPerTile;

        return vNewPos;
    }
}
