//
//  ConsentTask.swift
//  SAGE_RK_ML
//
//  Created by Juan David Lopez Regalado on 31/01/25.
//

import UIKit
import ResearchKit

public var ConsentTask: ORKOrderedTask {

    var steps = [ORKStep]()

    // Visualization of sections
    let consentDocument = ConsentDocument
    
    // Section 1: Welcome
    let section1 = ORKInstructionStep(identifier: "consentSection1InstructionStep")
    section1.title = "Welcome"
    section1.iconImage = UIImage(systemName: "hand.wave")
    section1.detailText = "Section 1: Welcome. This study is about..."
    steps += [section1]
    
    // Section 2: Data Collection
    let section2 = ORKInstructionStep(identifier: "consentSection2InstructionStep")
    section2.title = "Data Collection"
    section2.iconImage = UIImage(systemName: "doc.text")
    section2.detailText = "Section 2: Data Collection. This study will collect data..."
    steps += [section2]

    // Section 3: Privacy
    let section3 = ORKInstructionStep(identifier: "consentSection3InstructionStep")
    section3.title = "Privacy"
    section3.iconImage = UIImage(systemName: "lock.shield")
    section3.detailText = "Section 3: Privacy. We value your privacy..."
    steps += [section3]

    // Section 4: Data Use
    let section4 = ORKInstructionStep(identifier: "consentSection4InstructionStep")
    section4.title = "Data Use"
    section4.iconImage = UIImage(systemName: "chart.bar")
    section4.detailText = "Section 4: Data Use. The collected data will be used for..."
    steps += [section4]

    // Section 5: Time Commitment
    let section5 = ORKInstructionStep(identifier: "consentSection5InstructionStep")
    section5.title = "Time Commitment"
    section5.iconImage = UIImage(systemName: "clock")
    section5.detailText = "Section 5: Time Commitment. This study will take approximately..."
    steps += [section5]

    // Section 6: Study Survey
    let section6 = ORKInstructionStep(identifier: "consentSection6InstructionStep")
    section6.title = "Study Survey"
    section6.iconImage = UIImage(systemName: "list.bullet.rectangle")
    section6.detailText = "Section 6: Study Survey. For this study, you will need to complete a survey..."
    steps += [section6]

    // Section 7: Study Tasks
    let section7 = ORKInstructionStep(identifier: "consentSection7InstructionStep")
    section7.title = "Study Tasks"
    section7.iconImage = UIImage(systemName: "pencil.and.outline")
    section7.detailText = "Section 7: Study Tasks. You will be required to perform these tasks..."
    steps += [section7]

    // Section 8: Withdrawal
    let section8 = ORKInstructionStep(identifier: "consentSection8InstructionStep")
    section8.title = "Withdrawal"
    section8.iconImage = UIImage(systemName: "arrow.backward.circle")
    section8.detailText = "Section 8: Withdrawal. To withdraw from the study..."
    steps += [section8]
    
    // Review and Sign
    let signature = consentDocument.signatures!.first!
    let reviewConsentStep = ORKConsentReviewStep(identifier: "ConsentReviewStep", signature: signature, in: consentDocument)
    reviewConsentStep.title = "Review"
    reviewConsentStep.text = "Review the consent form."
    reviewConsentStep.reasonForConsent = "Consent to join the study"
    steps += [reviewConsentStep]
    
    // Passcode/TouchID Protection
    let passcodeStep = ORKPasscodeStep(identifier: "Passcode")
    passcodeStep.iconImage = UIImage(systemName: "lock.circle")
    passcodeStep.text = "You will now create a passcode to identify yourself in the app and protect the entered information."
    steps += [passcodeStep]
    
    // Completion
    let completionStep = ORKCompletionStep(identifier: "CompletionStep")
    completionStep.iconImage = UIImage(systemName: "checkmark.seal")
    completionStep.title = "Welcome aboard"
    completionStep.text = "Thank you for joining this study."
    steps += [completionStep]

    return ORKOrderedTask(identifier: "ConsentTask", steps: steps)
}
