//
//  MCareMessageRequest.swift
//  MCareFramework
//
//  Created by kangsh on 2018. 12. 04..
//  Copyright © 2018년 idatabank. All rights reserved.
//

import Foundation
import UIKit

public class MCareMessageRequest2:MCareMessageRequestProtocol {
    private var obf:Obfuscator = Obfuscator(withSalt: ["mcareplus" as AnyObject])
    
    
    enum requestConstants {
        static let str1: [UInt8] = [66, 34, 17, 2, 9, 4, 0, 0, 6, 12, 2, 13, 78, 49, 28, 9, 10, 0, 92, 4, 29, 19]
        static let str2: [UInt8] =  [66, 47, 8, 16, 23, 12, 17, 24, 93, 40, 2, 1, 8, 30, 0, 62, 22, 3, 1, 17, 31, 2, 21, 23, 74, 32, 12, 3, 27, 9, 8, 48, 20, 16, 22, 25, 17, 0, 6, 0, 67, 7, 24, 30, 12, 15]
        static let str3: [UInt8] = [66, 1, 8, 28, 74, 15, 2, 18, 26]
        static let str4: [UInt8] = [66, 22, 18, 0, 74, 30, 1, 8, 28, 74, 30, 16, 9, 22]
        static let str5: [UInt8] = [66, 6, 21, 17, 74, 12, 19, 21]
        static let str6: [UInt8] = [66, 19, 19, 27, 19, 12, 23, 4, 93, 15, 12, 10, 13, 16, 23, 8, 2, 10, 92, 17, 21, 23]
        static let str7: [UInt8] = [14, 26, 5, 27, 4, 87, 76, 78, 2, 4, 14, 8, 0, 21, 0, 66, 0, 14, 31, 75, 8, 27, 0, 31, 21, 1, 6, 79, 2, 4, 14, 8, 0, 21, 0]
    }
    
    
    public init() {
        
//        print("\n *********")
//        print(obf.bytesByObfuscatingString(string: "/Application/Cydia.app"))
//        print(obf.bytesByObfuscatingString(string: "/Library/MobileSubstrate/MobileSubstrate.dylib"))
//        print(obf.bytesByObfuscatingString(string: "/bin/bash"))
//        print(obf.bytesByObfuscatingString(string: "/usr/sbin/sshd"))
//        print(obf.bytesByObfuscatingString(string: "/etc/apt"))
//        print(obf.bytesByObfuscatingString(string: "/private/jailbreak.txt"))
//        print(obf.bytesByObfuscatingString(string: "cydia://package/com.example.package"))
//        print("\n *********")
    }
    
    public func isMsgRequest() -> Bool {
        let fileManager:FileManager = FileManager.default
        
        if fileManager.fileExists(atPath: obf.reveal(key:requestConstants.str1)) {
            return true
        }
        else if fileManager.fileExists(atPath: obf.reveal(key:requestConstants.str2)) {
            return true
        }
        else if fileManager.fileExists(atPath: obf.reveal(key:requestConstants.str3)) {
            return true
        }
        else if fileManager.fileExists(atPath: obf.reveal(key:requestConstants.str4)) {
            return true
        }
        else if fileManager.fileExists(atPath: obf.reveal(key:requestConstants.str5)) {
            return true
        }
        else if self.useSabonxd() {
            return true
        }
        //스키마 체크하는 부분이 세브란스에서 오작동하여 해당 기능은 프레임워크에서 제외함.
//        else if self.useCyschemedia() {
//            return true
//        }
        
        return false
    }
    
    private func useSabonxd() -> Bool {
        
        let beWritten:String = "This is a test."
        
        do {
            try beWritten.write(toFile: obf.reveal(key:requestConstants.str6), atomically: true, encoding: .utf8)
            return true
        } catch {
            do {
                try FileManager.default.removeItem(atPath: obf.reveal(key:requestConstants.str6))
            } catch {
                print("")
            }
        }
        
        return false
    }
    
    private func useCyschemedia() -> Bool {
        
        if UIApplication.shared.canOpenURL(URL.init(string: obf.reveal(key:requestConstants.str7))!) {
            return true
        }
        return false
    }
}
