using Giant.Res;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class DIDAMain : InstanceBase<DIDAMain>
{
    private int mainThreadId;
    public Queue<Action> actionsQueue;
    public Queue<BaseObject> nextFrameInstanceAdd;
    public Queue<BaseObject> nextFrameInstanceRemove;
    public List<BaseObject> allInstances = new List<BaseObject>();

    /// <summary>
    /// 版本号
    /// </summary>
    public string gameVersion { get; set; } = "unkonwn";

    // AB包构建号时间
    public string assetbundleBuildVersion { get; set; }

    public string gamePackageBuildVersion {get;set;}

    /// <summary>
    /// 更新后的资源目录
    /// </summary>
    public static string resUpdatePath = Application.persistentDataPath;
#if UNITY_STANDALONE
        static string m_platform = "win";
#elif UNITY_ANDROID
    static string m_platform = "android";
#elif UNITY_IOS
		static string m_platform = "ios";
#endif
    public override void Init()
    {
        using (Giant.Common.ProcessTickTimer.Get("DIDAMain.Instance.Init() "))
        {
            mainThreadId = Thread.CurrentThread.ManagedThreadId;
            actionsQueue = new Queue<Action>();
            nextFrameInstanceAdd = new Queue<BaseObject>();
            nextFrameInstanceRemove = new Queue<BaseObject>();

            string abpath = "/AssetBundles/" + m_platform + "/abasset/";
            Giant.Res.AssetsMgr.SetABPath(Application.streamingAssetsPath + abpath, resUpdatePath + abpath);
            Giant.Res.AssetsMgr.Initialize(Type2SuffixFunc);
            
            //Giant.Resource.AssetBundleManager.Instance.DoStart();
            //RoomManager.Instance.Init();
            //NetManager.Instance.Init();
        }

    }

    public override void Clear()
    {
        foreach (var ins in allInstances)
            ins.Clear();
    }

    /// <summary>
    /// 通过类型获取文件扩展名
    /// </summary>
    /// <param name="t"></param>
    /// <returns></returns>
    private static string Type2SuffixFunc(Type t)
    {

        if (AssetUtily.IsCompatible(t, typeof(GameObject)))
        {
            return ".prefab";
        }

        if (AssetUtily.IsCompatible(t, typeof(ScriptableObject)))
            return ".asset";

        if (AssetUtily.IsCompatible(t, typeof(Texture)) || AssetUtily.IsCompatible(t, typeof(Sprite)))
        {
            return ".png";
        }
        if (AssetUtily.IsCompatible(t, typeof(Material)))
        {
            return ".mat";
        }

        else if (AssetUtily.IsCompatible(t, typeof(TextAsset)))
        {
            return ".bytes";
        }
        else if (AssetUtily.IsCompatible(t, typeof(RuntimeAnimatorController)))
        {
            return ".controller";
        }

        return "";
    }

    public override void DoUpdate()
    {
    }

    public override void DoFixedUpdate()
    {
    }

    public override void DoLateUpdate()
    {
    }

    public override void DoOnGUI()
    {
    }

    public override void DoDestroy()
    {
        //Giant.Resource.AssetBundleManager.Instance.DoDestroy();
        var arr = allInstances.ToArray();
        foreach (var ins in arr)
            ins.DoDestroy();
    }

    public void RunOnMainThread(Action action)
    {
        if (IsMainThread())
        {
            action?.Invoke();
        }
        else
        {
            lock (actionsQueue)
            {
                actionsQueue.Enqueue(action);
            }
        }
    }

    public void AddInstanceNextFrame(BaseObject instance)
    {
        nextFrameInstanceAdd.Enqueue(instance);
    }

    public void RemoveInstanceNextFrame(BaseObject instance)
    {
        nextFrameInstanceRemove.Enqueue(instance);
    }

    public bool IsMainThread()
    {
        return Thread.CurrentThread.ManagedThreadId == mainThreadId;
    }
}
