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

public class Rift : MonoBehaviour, IPunObservable
{
    [Header("Visual Components:")]
    [SerializeField] private GameObject openning;
    [SerializeField] private GameObject maintain;
    [SerializeField] private GameObject closing;

    [Header("Enemies:")]
    [SerializeField] private List<GameObject> enemyPrefabs = new List<GameObject>();

    private Dictionary<int, int> enemyType_Quantity;

    WaitForSeconds delayBetweenSpawns = new WaitForSeconds(0.3f);
    WaitForSeconds delayToClose = new WaitForSeconds(1f);

    UnitDifficultySettings unitDifficultySettings;
    UnitDifficultySetter unitDifficultySetter;
    GameObject spawnedEnemy;

    RiftSettings settings;

    public void InitializeRift(RiftSettings settings)
    {
        this.settings = settings;
        enemyType_Quantity = settings.enemyIndexes_Quantity.ToDictionary(entry => entry.Key, entry => entry.Value);
        Debug.Log("Total Types of enemies: " + enemyType_Quantity.Keys.Count);
        unitDifficultySettings = GameDifficultyController.Instance.GetCurrentDifficultySettings();
    }

    public void StartSpawning()
    {
        if (PhotonNetwork.IsMasterClient)
            StartCoroutine(SpawnEnemiesOverTime());
    }

    IEnumerator SpawnEnemiesOverTime()
    {
        SetVisual(0);

        yield return new WaitForSeconds(0.8f);

        SetVisual(1);

        for (int i = 0; i < enemyType_Quantity.Keys.Count; i++)
        {
            for (int j = 0; j < enemyType_Quantity[i]; j++)
            {
                spawnedEnemy = PhotonNetwork.Instantiate("Enemies/" + enemyPrefabs[i].name, this.transform.position, this.transform.rotation);

                unitDifficultySetter = spawnedEnemy.GetComponentInChildren<UnitDifficultySetter>();
                unitDifficultySetter.InitializeUnitDifficulty(unitDifficultySettings);

                yield return delayBetweenSpawns;
            }
        }

        SetVisual(2);

        yield return delayToClose;

        SetVisual(-1);

        yield return delayBetweenSpawns;

        DestroyAfterSpawn();
    }

    private void DestroyAfterSpawn()
    {
        if (PhotonNetwork.LocalPlayer.IsMasterClient)
            PhotonNetwork.Destroy(this.gameObject);
    }


    private void SetVisual(int index)
    {
        switch (index)
        {
            case -1:
                openning.SetActive(false);
                maintain.SetActive(false);
                closing.SetActive(false);
                break;
            case 0:
                openning.SetActive(true);
                maintain.SetActive(false);
                closing.SetActive(false);
                break;
            case 1:
                openning.SetActive(false);
                maintain.SetActive(true);
                closing.SetActive(false);
                break;
            case 2:
                openning.SetActive(false);
                maintain.SetActive(false);
                closing.SetActive(true);
                break;
            default:
                break;
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if(stream.IsWriting)
        {
            stream.SendNext(openning.activeSelf);
            stream.SendNext(maintain.activeSelf);
            stream.SendNext(closing.activeSelf);
        }
        if(stream.IsReading)
        {
            openning.SetActive((bool)stream.ReceiveNext());
            maintain.SetActive((bool)stream.ReceiveNext());
            closing.SetActive((bool)stream.ReceiveNext());
        }
    }
}
