/*
 * This file is part of the YSSDWebImage package.
 * (c) Olivier Poitrey <rs@dailymotion.com>
 * (c) Fabrice Aneche
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#import "NSData+YSImageContentType.h"
#if YSSD_MAC
#import <CoreServices/CoreServices.h>
#else
#import <MobileCoreServices/MobileCoreServices.h>
#endif
#import "YSSDImageHEICCoderInternal.h"

// Currently Image/IO does not support WebP
#define kYSSDUTTypeWebP ((__bridge CFStringRef)@"public.webp")
#define kSVGTagEnd @"</svg>"

@implementation NSData (YSImageContentType)

+ (YSSDImageFormat)yssd_imageFormatForImageData:(nullable NSData *)data {
    if (!data) {
        return YSSDImageFormatUndefined;
    }
    
    // File signatures table: http://www.garykessler.net/library/file_sigs.html
    uint8_t c;
    [data getBytes:&c length:1];
    switch (c) {
        case 0xFF:
            return YSSDImageFormatJPEG;
        case 0x89:
            return YSSDImageFormatPNG;
        case 0x47:
            return YSSDImageFormatGIF;
        case 0x49:
        case 0x4D:
            return YSSDImageFormatTIFF;
        case 0x52: {
            if (data.length >= 12) {
                //RIFF....WEBP
                NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
                if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
                    return YSSDImageFormatWebP;
                }
            }
            break;
        }
        case 0x00: {
            if (data.length >= 12) {
                //....ftypheic ....ftypheix ....ftyphevc ....ftyphevx
                NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(4, 8)] encoding:NSASCIIStringEncoding];
                if ([testString isEqualToString:@"ftypheic"]
                    || [testString isEqualToString:@"ftypheix"]
                    || [testString isEqualToString:@"ftyphevc"]
                    || [testString isEqualToString:@"ftyphevx"]) {
                    return YSSDImageFormatHEIC;
                }
                //....ftypmif1 ....ftypmsf1
                if ([testString isEqualToString:@"ftypmif1"] || [testString isEqualToString:@"ftypmsf1"]) {
                    return YSSDImageFormatHEIF;
                }
            }
            break;
        }
        case 0x25: {
            if (data.length >= 4) {
                //%PDF
                NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(1, 3)] encoding:NSASCIIStringEncoding];
                if ([testString isEqualToString:@"PDF"]) {
                    return YSSDImageFormatPDF;
                }
            }
        }
        case 0x3C: {
            if (data.length > 100) {
                // Check end with SVG tag
                NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(data.length - 100, 100)] encoding:NSASCIIStringEncoding];
                if ([testString containsString:kSVGTagEnd]) {
                    return YSSDImageFormatSVG;
                }
            }
        }
    }
    return YSSDImageFormatUndefined;
}

+ (nonnull CFStringRef)yssd_UTTypeFromImageFormat:(YSSDImageFormat)format {
    CFStringRef UTType;
    switch (format) {
        case YSSDImageFormatJPEG:
            UTType = kUTTypeJPEG;
            break;
        case YSSDImageFormatPNG:
            UTType = kUTTypePNG;
            break;
        case YSSDImageFormatGIF:
            UTType = kUTTypeGIF;
            break;
        case YSSDImageFormatTIFF:
            UTType = kUTTypeTIFF;
            break;
        case YSSDImageFormatWebP:
            UTType = kYSSDUTTypeWebP;
            break;
        case YSSDImageFormatHEIC:
            UTType = kYSSDUTTypeHEIC;
            break;
        case YSSDImageFormatHEIF:
            UTType = kYSSDUTTypeHEIF;
            break;
        case YSSDImageFormatPDF:
            UTType = kUTTypePDF;
            break;
        case YSSDImageFormatSVG:
            UTType = kUTTypeScalableVectorGraphics;
            break;
        default:
            // default is kUTTypeImage abstract type
            UTType = kUTTypeImage;
            break;
    }
    return UTType;
}

+ (YSSDImageFormat)yssd_imageFormatFromUTType:(CFStringRef)uttype {
    if (!uttype) {
        return YSSDImageFormatUndefined;
    }
    YSSDImageFormat imageFormat;
    if (CFStringCompare(uttype, kUTTypeJPEG, 0) == kCFCompareEqualTo) {
        imageFormat = YSSDImageFormatJPEG;
    } else if (CFStringCompare(uttype, kUTTypePNG, 0) == kCFCompareEqualTo) {
        imageFormat = YSSDImageFormatPNG;
    } else if (CFStringCompare(uttype, kUTTypeGIF, 0) == kCFCompareEqualTo) {
        imageFormat = YSSDImageFormatGIF;
    } else if (CFStringCompare(uttype, kUTTypeTIFF, 0) == kCFCompareEqualTo) {
        imageFormat = YSSDImageFormatTIFF;
    } else if (CFStringCompare(uttype, kYSSDUTTypeWebP, 0) == kCFCompareEqualTo) {
        imageFormat = YSSDImageFormatWebP;
    } else if (CFStringCompare(uttype, kYSSDUTTypeHEIC, 0) == kCFCompareEqualTo) {
        imageFormat = YSSDImageFormatHEIC;
    } else if (CFStringCompare(uttype, kYSSDUTTypeHEIF, 0) == kCFCompareEqualTo) {
        imageFormat = YSSDImageFormatHEIF;
    } else if (CFStringCompare(uttype, kUTTypePDF, 0) == kCFCompareEqualTo) {
        imageFormat = YSSDImageFormatPDF;
    } else if (CFStringCompare(uttype, kUTTypeScalableVectorGraphics, 0) == kCFCompareEqualTo) {
        imageFormat = YSSDImageFormatSVG;
    } else {
        imageFormat = YSSDImageFormatUndefined;
    }
    return imageFormat;
}

@end
