//
//  UIUtils.swift
//  TrustBuilderMfaSampleApp
//
//  Created by Jimmy Visse on 04/05/2026.
//

import Foundation
import SwiftUI
import TrustBuilderMfaLibSwift

class UIUtils {
    /**
     Format Object Response into String
     - Parameter response: Failable<A, E>
     - Returns: String
     */
    public static func formatResponse<T: Encodable>(response: T) -> String {
        let jsonEncoder = JSONEncoder()
        let jsonData = try! jsonEncoder.encode(response)
        return String(data: jsonData, encoding: String.Encoding.utf8)!
    }
    
    public struct ExecuteUseCaseView: View {
        @Binding var displaySecretCode:Bool
        @Binding var SecretCode:String
        var newSecretCode:Bool
        let setup : () -> Void
        let execute : () -> Void
        var onCancel: (() -> Void)? = nil
        
        var body: some View {
            Button("Execute", action: setup).alert("Secret Code", isPresented: $displaySecretCode, actions: {
                SecureField("Secret Code", text: $SecretCode )
                Button("Validate", action: execute)
                Button("Cancel", role: .cancel, action: {
                    SecretCode = ""
                    onCancel?()
                })
            }, message: {
                Text(newSecretCode ? "Enter a new secret code"  : "Please enter your secret code .")
            })
        }
    }

    public struct ExecuteSealUseCaseView: View {
        @Binding var displayBio:Bool
        @Binding var displaySecretCode:Bool
        @Binding var SecretCode:String
        @Binding var SealData:String
        let setup : () -> Void
        let execute : () -> Void
        var onCancel: (() -> Void)? = nil
        
        var body: some View {
            Button("Execute", action: setup).alert("Please enter your secret code after read sealed data.", isPresented: $displaySecretCode, actions: {
                SecureField("Secret Code", text: $SecretCode )
                Button("Validate", action: execute)
                Button("Cancel", role: .cancel, action: {
                    SecretCode = ""
                    onCancel?()
                })
            }, message: {
                Text(SealData)
            }).alert("Bio authentication", isPresented: $displayBio, actions: {
                Button("Validate", action: {
                    execute()
                })
            }, message: {
                Text(SealData)
            })
        }
    }
}
