//
//  FichaManager.swift
//  RK-Journals
//
//  Created by Juan David López Regalado on 20/11/24.
//

import Foundation
import ResearchKit

class FichaManager {
    static let shared = FichaManager()
    
    func createFichaTask() -> ORKTask {
        
        let bienvenida = ORKInstructionStep(identifier: "bienvenida")
        bienvenida.title = "Ficha Identificación"
        bienvenida.iconImage = UIImage(systemName: "person.text.rectangle")
        bienvenida.detailText = "Por favor, llena la siguiente ficha de identificación con tus datos"
        
        // Nombre
        let textQuestionStepTitle = "Nombre"
        let textAnswerFormat = ORKTextAnswerFormat(maximumLength: 30)
        textAnswerFormat.multipleLines = false
        let textQuestionStep = ORKQuestionStep(identifier: "Nombre", title: textQuestionStepTitle, question: "¿Cuál es tu nombre?", answer: textAnswerFormat)
        textQuestionStep.isOptional = false
        
        // Sexo
        let multiGenderChoice = "Sexo"
        let gendChoices = [
            ORKTextChoice(text: "Hombre", value: "hombre" as NSString),
            ORKTextChoice(text: "Mujer", value: "mujer" as NSString),
            ORKTextChoice(text: "Prefiero no decirlo", value: "Inf" as NSString)]
        
        let genderChoiceFormat = ORKTextChoiceAnswerFormat(style: .singleChoice, textChoices: gendChoices)
        let multiGenderQuestionStep = ORKQuestionStep(identifier: "Sexo", title: multiGenderChoice,question: "¿Cuál es tu sexo?", answer: genderChoiceFormat)
        multiGenderQuestionStep.isOptional = false
        
        // Edad
        var ageChoices: [ORKTextChoice] = []
        for age in 16...50 {
            let ageChoice = ORKTextChoice(text: "\(age) años", value: "\(age)" as NSString)
            ageChoices.append(ageChoice)
        }
        
        let ageQuestionStep = ORKQuestionStep(
            identifier: "ageQuestionStep",
            title: "Pregunta sobre Edad",
            question: "¿Qué edad tienes?",
            answer: ORKValuePickerAnswerFormat(textChoices: ageChoices)
        )
        ageQuestionStep.isOptional = false // Hacemos que esta pregunta sea obligatoria
        
        // Peso
        let pesoQuestionStep = ORKQuestionStep(identifier: "peso", title: "Peso", question: "¿Cuál es tu peso en kilogramos?", answer: ORKNumericAnswerFormat(style: .integer, unit: "kg"))
        pesoQuestionStep.isOptional = false

        // Altura
        let alturaQuestionStep = ORKQuestionStep(identifier: "altura", title: "Altura", question: "¿Cuál es tu altura en metros?", answer: ORKNumericAnswerFormat(style: .decimal, unit: "m"))
        alturaQuestionStep.isOptional = false
        
        
        // Sintomas
        let symptomsChoices = [
            ORKTextChoice(text: "Dolor de cabeza", value: "dolor_de_cabeza" as NSString),
            ORKTextChoice(text: "Fiebre", value: "fiebre" as NSString),
            ORKTextChoice(text: "Tos", value: "tos" as NSString),
            ORKTextChoice(text: "Dificultad para respirar", value: "dificultad_respirar" as NSString),
            ORKTextChoice(text: "Dolor en el pecho", value: "dolor_pecho" as NSString),
            ORKTextChoice(text: "Fatiga", value: "fatiga" as NSString),
            ORKTextChoice(text: "Náuseas", value: "nauseas" as NSString),
            ORKTextChoice(text: "Otros", value: "otros" as NSString)
        ]
        
        // Crear la pregunta de síntomas
        let symptomsStep = ORKQuestionStep(
            identifier: "sintomas",
            title: "¿Qué síntomas estás presentando?",
            answer: ORKAnswerFormat.choiceAnswerFormat(with: .multipleChoice, textChoices: symptomsChoices)
        )
        symptomsStep.isOptional = false
        
        let termino = ORKInstructionStep(identifier: "termino")
        termino.title = "Gracias"
        termino.iconImage = UIImage(systemName: "checkmark.circle.fill")
        termino.detailText = "Gracias por llenar la ficha de identificación"
        
        return ORKOrderedTask(identifier: "FichaIdentificacion", steps: [bienvenida, textQuestionStep, multiGenderQuestionStep, ageQuestionStep, pesoQuestionStep, alturaQuestionStep, symptomsStep, termino])
    }
}
