//
//  JackCardApi.swift
//  Emoney Info
//
//  Created by Wira Irawan on 27/07/24.
//

import Foundation

public class JackCardApi : UnifiedNfcApi {
    var emoney : Emoney = Emoney()
    var riwayatList: [RiwayatCard] = []
    
    public override init() {}
    
    public func getBalance(resp : Data){
        self.emoney.setCardLabel("Jackcard")
        self.emoney.setCardNumber(resp.hexEncodedString().subString(from: 16, to: 32))
        apduRunner.exchangeApdu(apduCommand: EmoneyApduCommands.DKI_APDU01, completionHandler: {response in
            if (response.sw1 == 0x90 && response.sw2 == 0x00){
                self.emoney.setBalance(response.getData().hexEncodedString().hex2decimal())
                self.emoney.setTampilRiwayat(false)
                self.updateScreen()
                self.apduRunner.sessionEx?.alertMessage = "readFinish".localizeString(string: self.langCode!)
                self.apduRunner.invalidateSession()
            } else {
                self.apduRunner.invalidateSession(msg: "readFailed".localizeString(string: self.langCode!))
            }
        })
    }
    
    private func updateScreen(){
        if (self.apduRunner.callback != nil){
            self.apduRunner.callback?.complete(emoney: self.emoney)
        }
    }
}
