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

public class Cycle
{
    public int currCycleVal;
    public int currDay;
    private List<TransmissionData> currTransmissionList;


    //param: currentCycle, currentDay, previousTransmissionIDList
    public Cycle(int currCycleVal, int currDay) {
        this.currCycleVal = currCycleVal;
        this.currDay = currDay;
        }

    //Allows player to smooth into cycle;
    IEnumerator PauseBetweenDays(float seconds) {
        yield return new WaitForSeconds(seconds);
    }

    public void StartCycle() {

    //if(pair.Value.StartDay >= 0 && pair.Value.StartDay <= currDay && !pair.Value.TransmissionUsed)

        Debug.Log("new Cycle on day " + currDay);
        //StartCoroutine(PauseBetweenDays(3f));
        currTransmissionList = PopulateTransmission(
            TransmissionManager.Instance.transmissionDatas.Values.Where(t => t.StartDay == -100 && !t.TransmissionUsed).ToList(),
            TransmissionManager.Instance.transmissionDatas.Values.Where(t => t.StartDay >= 0 && t.StartDay <= currDay && !t.TransmissionUsed).ToList(),
            3);

        SwitchBoardManager.Instance.SwitchBoardDailyUpdate(currTransmissionList);

        Debug.Log("currTransmissionList: " + currTransmissionList.Count);

        //All Characters are Dead or Gone
        if(currTransmissionList.Count <= 0)
        {
            GameplayManager.Instance.EndingStoryControl.gameObject.SetActive(true);
            GameplayManager.Instance.EndingStoryControl.StartEndingStory();
        }
    }

    //Update current cycle (i.e. whether player has attached the peg)
    public void UpdateCycle(Character charFrom, Character charTo, TransmissionData currTransmissionData)
    {
        currTransmissionData.TransmissionUsed = true;

        if (currTransmissionData.EffectId == 1)
        {
            if (currTransmissionData.EndGameConnectedText.ContainsKey(charTo.CharId))
            {
                GameplayManager.Instance.EndGameTextLines = new List<string>()
                {
                    currTransmissionData.EndGameConnectedText[charTo.CharId]
                };
            }
            else
            {
                GameplayManager.Instance.EndGameTextLines = new List<string>()
                {
                    ""
                };
            }
            GameplayManager.Instance.EndGameImages = new List<Sprite>() { null };
            GameplayManager.Instance.EndingStoryControl.gameObject.SetActive(true);
            GameplayManager.Instance.EndingStoryControl.StartEndingStory();
            return;
        }

        currTransmissionList.Remove(currTransmissionData);
        if (charFrom != null || charTo != null)
        {
            if(charTo.CharId != CharacterId.NONE && 
                currTransmissionData.ConnectedTransmissionId.ContainsKey(charTo.IsDeath ? CharacterId.Death : charTo.CharId))
            {
                TransmissionManager.Instance.GetTransmissionDataById(
                        currTransmissionData.ConnectedTransmissionId[charTo.IsDeath ? CharacterId.Death : charTo.CharId])
                    .StartDay = -100;
            }
        }
        if(currTransmissionList.Count <= 0)
        {
            GameplayManager.Instance.StartCoroutine(EndCycle());
        }
    }

    //End Cycle
    public IEnumerator EndCycle()
    {
        if (currDay == 0)
        {
            yield return new WaitForSeconds(6.0f);
            AudioManager.Instance.PlaySoundEffect(AudioManager.Instance.EndOfTutorial);
            yield return new WaitForSeconds(8.0f);
        }
        else
        {
            yield return new WaitForSeconds(1.0f);
        }
        GameplayManager.Instance.UpdateGameplayManager();
    }

    public List<TransmissionData> PopulateTransmission(List<TransmissionData> prevTransmissions, List<TransmissionData> newTransmissions, int maxCount){
        prevTransmissions = prevTransmissions.OrderBy(t => Random.value).ToList();
        newTransmissions = newTransmissions.OrderBy(t => Random.value).ToList();

        List<TransmissionData> nextCycleTransmissions = new List<TransmissionData>();

        for(int i = 0; i < prevTransmissions.Count; i++) {
            if(nextCycleTransmissions.Count < maxCount &&
                nextCycleTransmissions.All(t => prevTransmissions[i].ToCharacterId != t.ToCharacterId &&
                prevTransmissions[i].FromCharacterId != t.FromCharacterId &&
                prevTransmissions[i].ToCharacterId != t.FromCharacterId &&
                prevTransmissions[i].FromCharacterId != t.ToCharacterId)) {
                nextCycleTransmissions.Add(prevTransmissions[i]);
            }
        }

        for(int i = 0; i < newTransmissions.Count; i++) {
            if(nextCycleTransmissions.Count < maxCount &&
                nextCycleTransmissions.All(t => newTransmissions[i].ToCharacterId != t.ToCharacterId &&
                newTransmissions[i].FromCharacterId != t.FromCharacterId &&
                newTransmissions[i].ToCharacterId != t.FromCharacterId &&
                newTransmissions[i].FromCharacterId != t.ToCharacterId)) {
                nextCycleTransmissions.Add(newTransmissions[i]);
            }
        }

        return nextCycleTransmissions;
    }

    
}
