/*
<copyright file="BGExcelImageAsset.cs" company="BansheeGz">
    Copyright (c) 2018-2024 All Rights Reserved
</copyright>
*/

using System.IO;
using NPOI.SS.UserModel;
using UnityEditor;
using UnityEngine;

namespace BansheeGz.BGDatabase.Editor
{
    public class BGExcelImageAsset
    {
        private readonly Texture texture;
        public readonly BGExcelImageField imageField;
        public readonly BGEntity entity;
        private readonly string path;
        public readonly PictureType pictureType = PictureType.None;

        public Texture Texture => texture;

        public bool IsValid => texture != null && pictureType != PictureType.None;

        public bool NoValue { get; }

        public BGEntity Entity => entity;

        public BGExcelImageAsset(BGExcelImageField imageField, BGEntity entity)
        {
            this.imageField = imageField;
            this.entity = entity;
            var address = imageField.GetStoredValue(entity.Index);
            if (string.IsNullOrEmpty(address))
            {
                NoValue = true;
                return;
            }

            var isSprite = imageField.field is BGFieldUnitySprite;
            if (isSprite)
            {
                var location = new BGFieldUnitySprite.SpriteLocation(address);
                //no multi sprites
                if (location.Location != BGFieldUnitySprite.LocationEnum.Single) return;
                address = location.AssetPath;
            }

            texture = imageField.loaderManager.TryToLoad<Texture>(address, imageField.loader);
            if (texture == null) return;

            path = AssetDatabase.GetAssetPath(texture);
            if (path == null) return;
            if (isSprite)
            {
                //location is not enough? check the actual settings
                var importer = AssetImporter.GetAtPath(path);
                if (!(importer is TextureImporter textureExporter)) return;
                if (textureExporter.textureType != TextureImporterType.Sprite) return;
                if (textureExporter.spriteImportMode != SpriteImportMode.Single) return;
            }

            pictureType = GetPictureType(path);
        }

        public byte[] Content
        {
            get => File.ReadAllBytes(path);
            set
            {
                File.WriteAllBytes(path, value);
                AssetDatabase.ImportAsset(path);
            }
        }

        private static PictureType GetPictureType(string path)
        {
            var ext = Path.GetExtension(path);
            switch (ext)
            {
                case ".jpeg":
                case ".jpg": return PictureType.JPEG;
                case ".png": return PictureType.PNG;
                case ".tiff": return PictureType.TIFF;
                case ".gif": return PictureType.GIF;
                case ".bmp": return PictureType.BMP;
                case ".pict": return PictureType.PICT;
            }

            return PictureType.None;
        }

        public static string GetExtension(PictureType type)
        {
            //tiff,jpg,tga,png,gif,bmp,iff,pict <- Unity supported
            switch (type)
            {
                case PictureType.PICT: return "pict";
                case PictureType.JPEG: return "jpeg";
                case PictureType.PNG: return "png";
                case PictureType.GIF: return "gif";
                case PictureType.TIFF: return "tiff";
                case PictureType.BMP: return "bmp";
            }

            return null;
        }
    }
}