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

import Foundation

class LockScreenDAO: CustomStringConvertible {
    var lockScreenUseYN:Bool?
    var touchIDUseYN:Bool?
    var arrPassword:[Int]?
    var pwInputType:PASSWORD_INPUT_TYPE? = .FIRST_INPUT
    var userDefaults = UserDefaults.standard
    var requiredYN:Bool?
    
    let LOCK_SCREEN_USE = "lock_screen_use"
    let TOUCH_ID_USE = "touch_id_use"
    let LOCK_SCREEN_PW = "lock_screen_pw"
    let PASSWORD_INPUT_TYPE = "password_input_type"
    
    enum PASSWORD_INPUT_TYPE:Int{
        case FIRST_INPUT
        case CONFIRM_INPUT
        case UNLOCK_INPUT
        case UNLOCK_FAIL_RE_INPUT
        case CHANGE_FIRST_INPUT
        case CHANGE_CONFIRM_INPUT
    }
    
    static let sharedInstance = LockScreenDAO()
    
    private init(){
        let _ = initPrivate()
    }
}

extension LockScreenDAO{
    
    func initPrivate() -> LockScreenDAO{
        requiredYN = true
        lockScreenUseYN = true
        pwInputType = LockScreenDAO.PASSWORD_INPUT_TYPE(rawValue: userDefaults.integer(forKey: self.PASSWORD_INPUT_TYPE))
        
        return self
    }
    
    func saveOptionValues(arrPassword:[Int]){
        if self.requiredYN == false{
            let lockScreenUseYN = self.lockScreenUseYN ?? false
            let touchIDUseYN = self.touchIDUseYN ?? false
            self.userDefaults.set(lockScreenUseYN, forKey: self.LOCK_SCREEN_USE)
            self.userDefaults.set(touchIDUseYN, forKey: self.TOUCH_ID_USE)
        }
        
        if let pwInputType = self.pwInputType{
            self.userDefaults.set(pwInputType.rawValue, forKey: self.PASSWORD_INPUT_TYPE)
        }
        
        // 비밀번호 암호화 저장
        self.arrPassword = arrPassword
        self.encryptPassword(key: Constants.PACS_APP_ENC_KEY)
        
    }
    
    func encryptPassword(key:String){
        guard let arrPassword = self.arrPassword else {
            return
        }
        for i in 0..<arrPassword.count{
            if let encPass = String(arrPassword[i]).encryptWithKey(key: key){
                self.userDefaults.set(encPass, forKey: "\(i)")
            }
        }
    }
    
    func encryptedPasswordWithKey(key:String) -> String? {
        return self.userDefaults.object(forKey: key) as? String
    }
    
    func saveOptionValuesWithoutPassword(){
        if self.requiredYN == false{
            let lockScreenUseYN = self.lockScreenUseYN ?? false
            let touchIDUseYN = self.touchIDUseYN ?? false
            self.userDefaults.set(lockScreenUseYN, forKey: self.LOCK_SCREEN_USE)
            self.userDefaults.set(touchIDUseYN, forKey: self.TOUCH_ID_USE)
        }
        
        if let pwInputType = self.pwInputType{
            self.userDefaults.set(pwInputType.rawValue, forKey: self.PASSWORD_INPUT_TYPE)
        }
    }
    
    func loadOptionValues(){
        let representation = self.userDefaults.dictionaryRepresentation()
        let count = representation.filter({$0.key == LOCK_SCREEN_USE}).count
        if count > 0, requiredYN == false{
            self.lockScreenUseYN = self.userDefaults.bool(forKey: LOCK_SCREEN_USE)
        }
        
        if let pwInputType = self.pwInputType{
            self.userDefaults.set(pwInputType.rawValue, forKey: self.PASSWORD_INPUT_TYPE)
        }
    }
}
