//
//  GetSecureTokensView.swift
//  tb-mfa-lib-tests
//
//  Created by inwebo imac on 02/11/2021.
//

import SwiftUI
import TrustBuilderMfaLibSwift

struct GetSecureTokensView: View {
    @EnvironmentObject var buttonBlock: ButtonBlock
    @State private var code: String = ""
    @State private var result: String = ""

    init() { }

    var body: some View {
        VStack {
            Text("Get secure tokens")
            Button("Execute", action: execute).disabled(buttonBlock.buttonIsBlocked)
            Text(result)
                    .font(.footnote)
                    .foregroundColor(.red)
                    .textSelection(.enabled)
        }
                .buttonStyle(.bordered)
    }

    func execute() {
        Task.init {
            buttonBlock.buttonIsBlocked = true
            defer { buttonBlock.buttonIsBlocked = false }
            do {
                let useCase = TrustBuilderMfaLibHolder.getLibInstance().startSecureTokensListing()
                let response:SecureTokensListingResult = try await useCase.execute()
                if (response.response?.count == 0) {
                    result = "There is no secureTokens in memory"
                } else {
                    result = UIUtils.formatResponse(response: response.response!)
                    print(result)
                }
            } catch {
                result = "Not Found"
            }
        }
        
    }

}
