//
//  KMCropTools.swift
//  PDF Reader Pro
//
//  Created by tangchao on 2023/1/1.
//

import Cocoa

@objcMembers class KMCropTools: NSObject {
    class func getPageSize() -> Array<String> {
        return ["A3","A4","A5","A6",
                "Envelope #10","Envelope Choukei 3","Envelope DL",
                "JIS B5","ROC 16K","Super B/A3",
                "Tabloid","Tabloid Oversize",
                "US Legal","US Letter"]
    }
    
    class func getPageSizeValue(_ pageSize: String) -> NSSize {
        var size = NSZeroSize
        if (pageSize == "A3") {
            size = NSSize(width:297 , height: 420)
        } else if (pageSize == "A4") {
            size = NSSize(width:210 , height: 297)
        } else if (pageSize == "A5") {
            size = NSSize(width:148 , height: 210)
        } else if (pageSize == "A6") {
            size = NSSize(width:176 , height: 250)
        } else if (pageSize == "Envelope #10") {
            size = NSSize(width:105 , height: 241)
        } else if (pageSize == "Envelope Choukei 3") {
            size = NSSize(width:120 , height: 235)
        } else if (pageSize == "Envelope DL") {
            size = NSSize(width:110 , height: 220)
        } else if (pageSize == "JIS B5") {
            size = NSSize(width:182 , height: 257)
        } else if (pageSize == "ROC 16K") {
            size = NSSize(width:197 , height: 273)
        } else if (pageSize == "Super B/A3") {
            size = NSSize(width:330 , height: 483)
        } else if (pageSize == "Tabloid") {
            size = NSSize(width:279 , height: 432)
        } else if (pageSize == "Tabloid Oversize") {
            size = NSSize(width:305 , height: 457)
        } else if (pageSize == "US Legal") {
            size = NSSize(width:216 , height: 356)
        } else if (pageSize == "US Letter") {
            size = NSSize(width:216 , height: 279)
        }
        
        return size
    }
    
    class func getPageForegroundBox(_ page: CPDFPage) -> NSRect {
        let marginWidth: CGFloat = 10
        let marginHeight: CGFloat = 10
        
        let imageRep = KMCropTools.newBitmapImageRepForBox(page, .mediaBox)
        let bounds = page.bounds(for: .mediaBox)
        var foregroundBox = imageRep?.foregroundRect() ?? .zero
        if (imageRep == nil) {
            foregroundBox = bounds
        } else if (NSIsEmptyRect(foregroundBox)) {
            let centerPoint = NSPoint(x: bounds.midX, y: bounds.midY)
            let origin = NSPoint(x: round(centerPoint.x), y: round(centerPoint.y))
            foregroundBox.origin = origin
            foregroundBox.size = NSZeroSize
        } else {
            let origin = NSPoint(x: foregroundBox.origin.x+bounds.origin.x, y: foregroundBox.origin.y+bounds.origin.y)
            foregroundBox.origin = origin
        }
        
        return NSIntegralRect(NSInsetRect(foregroundBox, -marginWidth, -marginHeight))
    }
    
    class func newBitmapImageRepForBox(_ page: CPDFPage, _ box: CPDFDisplayBox) -> NSBitmapImageRep? {
        let bounds = page.bounds(for: box)
        var imageRep = NSBitmapImageRep(bitmapDataPlanes: nil,
                                        pixelsWide: Int(NSWidth(bounds)),
                                        pixelsHigh: Int(NSHeight(bounds)),
                                        bitsPerSample: 8,
                                        samplesPerPixel: 4,
                                        hasAlpha: true,
                                        isPlanar: false,
                                        colorSpaceName: .calibratedRGB,
                                        bitmapFormat: NSBitmapImageRep.Format(rawValue: 0),
                                        bytesPerRow: 0,
                                        bitsPerPixel: 32)
        
        if (imageRep != nil) {
            NSGraphicsContext.saveGraphicsState()
            NSGraphicsContext.current = NSGraphicsContext.init(bitmapImageRep: imageRep!)
            NSGraphicsContext.current?.imageInterpolation = .none
            NSGraphicsContext.current?.shouldAntialias = false
            if (page.rotation != 0) {
                var transform = NSAffineTransform()
                if (page.rotation == 90) {
                    transform.translateX(by: NSWidth(bounds), yBy: 0)
                } else if (page.rotation == 180) {
                    transform.translateX(by: NSHeight(bounds), yBy: NSWidth(bounds))
                } else if (page.rotation == 270) {
                    transform.translateX(by: 0, yBy: NSHeight(bounds))
                }
                
                transform.rotate(byDegrees: CGFloat(page.rotation))
                transform.concat()
            }
            
            page.draw(with: box, to: (NSGraphicsContext.current?.cgContext))
            NSGraphicsContext.current?.imageInterpolation = .default
            NSGraphicsContext.restoreGraphicsState()
        }
        
        return imageRep
    }
}