//
//  PDFCompareTest.swift
//  KdanAuto
//
//  Created by 朱东勇 on 2023/2/16.
//

import Foundation
import AppKit

class PDFCompareTest : AutoTest {
    
    static var pdfSharedInstance = PDFCompareTest()
    override class func shared() -> AutoTest? {
        return pdfSharedInstance
    }
    
    override func clearCacheFiles() {
        
    }
   
    override func process(_ originPath:String, resultPath:String) -> Bool {
        
        return true
    }
    
    override func compareFiles(_ fileName: String) -> NSArray? {
        let files = NSMutableArray()
        
        let checkDirectory = self.checkFileDirectory()
        let originDirectory = self.originFileDirectory()
        let resultDirectory = self.resultFileDirectory()
        
        let fName = NSString(string: fileName).deletingPathExtension
        //        let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
        let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+".jpg")
        //        let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
        
        
        var isDirectory = ObjCBool(false)
        if FileManager.default.fileExists(atPath: resultPath, isDirectory:&isDirectory) {
            // compare screenshoot between result file with check file
            let items = NSMutableArray()
            if (isDirectory.boolValue) {
                let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
                for item in NSArray(array: searchItems) {
                    let ext = NSString(string: item as! String).pathExtension.lowercased()
                    if NSArray(array: ["jpg"]).contains(ext) {
                        let fileName = NSString(string: fName+".jpg"+"/\(item as! String)").deletingPathExtension
                        items.add(fileName)
                    }
                }
            }else {
                items.add(fName)
            }
            
            let nitems = items.sortedArray { str1, str2 in
                let s1 = str1 as! String
                let s2 = str2 as! String
                
                return NSString(string: s1).compare(s2)
            }
            
            for item in nitems {
                let subFileName = item as! String
                let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
                let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
                
                let fileInfo = NSMutableDictionary.fileInfoWith(fileName, // 实际文档名
                                                                refFilePath: (isDirectory.boolValue ? (subFileName+".jpg") : nil),
                                                                resultPath: rComparePath,
                                                                comparePath: cComparePath,
                                                                objc: self)
                files.add(fileInfo)
            }
        }
        
        return files
    }
    
}
