using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.U2D;
using UnityEngine;
using UnityEngine.U2D;

public static class AtlasUtils
{
    public static SpriteAtlas CreateAtlas(string name, List<Sprite> sprites, string path, int packPadding = 2, 
        FilterMode filterMode = FilterMode.Bilinear,
        int maxTextureSize = 2048,
        TextureImporterFormat androidFormat = TextureImporterFormat.ASTC_6x6,
        TextureImporterFormat iosFormat = TextureImporterFormat.ASTC_6x6)
    {
        SpriteAtlas spriteAtlas = new SpriteAtlas();
        spriteAtlas.Add(sprites.ToArray());
        spriteAtlas.SetProjectCommonSetting(name, packPadding, filterMode, maxTextureSize, androidFormat, iosFormat);
        string assetPath = $"{path}/{spriteAtlas.name}.spriteatlas";
        if (File.Exists(assetPath))
        {
            File.Delete(assetPath);
            AssetDatabase.Refresh();
        }
        AssetDatabase.CreateAsset(spriteAtlas,$"{path}/{spriteAtlas.name}.spriteatlas");
        return spriteAtlas;
    }
}

public static partial class Extensions
{
    public static SpriteAtlas SetProjectCommonSetting(this SpriteAtlas spriteAtlas, string name, int packPadding = 2, 
        FilterMode filterMode = FilterMode.Bilinear,
        int maxTextureSize = 2048,
        TextureImporterFormat androidFormat = TextureImporterFormat.ASTC_6x6,
        TextureImporterFormat iosFormat = TextureImporterFormat.ASTC_6x6)
    {
        spriteAtlas.name = name;
        spriteAtlas.SetCommonPackingSetting(packPadding)
            .SetCommonTextureSetting(filterMode)
            .SetCommonAndroidTextureSettings(maxTextureSize, androidFormat)
            .SetCommonIOSTextureSettings(maxTextureSize, iosFormat);
        return spriteAtlas;
    }
    
    public static SpriteAtlas SetCommonPackingSetting(this SpriteAtlas spriteAtlas, int padding = 2)
    {
        SpriteAtlasPackingSettings packingSettings = new SpriteAtlasPackingSettings
        {
            enableTightPacking = false,
            enableRotation = false,
            blockOffset = 1,
            padding = padding
        };
        spriteAtlas.SetPackingSettings(packingSettings);
        return spriteAtlas;
    }
    
    public static SpriteAtlas SetCommonTextureSetting(this SpriteAtlas spriteAtlas, FilterMode filterMode = FilterMode.Bilinear)
    {
        SpriteAtlasTextureSettings textureSettings = new SpriteAtlasTextureSettings
        {
            readable = false,
            anisoLevel = 1,
            filterMode = filterMode,
            generateMipMaps = false,
            sRGB = true
        };
        spriteAtlas.SetTextureSettings(textureSettings);
        return spriteAtlas;
    }
    
    public static SpriteAtlas SetCommonAndroidTextureSettings(this SpriteAtlas spriteAtlas, int maxTextureSize = 2048, TextureImporterFormat format = TextureImporterFormat.ASTC_6x6)
    {
        TextureImporterPlatformSettings settings = new TextureImporterPlatformSettings
        {
            name = "Android",
            maxTextureSize = maxTextureSize,
            format = format,
            overridden = true,
            textureCompression = TextureImporterCompression.Compressed
        };
        spriteAtlas.SetPlatformSettings(settings);
        return spriteAtlas;
    }
    
    public static SpriteAtlas SetCommonIOSTextureSettings(this SpriteAtlas spriteAtlas, int maxTextureSize = 2048, TextureImporterFormat format = TextureImporterFormat.ASTC_4x4)
    {
        TextureImporterPlatformSettings settings = new TextureImporterPlatformSettings
        {
            name = "iPhone",
            maxTextureSize = maxTextureSize,
            format = format,
            overridden = true,
            textureCompression = TextureImporterCompression.Compressed
        };
        spriteAtlas.SetPlatformSettings(settings);
        return spriteAtlas;
    }
}
