using UnityEngine;
using System.Collections;

public class TopSocket : MonoBehaviour
{
    public enum TopSocketState
    {
        idle,
        pluggedInIdle,
        callReceived,
        pluggedInCorrect,
        pluggedInIncorrect
    }
    public bool ignoreSystem;
    [ReadOnly] public SocketChannelSettings channel;
    [ReadOnly] public TopSocketState currentState;

    public CableGrabSphere connectedCableSphere;
    public Rigidbody socketBody;
    public MeshRenderer lightRenderer;
    public MeshRenderer validationRenderer;
    
    public void Awake ()
    {
        socketBody = GetComponent<Rigidbody>();
    }

    private void Start ()
    {
        if (!ignoreSystem)
        {
            TopSocketController.addSocket(this);
        }
    }

    private void OnDestroy ()
    {
        TopSocketController.removeSocket(this);
    }

    public void setConnectedCableSphere (CableGrabSphere sphereToConnect)
    {
        if (sphereToConnect != null)
        {
            if (channel == null)
            {
                setState(TopSocketState.pluggedInIdle);
            }
            else if (sphereToConnect.parentCable.channel.GUID == channel.GUID)
            {
                setState(TopSocketState.pluggedInCorrect);
            }
            else
            {
                setState(TopSocketState.pluggedInIncorrect);
            }
        }
        else
        {
            if (channel == null)
            {
                setState(TopSocketState.idle);
                AudioManager.instance.Play("wire_unplug_normal");
            }
            else
            {
                setState(TopSocketState.callReceived);
                if (connectedCableSphere != null && connectedCableSphere.parentCable.channel.GUID == channel.GUID)
                {
                    AudioManager.instance.Play("wire_unplug_incorrect");
                }
                else
                {
                    AudioManager.instance.Play("wire_unplug_normal");
                }
            }
        }
        connectedCableSphere = sphereToConnect;
    }

    public void setChannel (SocketChannelSettings channelToSet)
    {
        channel = channelToSet;
        if (channelToSet != null)
        {
            if (connectedCableSphere == null)
            {
                setState(TopSocketState.callReceived);
            }
            else if (connectedCableSphere.parentCable.channel.GUID == channel.GUID)
            {
                setState(TopSocketState.pluggedInCorrect);
            }
            else
            {
                setState(TopSocketState.pluggedInIncorrect);
            }
        }
        else
        {
            setState(TopSocketState.idle);
        }
    }

    public void setState (TopSocketState stateToSet)
    {
        if (stateToSet != currentState)
        {
            switch (stateToSet)
            {
                case TopSocketState.idle:
                {
                    if (currentState == TopSocketState.pluggedInCorrect)
                    {
                        //SFX Call complete success
                        
                        AudioManager.instance.Play("connection_successful");
                        IncomingConnectionController.instance.setScoreType(ScoreType.callCompleted);
                    }
                    else
                    {
                        //SFX Call complete failed
                        AudioManager.instance.Play("light_failed_spark");
                        IncomingConnectionController.instance.setScoreType(ScoreType.callFailed);
                    }
                    lightRenderer.material = HayWiresSettings.socketSettings.lightOffMaterial;
                    validationRenderer.material = HayWiresSettings.socketSettings.validationOffMaterial;
                    break;
                }
                case TopSocketState.pluggedInIdle:
                {
                    //SFX regular plugged in
                    AudioManager.instance.Play("wire_insert_normal");
                    lightRenderer.material = HayWiresSettings.socketSettings.lightOffMaterial;
                    validationRenderer.material = HayWiresSettings.socketSettings.validationOffMaterial;
                    break;
                }
                case TopSocketState.callReceived:
                {
                    //SFX call received
                    AudioManager.instance.Play("light_turn_on");
                    lightRenderer.material = channel.channelSocketMaterial;
                    validationRenderer.material = HayWiresSettings.socketSettings.validationOffMaterial;
                    break;
                }
                case TopSocketState.pluggedInCorrect:
                {
                    if (currentState == TopSocketState.idle)
                    {
                        //SFX call received
                        AudioManager.instance.Play("light_turn_on");
                    }
                    //SFX plugged in Correct
                    AudioManager.instance.playRandomConvo();
                    AudioManager.instance.Play("wire_insert_correct");
                    lightRenderer.material = channel.channelSocketMaterial;
                    validationRenderer.material = HayWiresSettings.socketSettings.validationTrueMaterial;
                    break;
                }
                case TopSocketState.pluggedInIncorrect:
                {
                    if (currentState == TopSocketState.idle)
                    {
                        //SFX call received
                        AudioManager.instance.Play("light_turn_on");
                    }
                    //SFX plugged in Incorrect
                    AudioManager.instance.playRandomConvo();
                    AudioManager.instance.Play("wire_insert_incorrect");
                    lightRenderer.material = channel.channelSocketMaterial;
                    validationRenderer.material = HayWiresSettings.socketSettings.validationFalseMaterial;
                    break;
                }
            }
            currentState = stateToSet;
        }
    }

    public void clearSocket ()
    {
        connectedCableSphere = null;
        setChannel(null);
    }
}
