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

[ExecuteInEditMode]
public class LatLon2Unity : MonoBehaviour
{
    public double originLatitude;
    public double originLongitude;
    public double areaHeight;
    public double areaWidth;

    public double areaTop;
    public double areaBottom;
    public double areaLeft;
    public double areaRight;

    public bool forceMoveToLatLon = false;
    public double destinationLat;
    public double destinationLon;

    private double currentLatitude;
    private double currentLongitude;
	
	void Update ()
    {
        if(transform.position.z >= 0)
            currentLatitude = originLatitude + ((areaTop - areaBottom) * (Mathf.InverseLerp(0, (float)areaHeight, transform.position.z)));
        else
            currentLatitude = originLatitude - ((areaTop - areaBottom) * (Mathf.InverseLerp(0, (float)areaHeight, -transform.position.z)));

        if(transform.position.x >= 0)
            currentLongitude = originLongitude + ((areaRight - areaLeft) * (Mathf.InverseLerp(0, (float)areaWidth, transform.position.x)));
        else
            currentLongitude = originLongitude - ((areaRight - areaLeft) * (Mathf.InverseLerp(0, (float)areaWidth, -transform.position.x)));

        print("Current Lat: " + currentLatitude + "   Lon: " + currentLongitude);

        if(forceMoveToLatLon)
            MoveToLatLon();
	}

    private void MoveToLatLon ()
    {
        transform.position = new Vector3
            (
                (float)(((destinationLon - areaLeft) / (areaRight - areaLeft)) * (areaWidth)) - (float)(areaWidth / 2f),
                0,
                (float)(((destinationLat - areaBottom) / (areaTop - areaBottom)) * (areaHeight)) - (float)(areaHeight / 2f)
            );
    }
}

