using System.Collections;
using System.Collections.Generic;
using HedgehogTeam.EasyTouch;
using UnityEngine;

public class WireHead : MonoBehaviour
{
    public Vector3 OriginalPos;
    public WireSocket CurrentConnectedWireSocket;
    public Collider Collider;
    public bool IsTransmissionEnd;
    public WireHead ConnectedWireHead;
    public bool OccupyingVoiceChannel;
    public QuickDrag QuickDrag;

    public bool ConnectedToSender
    {
        get { return CurrentConnectedWireSocket != null && CurrentConnectedWireSocket.IsSender; }
    }

    public bool Connected
    {
        get { return CurrentConnectedWireSocket != null; }
    }

    void OnTriggerEnter(Collider col)
    {
        if (col.tag == "WireSocket")
        {
            CurrentConnectedWireSocket = col.gameObject.GetComponent<WireSocket>();
        }
    }

    void OnTriggerExit(Collider col)
    {
        if (col.tag == "WireSocket")
        {
            AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.OnWireHeadUnPlugged);
            if (OccupyingVoiceChannel)
            {
                AudioManager.Instance.StopTransmissionVoice();
            }
            if (CurrentConnectedWireSocket != null && CurrentConnectedWireSocket.ConnectedWireHead == this)
            {
                CurrentConnectedWireSocket.ConnectedWireHead = null;
            }
            CurrentConnectedWireSocket = null;
        }
    }

    void Awake()
    {
        OriginalPos = transform.position;
    }

    void Start()
    {
        QuickDrag = GetComponent<QuickDrag>();
    }

    public void OnDragged(Gesture gesture)
    {
        AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.OnWireHeadPicked);
        transform.forward = Vector3.down;
        if (IsTransmissionEnd && !ConnectedWireHead.Connected)
        {
            StartCoroutine(DelayReturnToOriginalPos());
        }
    }

    IEnumerator DelayReturnToOriginalPos()
    {
        yield return new WaitForSeconds(0.2f);
        QuickDrag.StopDrag();
        AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.OnWireHeadPlugInvalid);
        ReturnToOriginalPos();
    }

    public void OnDropped(Gesture gesture)
    {
        if (CurrentConnectedWireSocket != null)
        {
            if (CurrentConnectedWireSocket.ConnectedWireHead != null)
            {
                AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.OnWireHeadPlugInvalid);
                ReturnToOriginalPos();
            }
            else if (IsTransmissionEnd && !ConnectedWireHead.ConnectedToSender)
            {
                AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.OnWireHeadPlugInvalid);
                ReturnToOriginalPos();
            }
            else if (CurrentConnectedWireSocket.IsSender && IsTransmissionEnd)
            {
                AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.OnWireHeadPlugInvalid);
                ReturnToOriginalPos();
            }
            else if (CurrentConnectedWireSocket.IsReciver && !IsTransmissionEnd)
            {
                AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.OnWireHeadPlugInvalid);
                ReturnToOriginalPos();
            }
            else if (!IsTransmissionEnd)
            {
                transform.position = new Vector3(CurrentConnectedWireSocket.gameObject.transform.position.x, CurrentConnectedWireSocket.gameObject.transform.position.y,
                    transform.position.z);
                SwitchBoardManager.Instance.OnListernerWireSocketGetPlugged(CurrentConnectedWireSocket, this);
                AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.OnWireHeadPlugValid);

            }
            else if (IsTransmissionEnd)
            {
                transform.position = new Vector3(CurrentConnectedWireSocket.gameObject.transform.position.x, CurrentConnectedWireSocket.gameObject.transform.position.y,
                    transform.position.z);
                SwitchBoardManager.Instance.OnReciverWireSocketGetPlugged(CurrentConnectedWireSocket, this);
                AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.OnWireHeadPlugValid);
                Collider.enabled = false;
            }
        }
        else
        {
            AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.OnWireHeadDropped);
            ReturnToOriginalPos();
        }
    }


    public void ReturnToOriginalPos()
    {
        if (CurrentConnectedWireSocket != null && 
            CurrentConnectedWireSocket.ConnectedWireHead == this)
        {
            CurrentConnectedWireSocket.ConnectedWireHead = null;
        }
        CurrentConnectedWireSocket = null;
        transform.forward = Vector3.forward;
        transform.position = OriginalPos;
    }

    public void ResetWireHead()
    {
        OccupyingVoiceChannel = false;
        ReturnToOriginalPos();
        Collider.enabled = true;
    }

    public void InitWireHead(List<TransmissionData> datas)
    {
    }
}
