//
//  TapCashData.swift
//  Emoney Info
//
//  Created by Wira Irawan on 26/07/24.
//

import Foundation

class TapCashData {
    var purseData: [UInt8]?
    var purseStatus: [UInt8]?
    var purseBalance: [UInt8]?
    var CAN: [UInt8]?
    var CSN: [UInt8]?
    var purseExpiry: [UInt8]?
    var lastCredirTRP: [UInt8]?
    var lastCreditHeader: [UInt8]?
    var lastTxnTRP: [UInt8]?
    var lastTxnRecord: [UInt8]?
    var BDC: [UInt8]?
    var keySet: [UInt8]?
    var maxCardBalance: [UInt8]?
    var eData: [UInt8]?
    var crcb: [UInt8]?
    var lastTransactionSignCert: [UInt8]?
    var lastCounterData: [UInt8]?
    var version: [UInt8]?
    var lastDebitOption: [UInt8]?
    var totalRecords: [UInt8]?

    func getPurseData() -> [UInt8]? {
        return purseData
    }

    func setPurseData(_ purseData: [UInt8]) {
        self.purseData = purseData
        unpackPurseData(data: purseData)
    }

    func getPurseStatus() -> [UInt8]? {
        return purseStatus
    }

    func setPurseStatus(_ purseStatus: [UInt8]) {
        self.purseStatus = purseStatus
    }

    func getPurseBalance() -> [UInt8]? {
        return purseBalance
    }

    func setPurseBalance(_ purseBalance: [UInt8]) {
        self.purseBalance = purseBalance
    }

    func getCAN() -> [UInt8]? {
        return CAN
    }

    func setCAN(_ CAN: [UInt8]) {
        self.CAN = CAN
    }

    func getCSN() -> [UInt8]? {
        return CSN
    }

    func setCSN(_ CSN: [UInt8]) {
        self.CSN = CSN
    }

    func getPurseExpiry() -> [UInt8]? {
        return purseExpiry
    }

    func setPurseExpiry(_ purseExpiry: [UInt8]) {
        self.purseExpiry = purseExpiry
    }

    func getLastCredirTRP() -> [UInt8]? {
        return lastCredirTRP
    }

    func setLastCredirTRP(_ lastCredirTRP: [UInt8]) {
        self.lastCredirTRP = lastCredirTRP
    }

    func getLastCreditHeader() -> [UInt8]? {
        return lastCreditHeader
    }

    func setLastCreditHeader(_ lastCreditHeader: [UInt8]) {
        self.lastCreditHeader = lastCreditHeader
    }

    func getLastTxnTRP() -> [UInt8]? {
        return lastTxnTRP
    }

    func setLastTxnTRP(_ lastTxnTRP: [UInt8]) {
        self.lastTxnTRP = lastTxnTRP
    }

    func getLastTxnRecord() -> [UInt8]? {
        return lastTxnRecord
    }

    func setLastTxnRecord(_ lastTxnRecord: [UInt8]) {
        self.lastTxnRecord = lastTxnRecord
    }

    func getBDC() -> [UInt8]? {
        return BDC
    }

    func setBDC(_ BDC: [UInt8]) {
        self.BDC = BDC
    }

    func getKeySet() -> [UInt8]? {
        return keySet
    }

    func setKeySet(_ keySet: [UInt8]) {
        self.keySet = keySet
    }

    func getMaxCardBalance() -> [UInt8]? {
        return maxCardBalance
    }

    func setMaxCardBalance(_ maxCardBalance: [UInt8]) {
        self.maxCardBalance = maxCardBalance
    }

    func geteData() -> [UInt8]? {
        return eData
    }

    func seteData(_ eData: [UInt8]) {
        self.eData = eData
    }

    func getCrcb() -> [UInt8]? {
        return crcb
    }

    func setCrcb(_ crcb: [UInt8]) {
        self.crcb = crcb
    }
    
    func getTotalRecords() -> [UInt8]? {
        return totalRecords
    }
    
    private func unpackPurseData(data: [UInt8]) {
        guard data.count >= 95 else {
            debugLog("TapCash purse data too short: \(data.count)")
            return
        }
        version = Array(data[0..<1])
        purseStatus = Array(data[1..<2])
        purseBalance = Array(data[2..<5])
        CAN = Array(data[8..<16])
        CSN = Array(data[16..<24])
        purseExpiry = Array(data[24..<26])
        lastCredirTRP = Array(data[28..<32])
        lastCreditHeader = Array(data[32..<40])
        totalRecords = Array(data[40..<41])
        lastTxnTRP = Array(data[42..<46])
        lastTxnRecord = Array(data[46..<62])
        BDC = Array(data[63..<64])
        keySet = Array(data[71..<72])
        maxCardBalance = Array(data[78..<81])
        lastDebitOption = Array(data[94..<95])
    }
}
