//
//  ThumbnailProvider.m
//  PanoThumb
//
//  Created by Omar Mohamed Ali Mudhir on 15/06/18.
//  Copyright © 2018 OmixLab ltd. All rights reserved.
//

#import "ThumbnailProvider.h"


struct PPIThumb
{
    int width;
    int height;
    int comp;
    /*
    bool valid()
    {
        return (width == 128 && height == 128 && comp == 4);
    }
    */
};

struct PPIDocVersion
{
    int major;
    int minor;
};

struct PPISoftVersion
{
    int major;
    int minor;
    int fix;
    int build;
};

struct PPIHeader
{
    char magic[4];
    struct PPIDocVersion doc_version;
    struct PPISoftVersion soft_version;
    struct PPIThumb thumb_header;
    /*
    bool valid()
    {
        if (strcmp(magic, "PPI") != 0)
            return false;
        if (doc_version.major != 0 || doc_version.minor != 1)
            return false;
        if (!thumb_header.valid())
            return false;
        return true;
    }
    */
};

@implementation ThumbnailProvider

- (void)provideThumbnailForFileRequest:(QLFileThumbnailRequest *)request completionHandler:(void (^)(QLThumbnailReply * _Nullable, NSError * _Nullable))handler {
    
    // There are three ways to provide a thumbnail through a QLThumbnailReply. Only one of them should be used.
    /*

    // First way: Draw the thumbnail into the current context, set up with UIKit's coordinate system.
    handler([QLThumbnailReply replyWithContextSize:request.maximumSize currentContextDrawingBlock:^BOOL {
        // Draw the thumbnail here.
        // Return YES if the thumbnail was successfully drawn inside this block.
        return YES;
    }], nil);
*/
    
    // Second way: Draw the thumbnail into a context passed to your block, set up with Core Graphics' coordinate system.
    
    CGRect r = CGRectMake(0, 0, 128, 128);
    
    handler([QLThumbnailReply replyWithContextSize:r.size drawingBlock:^BOOL(CGContextRef  _Nonnull context) {
        // Draw the thumbnail here.

        NSLog(@"thumbnail generated");
        NSURL* u = [request fileURL];
        FILE* fp = fopen([u fileSystemRepresentation], "rb");
        if (fp)
        {
            struct PPIHeader header;
            fread(&header, sizeof(struct PPIHeader), 1, fp);
            int w = header.thumb_header.width;
            int h = header.thumb_header.height;
            int c = header.thumb_header.comp;

            //uint8_t* data = (uint8_t*)malloc(h[0]*h[1]*h[2]);
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            CGContextRef bmp = CGBitmapContextCreate(NULL, w, h, 8, w*c, colorSpace, kCGImageAlphaPremultipliedLast);
            
            uint8_t* data_dst = (uint8_t*)CGBitmapContextGetData(bmp);
            uint8_t* data_src = (uint8_t*)malloc(w*h*c);
            fread(data_src, w*h*c, 1, fp);
            fclose(fp);
            
            for(int i = 0; i < w*h; i++)
            {
                data_dst[i * 4 + 0] = data_src[i * c + 0];
                data_dst[i * 4 + 1] = data_src[i * c + 1];
                data_dst[i * 4 + 2] = data_src[i * c + 2];
                data_dst[i * 4 + 3] = 255;
            }
            
            free(data_src);
     
            CGImageRef img = CGBitmapContextCreateImage(bmp);
            CGRect r2 = CGRectMake(0, 0, 128*request.scale, 128*request.scale);
            CGContextDrawImage(context, r2, img);
        }

        // Return YES if the thumbnail was successfully drawn inside this block.
        return YES;
    }], nil);
  /*
    // Third way: Set an image file URL.
    handler([QLThumbnailReply replyWithImageFileURL:[NSBundle.mainBundle URLForResource:@"fileThumbnail" withExtension:@"jpg"]], nil);
    
    */
}

@end
