//
//  PACS.swift
//  MCPlus
//
//  Created by seo ha on 14/02/2019.
//  Copyright © 2019 KangSH. All rights reserved.
//

import Foundation


struct PACS:Codable{
    var serverUrl:String?
    var userId:String?
    var userPw:String?
    var product:String?
    var pId:String?
    var accno:String?
    var returnUrl:String?
    var type:String?
    var studyInstanceUid:String?
    
    enum CodingKeys: String, CodingKey {
        case serverUrl = "PACS_APP_SERVER_URL"
        case userId = "user_id"
        case userPw = "user_pw"
        case pId = "pid"
        case accno
        case type
        case studyInstanceUid = "study_instance_uid"
    }
    
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        serverUrl = try values.decode(String.self, forKey: .serverUrl)
        returnUrl = Constants.PACS_APP_RETURN_URL
        product = Constants.PACS_APP_PRODUCT
        userId = try values.decode(String.self, forKey: .userId)
        userPw = try values.decode(String.self, forKey: .userPw)
        pId = try values.decode(String.self, forKey: .pId)
        accno = try values.decode(String.self, forKey: .accno)
        studyInstanceUid = try values.decode(String.self, forKey: .studyInstanceUid)
        type = try values.decode(String.self, forKey: .type)

        // 파라미터를 암호화
        // AES 설명 http://olooi.tistory.com/entry/AES-%EC%95%94%ED%98%B8%ED%99%94
        // test site : http://aesencryption.net/#Java-aes-encryption-example
        self.encryption(Constants.PACS_APP_ENC_KEY)
    }
    
    mutating func encryption(_ key:String){
        self.serverUrl = serverUrl?.encryptWithKey(key: key)
        self.userId = userId?.encryptWithKey(key: key)
        self.userPw = userPw?.encryptWithKey(key: key)
        self.product = product?.encryptWithKey(key: key)
        self.pId = pId?.encryptWithKey(key: key)
        self.accno = accno?.encryptWithKey(key: key)
        self.returnUrl = returnUrl?.encryptWithKey(key: key)
        self.type = type?.encryptWithKey(key: key)
        self.studyInstanceUid = studyInstanceUid?.encryptWithKey(key: key)
    }
}

