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

import SwiftUI
import TrustBuilderMfaLibSwift
import os.log

struct ResetSecretCodeView: View {
    @EnvironmentObject var buttonBlock: ButtonBlock
    @State private var tknAlias: String = ""
    @State private var code: String = ""
    @State private var result: String = ""
    @State private var displaySecretCode = false
    @State private var secretCode : String = ""

    private var useCaseHandler : TrustBuilderOperationHandler<SecretCodeReset, CredentialProvider>
    init() {
        self.useCaseHandler = TrustBuilderOperationHandler<SecretCodeReset, CredentialProvider>()
    }

    var body: some View {
        VStack {
            Text("Reset secret code")
            TextField("tknAlias", text: $tknAlias).autocapitalization(.none)
            TextField("code", text: $code).autocapitalization(.none)
            UIUtils.ExecuteUseCaseView(displaySecretCode:$displaySecretCode, SecretCode:$secretCode, newSecretCode : true, setup: setup, execute: execute).disabled(buttonBlock.buttonIsBlocked)
            Text(result)
                    .font(.footnote)
                    .foregroundColor(.red)
        }
                .buttonStyle(.bordered)
                .textFieldStyle(RoundedBorderTextFieldStyle())
    }
    
    func setup() {
        Task.init {
            do{
                let secretCodeResetParams: SecretCodeResetParams = SecretCodeResetParams(
                    tknAlias: tknAlias,
                    code: code
                )
                let secretCodeReset = try TrustBuilderMfaLibHolder.getLibInstance().startSecretCodeReset(param: secretCodeResetParams)
                useCaseHandler.loadUseCase(useCase: secretCodeReset)
                let setup = await secretCodeReset.setup();
                if (setup.isError == true) {
                    result = UIUtils.formatResponse(response: setup)
                }
                else {
                    displaySecretCode = true
                    useCaseHandler.setSetupResult(setupResult: setup.response!)
                }
            }catch{
                os_log("Task failed: %@", log: .default, type: .error, String(describing: error))
                result = String(describing: error)
            }
        }
    }

    func execute(){
        Task.init {
            buttonBlock.buttonIsBlocked = true
            defer { buttonBlock.buttonIsBlocked = false }
            defer { secretCode = "" }
            let credProvider = useCaseHandler.getSetupResult()
            if(credProvider.credType.rawValue == CredType.SECRET_CODE.rawValue)
            {
                var bytes = Array(secretCode.utf8)
                defer { for i in 0..<bytes.count { bytes[i] = 0 } }
                let credValue: CredValue = CredValue(credType: .SECRET_CODE, value: bytes)
                let secretCodeReset = useCaseHandler.getUseCase()
                let execute = await secretCodeReset.execute(input: credValue)
                result = UIUtils.formatResponse(response: execute)
            }  else{
                os_log("Credential type not allowed", log: .default, type: .error)
            }
        }
    }
}
