//
//  TestDegreeManager.swift
//  KdanAuto
//
//  Created by 朱东勇 on 2023/2/9.
//

import Foundation

class TestDegreeManager : NSObject {
    var degreeInfo = NSMutableDictionary()
    
    static var tdManager = TestDegreeManager()
    class func shared() -> TestDegreeManager {
        return tdManager
    }
    
    // Clear
    func clearAllHistory() {
        degreeInfo.removeAllObjects()
    }
    
    // Set
    func set(_ degree:Double, fileType:String?) {
        set(degree, fileType: fileType, type: nil)
    }
    
    func set(_ degree:Double, fileType:String?, type:String?) {
        set(degree, fileType: fileType, type: type, fileName: nil)
    }
    
    func set(_ degree:Double, fileType:String?, type:String?, fileName:String?) {
        set(degree, fileType: fileType, type: type, fileName: fileName, refFilePath: nil)
    }
    
    func set(_ degree:Double, fileType:String?, type:String?, fileName:String?, refFilePath:String?) {
        let key = String((fileType ?? "") + (type ?? "") + (fileName ?? "") + (refFilePath ?? ""))
        
        let number = NSNumber(floatLiteral: degree)
        degreeInfo.setValue(number, forKey: key)
    }
    
    //Get
    
    func degreeFor(_ fileType:String?) -> Double {
        return degreeFor(fileType, type: nil)
    }
    
    func degreeFor(_ fileType:String?, type:String?) -> Double {
        return degreeFor(fileType, type: type, fileName: nil)
    }
    
    func degreeFor(_ fileType:String?, type:String?, fileName:String?) -> Double {
        return degreeFor(fileType, type: type, fileName: fileName, refFilePath: nil)
    }
    
    func degreeFor(_ fileType:String?, type:String?, fileName:String?, refFilePath:String?) -> Double {
        let key = String((fileType ?? "") + (type ?? "") + (fileName ?? "") + (refFilePath ?? ""))
        
        if nil != degreeInfo.value(forKey: key) {
            let number = degreeInfo.value(forKey: key) as! NSNumber
            return number.doubleValue
        }
        
        return 0.0
    }
    
    
}

// File Info
extension NSMutableDictionary {
    func degree() -> Double {
        return TestDegreeManager.shared().degreeFor(self.objc().fileType(),
                                                    type: self.objc().type(),
                                                    fileName: self.fileName(),
                                                    refFilePath: self.refFilePath())
    }
}
