using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

namespace Caper {
    public class Boat : MonoBehaviour {
        public WindManager windManager;
        public Text windSpeedText;
        public Transform graphics;
        public RectTransform windMenu;
        public Compass compass;

        float desire = 0f;
        bool sails = false;
        float speed = 0f;

        // Use this for initialization
        void Start() {
            StartCoroutine(RockBoat());
        }

        // Update is called once per frame
        void Update() {
            windSpeedText.text = windManager.GetKPHAtPosition(transform.position) + "KPH " + windManager.GetWindDirectionNameAtPosition(transform.position);

            Vector3 wind = windManager.GetWindDirectionAtPosition(transform.position);
            float windIntensity = windManager.GetWindIntensityAtPosition(transform.position);

            float angle = Mathf.Atan2(wind.z, wind.x) * Mathf.Rad2Deg + 90f;
            Vector3 rot = Vector3.forward * angle + Vector3.forward * compass.transform.eulerAngles.z;
            windMenu.rotation = Quaternion.Lerp(windMenu.rotation, Quaternion.Euler(rot), Time.deltaTime * 5f);

            var newSpeed = sails ? windIntensity * (1f - Mathf.Abs(desire) / 360f) * 10f : 0f;
            speed = Mathf.Lerp(speed, newSpeed, Time.deltaTime);

            var newRot = sails ? Quaternion.LookRotation(RotateVector(wind, -desire * Mathf.Deg2Rad), Vector3.up) : transform.rotation;
            transform.rotation = Quaternion.Lerp(transform.rotation, newRot, Time.deltaTime * 0.5f);
            transform.Translate(Vector3.forward * speed * Time.deltaTime);
        }

        Vector3 RotateVector(Vector3 vec, float angle) {
            Vector3 result = Vector2.zero;
            result.x = vec.x * Mathf.Cos(angle) - vec.z * Mathf.Sin(angle);
            result.z = vec.x * Mathf.Sin(angle) + vec.z * Mathf.Cos(angle);
            return result;
        }

        //0 graus é a favor do vento
        public void SetDesire(float dir) {
            desire = dir;
        }

        public void SetSails(bool value) {
            sails = value;
        }

        IEnumerator RockBoat() {
            while (true) {
                var windIntensity = windManager.GetWindIntensityAtPosition(transform.position);
                if (windIntensity <= 0f) {
                    Vector3 rockVector = Vector3.zero;
                    rockVector *= windIntensity;
                    rockVector.y = 180f;
                    var rotTweener = graphics.DOLocalRotate(rockVector, 2f);
                    rotTweener.SetEase(AnimationCurve.EaseInOut(0f, 0f, 2f, 1f));

                    float posY = Random.Range(-.1f, .1f);
                    var posTweener = graphics.DOLocalMoveY(posY, 2f);
                    posTweener.SetEase(AnimationCurve.EaseInOut(0f, 0f, 2f, 1f));

                    yield return new WaitForSeconds(2f);
                    continue;
                }
                else {
                    Vector3 rockVector = Vector3.zero;
                    rockVector.x = Random.Range(-2f, 2f);
                    rockVector.z = Random.Range(-2f, 2f);
                    rockVector *= windIntensity / 2f;
                    rockVector.y = 180f;
                    var rotTweener = graphics.DOLocalRotate(rockVector, 5f / windIntensity);
                    rotTweener.SetEase(AnimationCurve.EaseInOut(0f, 0f, 5f / windIntensity, 1f));

                    float posY = Random.Range(-.1f, .1f);
                    var posTweener = graphics.DOLocalMoveY(posY, 5f / windIntensity);
                    posTweener.SetEase(AnimationCurve.EaseInOut(0f, 0f, 5f / windIntensity, 1f));

                    yield return new WaitForSeconds(5f / windIntensity);
                }
            }
        }
    }
}
