import Foundation


class PrinterManager: NSObject {
    
    //MARK: - class variables
    
    fileprivate static var queue = DispatchQueue(label: "sync_queue", qos: .userInitiated)
    
    enum PrintError: String, LocalizedError {
        
        case Success
        
        case ConnectErrorTimeout
        case ConnectErrorSocket
        case ConnectErrorUnkown

        case SendDataError
        
        var errorDescription: String? {
            
            return rawValue
        }
    }

    static func printTaskFor(image: UIImage, inPrinter ipAddress: String, model: String, openDrawer: Bool, completion: @escaping ReturnBlock) {
        
        let printer = Printer(model: model, ipAddress: ipAddress)
        
        if openDrawer {
            printer.addOpenDrawer()
        }
        
        printer.add(image: image)
        printer.addCut()
        printer.addRequestPrintSuccessCallback()
        
        return sendData(printer: printer, completion: completion)
    }

    static func openDrawerTaskForPrinterWith(ipAddress: String, model: String, completion: @escaping ReturnBlock) {
        
        let printer = Printer(model: model, ipAddress: ipAddress)
        
        printer.addOpenDrawer()
        
        /// if need print success callback , add 'addRequestPrintSuccessCallback'
        printer.addRequestPrintSuccessCallback()
        return sendData(printer: printer, completion: completion)
    }
    
    fileprivate static func sendData(printer: Printer, completion: @escaping ReturnBlock) {
        
        queue.async {
            printer.sendData(completion: completion)
        }
    }
}
