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

public class FunctionSync_MaterialTexture : OneValueSyncObject
{
    Renderer renderer;

    Dictionary<int, Texture> matDic = new Dictionary<int, Texture>();

    private void Start()
    {
        Init();
    }

    public void Init()
    {
        if (!hasInit)
        {
            renderer = GetComponent<Renderer>();
            InitDynamic("MaterialTex_" + gameObject.name, CallBack, ValueType.Int);
        }
    }
    /// <summary>
    /// 设置材质
    /// </summary>
    /// <param name="ResourcePath">Resource文件下加载路径</param>
    public void SetMaterial(int num)
    {
        if(num<0)
        {
            return;
        }
        else if(num>0)
        {

        }

        myint = num;
        SendSync();
        if (num == 0)
        {
            //关闭
            renderer.material.mainTexture = null;
        }
        else
        {
            if (!matDic.ContainsKey(num))
            {
                //StartCoroutine(GetTexture(num, MediaPanel.instance.dicPicture[num].url, a =>
                //{
                //    if (a)
                //    {
                //        renderer.material.mainTexture = matDic[num];
                //    }
                //    else
                //    {
                //        Debug.LogError("下载图片失败:" + num);
                //    }
                //}));
            }
            else
            {
                renderer.material.mainTexture = matDic[num];
            }
        }
    }
    /// <summary>
    /// 下载图片
    /// </summary>
    /// <returns></returns>
    IEnumerator GetTexture(int num, string url1, Action<bool> callback)
    {
        WWW www1 = new WWW(url1);
        yield return www1;
        if (www1.isDone)
        {
            if (!string.IsNullOrEmpty(www1.error))
            {
                Debug.LogError(www1.error);
                MessagePanel.ShowMessage("获取图片失败:" + www1.error, GameObject.Find("Canvas").transform, a => { });
                callback.Invoke(false);
            }
            else
            {
                Debug.Log("下载图片成功:" + num);
                matDic.Add(num, www1.texture);
                callback.Invoke(true);
            }
        }
    }

    /// <summary>
    /// 回调
    /// </summary>
    /// <param name="id"></param>
    public void CallBack(string id, bool isEnterRoom)
    {
        if (myint == 0)
        {
            renderer.material.mainTexture = null;
        }
        else if (!matDic.ContainsKey(myint))
        {
            //StartCoroutine(GetTexture(myint, MediaPanel.instance.dicPicture[myint].url, a =>
            //{
            //    if (a)
            //    {
            //        renderer.material.mainTexture = matDic[myint];
            //    }
            //    else
            //    {
            //        Debug.LogError("下载图片失败:" + myint);
            //    }
            //}));
        }
        else
        {
            renderer.material.mainTexture = matDic[myint];
        }
    }
}