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

public class CamManager : MonoBehaviour
{
	[Tooltip("il target osservato dalla camera")]
	public Transform camTarget;

	[Tooltip("valore minimo della view port 0-1")]
	public Vector2 minLim;
	[Tooltip("valore minimo della view port 0-1")]
	public Vector2 maxLim;

	[Tooltip("valore minimo del camTarget")]
	public Vector3 minTargetPos;
	[Tooltip("valore massimo del camTarget")]
	public Vector3 maxTargetPos;
	[Tooltip("valore default del camTarget")]
	public Vector3 defTargetPos;

	[Tooltip("valore z del camTarget")]
	public float zTargetPos;

    private RaycastHit hit;
    private Ray ray;
	private bool isHit, isOutView, isLeft, isRight, isUp, isDown;
    private Transform hitTrn, camTrn;
	private AudioSource jackCable;
    private Vector3 offset, newTargetPos, defJackPos;
    private Vector2 mPos, wPos;

    private void Awake()
    {
        isHit = false;
		isOutView = false;
		isLeft = false;
		isRight = false;
		isUp = false;
		isDown = false;
		camTrn = Camera.main.transform;

		newTargetPos = defTargetPos;
		newTargetPos.z = zTargetPos;
		camTarget.position = newTargetPos;
    }

	public void SetCamera(int val, MouseArea mArea)
	{
		switch (val)
		{
		case 0: //up
			if (!isUp && !isDown)
			{
				newTargetPos.y = maxTargetPos.y;
				isUp = true;
			}
			else if (isDown)
			{
				newTargetPos.y = defTargetPos.y;
				isDown = false;
			}
			break;
		case 1: //down
			if (!isDown && !isUp)
			{
				newTargetPos.y = minTargetPos.y;
				isDown = true;
			}
			else if (isUp)
			{
				newTargetPos.y = defTargetPos.y;
				isUp = false;
			}

			break;
		case 2: //left
			
			if (!isLeft && !isRight)
			{
				newTargetPos.x = minTargetPos.x;
				isLeft = true;
			}
			else if (isRight)
			{
				newTargetPos.x = defTargetPos.x;
				isRight = false;
			}
			break;
		case 3: // right
			
			if (!isRight && !isLeft)
			{
				newTargetPos.x = maxTargetPos.x;
				isRight = true;
			}
			else if (isLeft)
			{
				newTargetPos.x = defTargetPos.x;
				isLeft = false;
			}
			break;
		}

		newTargetPos.z = zTargetPos;
		camTarget.position = newTargetPos;
	}

    private void Update()
    {
		mPos = Input.mousePosition;
		wPos = Camera.main.ScreenToViewportPoint(mPos);

		//Debug.Log(wPos);

        if ( Input.GetButton("Fire1") )
        {
            
            ray = Camera.main.ScreenPointToRay(mPos);
            Physics.Raycast(ray, out hit);

            if (!isHit && hit.transform.CompareTag("Jack") ) 
            {
                isHit = true;
                hitTrn = hit.transform;
                hitTrn.GetComponent<Collider>().enabled = false;
				jackCable = hitTrn.GetComponent<AudioSource>();
				if (jackCable != null) jackCable.Play();

                Physics.Raycast(ray, out hit);
                offset = hitTrn.position - hit.point;

				defJackPos = hitTrn.position;
            }

            if (isHit && hitTrn != null)
            {
				if (hit.transform.CompareTag("Slot"))
				{
					SlotManager sManager = hit.transform.GetComponent<SlotManager>();
					sManager.JackAttach(hitTrn.GetComponent<JackManager>(), defJackPos);
					hitTrn = null;
					isHit = false;
					if (jackCable != null) jackCable.Stop();
					jackCable = null;
				}
				else 
				{
					hitTrn.position = hit.point + offset;
				}
            }
        }

        if (Input.GetButtonUp("Fire1") )
        {
			if (hitTrn != null)
			{
				hitTrn.GetComponent<Collider>().enabled = true;
				hitTrn.position = defJackPos;
				hitTrn = null;
				isHit =false;
				if (jackCable != null) jackCable.Stop();
				jackCable = null;
			}
        }
    }
}
