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

public class DoorLerp : InteractableObject
{
    //if this seems broken, it's because you need to re-adjust the dot-product check object (make empty under door, move down to dot product, copy transform)

    public Transform doorFrameTransform;
    public Transform doorTransform;
    public Transform playerTransform;

    public AnimationCurve curve;
    public Animator doorAnim;

    public Transform doorClockwiseTransform;
    public Transform doorAnticlockwiseTransform;

    public DoorCollision doorCollision;

    private FMODUnity.StudioEventEmitter fmod_doorLocked;
    private FMODUnity.StudioEventEmitter fmod_doorOpen;

    public bool locked = false;

    [HideInInspector]
    public float sideDoor;

    bool rotating = false;
    bool clockwise = true;
  
    bool collision = false;
    bool ignoreCollision = false;

    bool openDoor = true;

    Vector3 closedVector;
    Vector3 openRightVector;
    Vector3 openLeftVector;

    public void Start()
    {
        fmod_doorLocked = GetComponent<FMODUnity.StudioEventEmitter>();
        fmod_doorOpen = GetComponent<FMODUnity.StudioEventEmitter>();

        closedVector = doorFrameTransform.rotation.eulerAngles;
        openRightVector = doorFrameTransform.rotation.eulerAngles;
        openRightVector.y += 90;
        openLeftVector = doorFrameTransform.rotation.eulerAngles;
        openLeftVector.y -= 90;
    }

    public void Update()
    {
        if (!ignoreCollision)
            collision = doorCollision.collision;
    }

    public override void Interact()
    {
        if (locked) fmod_doorLocked.Play();
        else fmod_doorOpen.Play();

        sideDoor = DoorOpenDirection(doorFrameTransform.forward, (doorFrameTransform.position - doorTransform.transform.position), doorFrameTransform.up);
        float sidePlayer = PlayerToDoorDirection(doorFrameTransform.forward, (doorFrameTransform.position - playerTransform.transform.position), doorFrameTransform.up);

        if (sideDoor == 0 && !rotating) //if door is closed
        {
            if (sidePlayer <0)
            {
                doorAnim.Play("DoorHandle");
                clockwise = true;                  
                if (!locked)
                {
                    StartCoroutine(doorOpen(doorClockwiseTransform.rotation, Quaternion.Euler(openRightVector)));
                }
                   
            }
            else if (sidePlayer>0)
            {
                doorAnim.Play("DoorHandle");
                clockwise = false;
                if (!locked)
                {
                    StartCoroutine(doorOpen(doorAnticlockwiseTransform.rotation, Quaternion.Euler(openLeftVector)));
                }  
            }
        }
        else if (sideDoor>0 && !rotating) //if door is open towards right
        {
            if (openDoor)
            {
                clockwise = true;
                if (!locked)
                    StartCoroutine(doorOpen(doorClockwiseTransform.rotation, Quaternion.Euler(openRightVector)));
            }
            else if (!openDoor)
            {
                clockwise = true;
                if (!locked)
                    StartCoroutine(doorOpen(doorClockwiseTransform.rotation, Quaternion.Euler(closedVector)));
            }
        }
        else if (sideDoor<0 && !rotating) //if door is open towards left
        {
            if (openDoor)
            {
                clockwise = false;
                if (!locked)
                    StartCoroutine(doorOpen(doorAnticlockwiseTransform.rotation, Quaternion.Euler(openLeftVector)));
            }
            else if (!openDoor)
            {
                clockwise = false;
                if (!locked)
                    StartCoroutine(doorOpen(doorAnticlockwiseTransform.rotation, Quaternion.Euler(closedVector)));
            }
        }
       
    }

    public IEnumerator doorOpen(Quaternion start, Quaternion end)
    {
        float i = 0;

        ignoreCollision = true;
        collision = false;
        while (i<1 && !collision) //while not in collision with anything
        {
            rotating = true;
            float j = curve.Evaluate(i);
            if (clockwise)
            {
                if (j > 0.98)
                    j = 1;
                doorClockwiseTransform.rotation = Quaternion.Lerp(start, end, j);
            }
            else if (!clockwise)
            {
                if (j > 0.98)
                    j = 1;
                doorAnticlockwiseTransform.rotation = Quaternion.Lerp(start, end, j);
            }

            if (j > 0.02)
                ignoreCollision = false;
            i += (Time.deltaTime*0.6f);
            yield return new WaitForEndOfFrame();
        }
      
        rotating = false;

        if (openDoor) //need !rotating?
        {
            openDoor = false;
            displayName = "Close Door";
        }
        else if (!openDoor)
        {
            openDoor = true;
            displayName = "Open Door";
        }
        sideDoor = DoorOpenDirection(doorFrameTransform.forward, (doorFrameTransform.position - doorTransform.transform.position), doorFrameTransform.up);
    }

    //returns -1 when to the left, 1 to the right, and 0 for forward/backward
    public float DoorOpenDirection(Vector3 forward, Vector3 targetDir, Vector3 up)
    {
        Vector3 perpendicular = Vector3.Cross(forward, targetDir);
        float direction = Vector3.Dot(perpendicular, up);

        if (direction > 0.1)
            return 1.0f;
        else if (direction < -0.1)
            return -1.0f;
        else
            return 0.00f;
    }

    public float PlayerToDoorDirection(Vector3 forward, Vector3 targetDir, Vector3 up)
    {
        Vector3 perpendicular = Vector3.Cross(forward, targetDir);
        float direction = Vector3.Dot(perpendicular, up);

        if (direction > 0.0)
            return 1.0f;
        else if (direction < 0.0)
            return -1.0f;
        else
            return 0.00f;
    }
}
