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

namespace Giant.Res {
    public class InstanceAsyncRequest : InstanceRequest
    {
        public InstanceAsyncRequest(Vector3 _pos, Quaternion _rot) 
            : base(_pos, _rot) { 
        
        }

        public override bool isDone {
            get {
                if (error || assetRequest.error) {
                    return true;

                }
                switch (loadState) {
                    case LoadState.Init:
                        return false;
                    case LoadState.Loaded:
                        return true;
                    case LoadState.LoadAssetBundle:
                        break;
                    case LoadState.LoadAsset:
                        {
                            if (!assetRequest.isDone)
                                return false;
                            if (assetRequest.asset == null) {
                                error = true;
                                return true;
                            }

                            asset = GameObject.Instantiate(assetRequest.asset, positon, rotation);
                            loadState = LoadState.Instance;
                            break;
                        }
                    case LoadState.Instance:
                        break;
                    case LoadState.Unload:
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();

                }

                if (loadState != LoadState.Instance)
                    return false;

                loadState = LoadState.Loaded;
                return true;
            }
        }

        internal override void Load()
        {
            assetRequest = AssetsMgr.LoadAssetAsync(path, typeof(UnityEngine.GameObject));
            loadState = LoadState.LoadAsset;
        }

        internal override void Unload()
        {
            loadState = LoadState.Unload;
            base.Unload();
        }
    }
}

