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

import Foundation
import ResearchKit

class IPAQManager {
    static let shared = IPAQManager()
    
    func createIPAQ() -> ORKTask {
        // Define the introduction step
        let ipaqInfo = ORKInstructionStep(identifier: "IPAQInfo")
        ipaqInfo.title = "IPAQ Test"
        ipaqInfo.iconImage = UIImage(systemName: "figure.run")
        ipaqInfo.detailText = "International Physical Activity Questionnaire\n\nWe will ask you 7 questions related to your physical activity. Please answer as honestly as possible."
        
        // Section 1: Vigorous Activities
        let sec1 = ORKInstructionStep(identifier: "Sec1")
        sec1.title = "Section 1"
        sec1.detailText = "Think about all the VIGOROUS activities you did in the last 7 days. Vigorous physical activities are those that require intense physical effort and make you breathe much harder than usual. Consider only those activities that lasted at least 10 consecutive minutes."
        
        let vigorousDaysChoices = [
            ORKTextChoice(text: "None", value: "None" as NSString),
            ORKTextChoice(text: "1 day", value: "1 day" as NSString),
            ORKTextChoice(text: "2 days", value: "2 days" as NSString),
            ORKTextChoice(text: "3 days", value: "3 days" as NSString),
            ORKTextChoice(text: "4 days", value: "4 days" as NSString),
            ORKTextChoice(text: "5 days", value: "5 days" as NSString),
            ORKTextChoice(text: "6 days", value: "6 days" as NSString),
            ORKTextChoice(text: "7 days", value: "7 days" as NSString)
        ]
        
        let vigorousDaysStep = ORKQuestionStep(
            identifier: "VigorousDays",
            title: "Question 1",
            question: "In the last 7 days, how many days did you perform vigorous physical activities such as heavy lifting, digging, aerobics, or fast cycling?",
            answer: ORKValuePickerAnswerFormat(textChoices: vigorousDaysChoices)
        )
        vigorousDaysStep.isOptional = false
        
        let vigorousMinutesStep = ORKQuestionStep(
            identifier: "VigorousMinutes",
            title: "Question 2",
            question: "On those days, how much time (in minutes) did you usually spend performing vigorous physical activities?",
            answer: ORKNumericAnswerFormat(style: .integer, unit: "minutes", minimum: 0, maximum: 1440)
        )
        vigorousMinutesStep.isOptional = false
        
        // Section 2: Moderate Activities
        let sec2 = ORKInstructionStep(identifier: "Sec2")
        sec2.title = "Section 2"
        sec2.detailText = "Think about all the MODERATE activities you did in the last 7 days. Moderate activities require moderate physical effort and make you breathe somewhat harder than usual. Consider only those activities that lasted at least 10 consecutive minutes."
        
        let moderateDaysChoices = [
            ORKTextChoice(text: "None", value: "None" as NSString),
            ORKTextChoice(text: "1 day", value: "1 day" as NSString),
            ORKTextChoice(text: "2 days", value: "2 days" as NSString),
            ORKTextChoice(text: "3 days", value: "3 days" as NSString),
            ORKTextChoice(text: "4 days", value: "4 days" as NSString),
            ORKTextChoice(text: "5 days", value: "5 days" as NSString),
            ORKTextChoice(text: "6 days", value: "6 days" as NSString),
            ORKTextChoice(text: "7 days", value: "7 days" as NSString)
        ]
        
        let moderateDaysStep = ORKQuestionStep(
            identifier: "ModerateDays",
            title: "Question 3",
            question: "In the last 7 days, how many days did you perform moderate physical activities such as carrying light loads, cycling at a regular pace, or playing doubles tennis? Do not include walking.",
            answer: ORKValuePickerAnswerFormat(textChoices: moderateDaysChoices)
        )
        moderateDaysStep.isOptional = false
        
        let moderateMinutesStep = ORKQuestionStep(
            identifier: "ModerateMinutes",
            title: "Question 4",
            question: "On those days, how much time (in minutes) did you usually spend performing moderate physical activities?",
            answer: ORKNumericAnswerFormat(style: .integer, unit: "minutes", minimum: 0, maximum: 1440)
        )
        moderateMinutesStep.isOptional = false
        
        // Section 3: Walking
        let sec3 = ORKInstructionStep(identifier: "Sec3")
        sec3.title = "Section 3"
        sec3.detailText = "Think about the time you spent WALKING in the last 7 days. This includes walking at work or home, walking to travel from place to place, or any other walking you might do solely for recreation, sport, exercise, or leisure."
        
        let walkingDaysChoices = [
            ORKTextChoice(text: "None", value: "None" as NSString),
            ORKTextChoice(text: "1 day", value: "1 day" as NSString),
            ORKTextChoice(text: "2 days", value: "2 days" as NSString),
            ORKTextChoice(text: "3 days", value: "3 days" as NSString),
            ORKTextChoice(text: "4 days", value: "4 days" as NSString),
            ORKTextChoice(text: "5 days", value: "5 days" as NSString),
            ORKTextChoice(text: "6 days", value: "6 days" as NSString),
            ORKTextChoice(text: "7 days", value: "7 days" as NSString)
        ]
        
        let walkingDaysStep = ORKQuestionStep(
            identifier: "WalkingDays",
            title: "Question 5",
            question: "In the last 7 days, how many days did you walk for at least 10 consecutive minutes?",
            answer: ORKValuePickerAnswerFormat(textChoices: walkingDaysChoices)
        )
        walkingDaysStep.isOptional = false
        
        let walkingMinutesStep = ORKQuestionStep(
            identifier: "WalkingMinutes",
            title: "Question 6",
            question: "On those days, how much time (in minutes) did you usually spend walking?",
            answer: ORKNumericAnswerFormat(style: .integer, unit: "minutes", minimum: 0, maximum: 1440)
        )
        walkingMinutesStep.isOptional = false
        
        // Section 4: Sitting
        let sec4 = ORKInstructionStep(identifier: "Sec4")
        sec4.title = "Section 4"
        sec4.detailText = "The last question is about the time you spent SITTING during the weekdays of the last 7 days. This includes time spent at work, at home, while studying, and during leisure time. It includes time spent sitting at a desk, visiting friends, reading, traveling by car, bus, train, or watching television."
        
        let sittingStep = ORKQuestionStep(
            identifier: "SittingTime",
            title: "Question 7",
            question: "On a typical weekday, how much time (in minutes) did you spend sitting?",
            answer: ORKNumericAnswerFormat(style: .integer, unit: "minutes", minimum: 0, maximum: 1440)
        )
        sittingStep.isOptional = false
        
        // Completion Step
        let completionStep = ORKCompletionStep(identifier: "IPAQCompletion")
        completionStep.title = "Thank you!"
        completionStep.iconImage = UIImage(systemName: "checkmark.seal")
        completionStep.text = "You have completed the IPAQ Test. Thank you for your participation!"
        
        // Retorna la tarea combinando los pasos
        return ORKOrderedTask(identifier: "SurveyTask", steps: [ipaqInfo,
                                                                sec1, vigorousDaysStep, vigorousMinutesStep,
                                                                sec2, moderateDaysStep, moderateMinutesStep,
                                                                sec3, walkingDaysStep, walkingMinutesStep,
                                                                sec4, sittingStep,
                                                                completionStep])
    }
}
