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

public class SwitchBoardManager : MonoBehaviour
{
    //DISPLAY
    private static SwitchBoardManager _instance;

    public List<WireHead> WireHeadsListenEnds;
    public List<WireHead> WireHeadsTransmissionEnds;
    public List<WireSocket> WireSockets;

    public Animator SignalAnimator;
    public AudioClip SignalSoundEffect;

    public void Awake() {
        if(Instance == null) {
            Instance = this;
        } else if(Instance != this) {
            Destroy(gameObject);
        }
        _instance = GetComponent<SwitchBoardManager>();
    }
    public static SwitchBoardManager Instance = null;

    public Character GetCharacterById(CharacterId characterId)
    {
        return WireSockets.Select(s => s.Character).FirstOrDefault(c => c.CharId == characterId);
    }

    void Start(){
        TransmissionManager.Instance.Init();
        GameplayManager.Instance.Init();
    }

    public void ResetAllWire()
    {
        WireHeadsListenEnds.ForEach(h => h.ResetWireHead());
        WireHeadsTransmissionEnds.ForEach(h => h.ResetWireHead());
        WireSockets.ForEach(h => h.ResetWireSocket());
    }

    public void SwitchBoardDailyUpdate (List<TransmissionData> currTransmissionDataList)
    {
        SwitchBoardManager.Instance.ResetAllWire();
        foreach (var tran in currTransmissionDataList)
        {
            tran.SetupTransmission();
        }
        foreach (var head in WireHeadsListenEnds)
        {
            //Debug.Log("WireHeadsListenEnds");
            head.InitWireHead(currTransmissionDataList);
        }
        foreach (var head in WireHeadsTransmissionEnds)
        {
            head.InitWireHead(currTransmissionDataList);
        }
        foreach (var socket in WireSockets)
        {
           socket.InitWireSocket(currTransmissionDataList);
        }
    }

    public void OnReciverWireSocketGetPlugged(WireSocket wireSocket, WireHead wireHead)
    {
        AudioManager.Instance.TutorialAudioSource.Stop();
        wireSocket.ConnectedWireHead = wireHead;
        wireHead.CurrentConnectedWireSocket = wireSocket;
        wireHead.ConnectedWireHead.Collider.enabled = false;
        AudioManager.Instance.StopTransmissionVoice();

        SignalAnimator.SetTrigger("Signal");
        AudioManager.Instance.PlaySoundEffect(SignalSoundEffect);

        //play immediate feedback
        if (wireHead.ConnectedWireHead.CurrentConnectedWireSocket.SenderTransmissionData.ImmediateAudioFeedback.ContainsKey(wireSocket.Character.IsDeath ? CharacterId.Death : wireSocket.Character.CharId))
        {
            AudioManager.Instance.PlayImmediateFeedBackVoice(wireHead.ConnectedWireHead.CurrentConnectedWireSocket.SenderTransmissionData.ImmediateAudioFeedback[wireSocket.Character.IsDeath ? CharacterId.Death : wireSocket.Character.CharId]);
        }

        //add ending text
        if (wireHead.ConnectedWireHead.CurrentConnectedWireSocket.SenderTransmissionData.EndGameConnectedText.ContainsKey(wireSocket.Character.IsDeath ? CharacterId.Death : wireSocket.Character.CharId))
        {
            GameplayManager.Instance.EndGameTextLines.Add(wireHead.ConnectedWireHead.CurrentConnectedWireSocket.SenderTransmissionData.EndGameConnectedText[wireSocket.Character.IsDeath ? CharacterId.Death : wireSocket.Character.CharId]);
        }

        //add ending images
        if (wireHead.ConnectedWireHead.CurrentConnectedWireSocket.SenderTransmissionData.EndGameConnectedImage.ContainsKey(wireSocket.Character.IsDeath ? CharacterId.Death : wireSocket.Character.CharId))
        {
            GameplayManager.Instance.EndGameImages.Add(wireHead.ConnectedWireHead.CurrentConnectedWireSocket.SenderTransmissionData.EndGameConnectedImage[wireSocket.Character.IsDeath ? CharacterId.Death : wireSocket.Character.CharId]);
        }
        else
        {
            GameplayManager.Instance.EndGameImages.Add(null);
        }

        //kill character
        foreach (var character in wireHead.ConnectedWireHead.CurrentConnectedWireSocket.SenderTransmissionData.CharacterToKill)
        {
            foreach (var s in WireSockets)
            {
                if (s.Character.CharId == character)
                {
                    s.Character.IsDeath = true;
                }
            }
        }

        StartCoroutine(DelayUpdateCycle(wireHead.ConnectedWireHead.CurrentConnectedWireSocket.Character, wireSocket.Character,
             wireHead.ConnectedWireHead.CurrentConnectedWireSocket.SenderTransmissionData, wireHead.ConnectedWireHead.CurrentConnectedWireSocket.SenderTransmissionData.DelayTime));
    }

    public IEnumerator DelayUpdateCycle(Character charFrom, Character charTo, TransmissionData currTransmissionData, float delay)
    {
        yield return new WaitForSeconds(Mathf.Max(delay, 1.5f));
        GameplayManager.Instance.currCycle.UpdateCycle(charFrom, charTo, currTransmissionData);
    }

    public void OnListernerWireSocketGetPlugged(WireSocket wireSocket, WireHead wireHead)
    {
        wireHead.CurrentConnectedWireSocket = wireSocket;
        WireHeadsListenEnds.ForEach(h => h.OccupyingVoiceChannel = false);
        WireHeadsTransmissionEnds.ForEach(h => h.OccupyingVoiceChannel = false);
        wireHead.OccupyingVoiceChannel = true;
        if (wireSocket.SenderTransmissionData == null)
        {
            wireSocket.ConnectedWireHead = wireHead;
            AudioManager.Instance.PlayTransmissionVoice(AudioManager.Instance.EmptyWireSocketWhiteNoise);
        }
        else
        {
            wireSocket.ConnectedWireHead = wireHead;
            if (!GameplayManager.Instance.IsTutorial)
            {
                AudioManager.Instance.PlayTransmissionVoice(wireSocket.SenderTransmissionData.AudioContent);
            }
            else
            {
                AudioManager.Instance.PlayTransmissionVoice(AudioManager.Instance.EmptyWireSocketWhiteNoise);
            }
        }
    }
}
