using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Asset.Processor
{
    [System.Serializable]
    public class TextureImportOption : AssetImportOption<TextureImporter>
    {
        private Vector4 spriteBorder;
        private Vector2 spritePivot;

        //public TextureImporterFormat formatWithAlpha = TextureImporterFormat.ASTC_5x5;
        public TextureImporterFormat formatWithoutAlpha;

        protected override string PresetTemplateName => "TextureImporterTemplate";

        protected override void OnInit()
        {
            importKeys = new string[] { "Textures/", "Texture/" };
            formatWithoutAlpha = TextureImporterFormat.ETC2_RGB4;
        }
        protected override void PreApplyImporter(string assetPath, TextureImporter importer)
        {
            spriteBorder = importer.spriteBorder;
            spritePivot = importer.spritePivot;
        }
        protected override void OnApplyImporter(string assetPath, TextureImporter importer)
        {
            importer.spriteBorder = spriteBorder;
            importer.spritePivot = spritePivot;
            bool haveAlpha = importer.DoesSourceTextureHaveAlpha();
            //ios
            //var iosSetting = importer.GetPlatformTextureSettings("iOS");
            //iosSetting.overridden = true;
            //iosSetting.format = formatWithAlpha;
            //iosSetting.textureCompression = TextureImporterCompression.Compressed;
            //textureImporter.SetPlatformTextureSettings(iosSetting);

            //Android
            if (!haveAlpha)
            {
                importer.alphaSource = TextureImporterAlphaSource.None;
                var androidSetting = importer.GetPlatformTextureSettings("Android");
                androidSetting.overridden = true;
                androidSetting.format = formatWithoutAlpha;
                androidSetting.textureCompression = TextureImporterCompression.Compressed;
                importer.SetPlatformTextureSettings(androidSetting);
            }
            
        }
    }
}
