using System;
using Fusion;
using Fusion.Addons.ConnectionManagerAddon;
using Paroxe.PdfRenderer;
using UnityEngine;
using UnityEngine.UI;
using static tabletManager;

public class tabletManager : NetworkBehaviour
{
    [System.Serializable]
    public enum Items
    {
        pen=0,
        controller=1,
        image=2,
        video=3,
        spark=4


    }
    
    public GameObject SkyBoxItem;
    public GameObject createPdfButton;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    public override void Spawned()
    {
        if (Object.HasStateAuthority)
        {
            Create(0);
        }
        for (int i = 0; i < skyboxes.Length; i++)
        {
            GameObject item = Instantiate(SkyBoxItem, SkyBoxItem.transform.parent);
            item.name = i.ToString();
            item.GetComponentInChildren<RawImage>().texture=skyboxes[i].mainTexture;
            item.SetActive(true);
        }

    }
    public void createImage(GameObject i)
    {
        if (!Object.HasStateAuthority)
            return;

        ConnectionManager.instance.CreateImage(itemPlace, image, "http://194.62.43.230:8000/" + ApiConnection.instance.imageLinks[int.Parse(i.name)]);
    } 
    public void createPdf()
    {
        if (!Object.HasStateAuthority)
            return;

        ConnectionManager.instance.CreateImage(itemPlace, image, pdfViewer.Container.transform.GetChild(pdfViewer.CurrentPageIndex).GetComponent<RawImage>());
    } 
    public void createVideo(GameObject i)
    {
        if (!Object.HasStateAuthority)
            return;
        
        GetComponent<VideoPlayerManager>().Show(ApiConnection.instance.videoLinks[int.Parse(i.name)]);
       // ConnectionManager.instance.CreateVideo(itemPlace, video, ApiConnection.instance.videoLinks[int.Parse(i.name)]);
    }
    public void createPdf(GameObject i)
    {
        if (!Object.HasStateAuthority)
            return;
        //  RPC_SetPdf(i.name);
        
        print(ApiConnection.instance.pdfLinks[int.Parse(i.name)]);
        pdfViewer.FileURL = "http://194.62.43.230:8000/" + ApiConnection.instance.pdfLinks[int.Parse(i.name)];
        pdfViewer.FilePath = "http://194.62.43.230:8000/" + ApiConnection.instance.pdfLinks[int.Parse(i.name)];
        pdfViewer.gameObject.SetActive(true);
        //  GetComponent<VideoPlayerManager>().Show(ApiConnection.instance.videoLinks[int.Parse(i.name)]);
        // ConnectionManager.instance.CreateVideo(itemPlace, video, ApiConnection.instance.videoLinks[int.Parse(i.name)]);
    }
    [Rpc(RpcSources.InputAuthority, RpcTargets.All)]
    private void RPC_SetPdf(string name)
    {
        print(ApiConnection.instance.pdfLinks[int.Parse(name)]);
        pdfViewer.FileURL = "http://194.62.43.230:8000/" + ApiConnection.instance.pdfLinks[int.Parse(name)];
        pdfViewer.FilePath = "http://194.62.43.230:8000/" + ApiConnection.instance.pdfLinks[int.Parse(name)];
        pdfViewer.gameObject.SetActive(true);
    }

    public PDFViewer pdfViewer;

    // Update is called once per frame
    void Update()
    {
    
        createPdfButton.gameObject.SetActive( pdfViewer.gameObject.activeSelf&& pdfViewer.IsLoaded && pdfViewer.CurrentPageIndex>=0);


            if (Input.GetKeyDown(KeyCode.P))
            Create(0);
        if (Input.GetKeyDown(KeyCode.O))
            Create(1);
        if (Input.GetKeyDown(KeyCode.I))
            Create(2);
        if (Input.GetKeyDown(KeyCode.U))
            Create(3);

    }
    public NetworkObject image;
    public NetworkObject video;
    public NetworkObject spark;
    public Transform penPlace, itemPlace;
    public void Create(int itemIndex)
    {
        if (!Object.HasStateAuthority)
            return;
        Items item = (Items)itemIndex;
        switch (item)
        {
            case Items.pen:
                ConnectionManager.instance.CreateMarker(penPlace, Color.blue);
                break;
            case Items.controller:
                ConnectionManager.instance.CreateLaser(itemPlace);

                break;
            case Items.image:

                ConnectionManager.instance.CreateImage(itemPlace, image, "https://kakheroshd.ir:448/kolis/uploads/1741811777_banner.jpg");
                break;
            case Items.video:
                ConnectionManager.instance.CreateVideo(itemPlace, video, "https://kakheroshd.ir:448/kolis/uploads/1741811777_banner.jpg");
                break;
            case Items.spark:
                ConnectionManager.instance.CreateSpark(spark,itemPlace);
                break;
        }
    }
    public void Exit()
    {
        Debug.Log("exit");
        Destroy(EN.instance.gameObject);
        Destroy(ConnectionManager.instance.gameObject);
        //Application.LoadLevel(0);
    }
    [Rpc(RpcSources.InputAuthority, RpcTargets.All)]
    public void RPC_SetSkybox(int index)
    {
        if (index >= 0 && index < skyboxes.Length)
        {
            RenderSettings.skybox = skyboxes[index];
            DynamicGI.UpdateEnvironment(); // برای آپدیت نورپردازی
        }
    }

    public void SetSkyBox(GameObject i)
    {
        int index = int.Parse(i.name);
        if (Runner.IsServer)
        {
            RPC_SetSkybox(index); // اجرای RPC برای تغییر Skybox در همه‌ی کلاینت‌ها
        }
        else
        {
            Runner.SessionInfo.IsVisible = false; // اطمینان از وجود دسترسی شبکه‌ای
            RPC_SetSkybox(index);
        }
    }
    public Material[] skyboxes;
}