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

namespace Caper {
    public class WindManager : MonoBehaviour {
        Wind[] allWinds;
        // Use this for initialization
        void Awake() {
            allWinds = GameObject.FindObjectsOfType<Wind>();
        }

        public Vector3 GetWindDirectionAtPosition(Vector3 worldPosition) {
            Vector3 result = Vector3.zero;
            foreach(var wind in allWinds) {
                result += wind.GetDirection() * wind.GetIntensityAtPosition(worldPosition);
            }
            return result;
        }

        public float GetWindIntensityAtPosition(Vector3 worldPosition) {
            float result = 0f;
            foreach (var wind in allWinds) {
                result += wind.GetIntensityAtPosition(worldPosition);
            }
            return result;
        }

        public string GetWindDirectionNameAtPosition(Vector3 worldPosition) {
            Vector3 dir = GetWindDirectionAtPosition(worldPosition);
            var angle = Mathf.Atan2(dir.z, dir.x);
            if (angle < 0f)
                angle += Mathf.PI * 2;

            var eighthPI = Mathf.PI / 8f;
            if (angle >= eighthPI * 15f || angle < eighthPI)
                return "E";
            if (angle >= eighthPI && angle < eighthPI * 3f)
                return "NE";
            if (angle >= eighthPI * 3f && angle < eighthPI * 5f)
                return "N";
            if (angle >= eighthPI * 5f && angle < eighthPI * 7f)
                return "NW";
            if (angle >= eighthPI * 7f && angle < eighthPI * 9f)
                return "W";
            if (angle >= eighthPI * 9f && angle < eighthPI * 11f)
                return "SW";
            if (angle >= eighthPI * 11f && angle < eighthPI * 13f)
                return "S";
            return "SE";
        }

        public int GetKPHAtPosition(Vector3 worldPosition) {
            return (int)(GetWindIntensityAtPosition(worldPosition) * 100f);
        }
    }
}
