using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Asset.Processor
{
    [System.Serializable]
    public class AssetsImportHandle : IAssetsImportHandle
    {
        private const string Readable = "readable";
        [SerializeField]
        public AtlasImportOption[] atlasOptions;
        [SerializeField]
        public TextureImportOption[] textureOptions;
        [SerializeField]
        public ModelImportOption[] modelOptions;

        bool IAssetsImportHandle.TryApplyTextureImporter(string assetPath, TextureImporter importer)
        {
            var result = false;
            // 过滤Packages的资源
            if (assetPath.StartsWith(PathDefine.PackageRelative))
                return result;
            if (TryApplyImport(assetPath, importer, atlasOptions) || TryApplyImport(assetPath, importer, textureOptions))
            {
                result = true;
            }
            //是否可读
            importer.isReadable = assetPath.ToLower().Contains(Readable);
            return result;
        }
        bool IAssetsImportHandle.TryApplyModelImporter(string assetPath, ModelImporter importer)
        {
            // 过滤Packages的资源
            if (assetPath.StartsWith(PathDefine.PackageRelative))
                return false;
            if (TryApplyImport(assetPath, importer, modelOptions))
            {
                return true;
            }
            return false;
        }
        private bool TryApplyImport<TImporter>(string assetPath, TImporter importer, AssetImportOption<TImporter>[] options) where TImporter : AssetImporter
        {
            if (options != null)
            {
                foreach (var option in options)
                {
                    if (option.TryApplyImporter(assetPath, importer))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static AssetsImportHandle Default()
        {
            var handle = new AssetsImportHandle();
            handle.atlasOptions = new AtlasImportOption[] { AssetImportOption<TextureImporter>.Create<AtlasImportOption>() };
            handle.textureOptions = new TextureImportOption[] { AssetImportOption<TextureImporter>.Create<TextureImportOption>() };
            handle.modelOptions = new ModelImportOption[] { AssetImportOption<ModelImporter>.Create<ModelImportOption>() };
            return handle;
        }

    }
}
