using LightReflectiveMirror;
using Mirror;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class loadingScreen : MonoBehaviour
{
    public string sceneName;
    public int sceneIndex;
    public RectTransform loadingWheel;
    public RectTransform loadingProgress;
    CanvasGroup canvasGroup;

    private LightReflectiveMirrorTransport _LRM;

    public void show()
    {
        canvasGroup.blocksRaycasts = canvasGroup.interactable = true;
    }

    public void hide()
    {
        canvasGroup.blocksRaycasts = canvasGroup.interactable = false;
    }

    void Start()
    {
        LoadingScreen.instance=this;
        DontDestroyOnLoad(gameObject);
        canvasGroup = GetComponent<CanvasGroup>();

        if (_LRM == null)
        {
            _LRM = (LightReflectiveMirrorTransport)Transport.activeTransport;
        }

    }
    AsyncOperation loadingOperation;
    // Update is called once per frame
    void Update()
    {
        loadingWheel.Rotate(new Vector3(0, 0, 1f));

        if (loading)
        {
            loadingProgress.sizeDelta = new Vector2(165f * loadingOperation.progress, loadingProgress.sizeDelta.y);
        }

        if (canvasGroup.interactable && canvasGroup.alpha < 1)
        {
            canvasGroup.alpha += 0.01f;
        }
        if(!canvasGroup.interactable && canvasGroup.alpha > 0)
        {
            canvasGroup.alpha -= 0.01f;
        }
    }

    
    bool loading = false;
    public bool isHost= true;
    public async void load()
    {
        show();
        await Task.Delay(1000);
        loading = true;
        loadingProgress.sizeDelta = new Vector2(0, loadingProgress.sizeDelta.y);
        SceneManager.sceneLoaded += SceneManager_sceneLoaded;
        if(sceneName != null){
            loadingOperation = SceneManager.LoadSceneAsync(sceneName);
        }else{
            loadingOperation = SceneManager.LoadSceneAsync(sceneIndex);
        }
    }

    private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
    {
        hide();
        if (loading && !alreadyConnected)
        {
           if(isHost){host();}else{join();}
           loading = false;
        }
        if(alreadyConnected){
           // Debug.Log()
           SceneData.netSceneData = FindObjectOfType<NetSceneData>();
           SceneData.localPlayer.GetComponent<NetPlayer>().ReturnToSpawn();
           
        }
    }
    public bool alreadyConnected= false;
    public string serverName;
    public bool isPublic;
    public string serverMetadata;
    
    void host()
    {
        _LRM.serverName = serverName;
        _LRM.extraServerData = serverMetadata;
        _LRM.isPublicServer = isPublic;

        _LRM.ServerStart();
        NetworkManager.singleton.StartHost();
    }

    void join()
    {
        NetworkManager.singleton.networkAddress = serverName;
        NetworkManager.singleton.StartClient();
    }
}


public static class LoadingScreen{
    public static loadingScreen instance;
}
