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

import Foundation
import ResearchKit
import ResearchKitUI


class MMSEManager {
    static let shared = MMSEManager()
    
    func createMMSE() -> ORKTask {
        // Define questionnaire steps
        let mmseInfo = ORKInstructionStep(identifier: "MMSEInfo")
        mmseInfo.title = "MMSE Test"
        mmseInfo.iconImage = UIImage(systemName: "brain.head.profile")
        mmseInfo.detailText = "Mini-Mental State Examination\n\nWe will ask you some questions related to your cognition. Please answer as honestly as possible."
        
        // Section 1
        let sec1 = ORKInstructionStep(identifier: "Sec1")
        sec1.title = "Orientation in Time and Space"
        sec1.detailText = "Without checking anything on your phone or any other device, please answer the following questions."
        
        // Question 1
        let MMSE1choices = [
            ORKTextChoice(text: "Monday", value: "Monday" as NSString),
            ORKTextChoice(text: "Tuesday", value: "Tuesday" as NSString),
            ORKTextChoice(text: "Wednesday", value: "Wednesday" as NSString),
            ORKTextChoice(text: "Thursday", value: "Thursday" as NSString),
            ORKTextChoice(text: "Friday", value: "Friday" as NSString),
            ORKTextChoice(text: "Saturday", value: "Saturday" as NSString),
            ORKTextChoice(text: "Sunday", value: "Sunday" as NSString),
            ORKTextChoice(text: "I don’t know", value: "I don’t know" as NSString)
        ]
        
        let MMSE1QuestionStep = ORKQuestionStep(
            identifier: "01DayOfWeek",
            title: "Question 1",
            question: "What day of the week is today?",
            answer: ORKValuePickerAnswerFormat(textChoices: MMSE1choices)
        )
        MMSE1QuestionStep.isOptional = false
        
        // Question 2
        let MMSE2choices = (2000...2030).map { year in
            ORKTextChoice(text: "\(year)", value: "\(year)" as NSString)
        }
        
        let MMSE2QuestionStep = ORKQuestionStep(
            identifier: "02Year",
            title: "Question 2",
            question: "What year is it?",
            answer: ORKValuePickerAnswerFormat(textChoices: MMSE2choices)
        )
        MMSE2QuestionStep.isOptional = false
        
        // Question 3
        let MMSE3choices = [
            ORKTextChoice(text: "January", value: "January" as NSString),
            ORKTextChoice(text: "February", value: "February" as NSString),
            ORKTextChoice(text: "March", value: "March" as NSString),
            ORKTextChoice(text: "April", value: "April" as NSString),
            ORKTextChoice(text: "May", value: "May" as NSString),
            ORKTextChoice(text: "June", value: "June" as NSString),
            ORKTextChoice(text: "July", value: "July" as NSString),
            ORKTextChoice(text: "August", value: "August" as NSString),
            ORKTextChoice(text: "September", value: "September" as NSString),
            ORKTextChoice(text: "October", value: "October" as NSString),
            ORKTextChoice(text: "November", value: "November" as NSString),
            ORKTextChoice(text: "December", value: "December" as NSString)
        ]
        
        let MMSE3QuestionStep = ORKQuestionStep(
            identifier: "03Month",
            title: "Question 3",
            question: "What month is it?",
            answer: ORKValuePickerAnswerFormat(textChoices: MMSE3choices)
        )
        MMSE3QuestionStep.isOptional = false
        
        // Question 4
        let MMSE4choices = (1...31).map { day in
            ORKTextChoice(text: "\(day)", value: "\(day)" as NSString)
        }
        
        let MMSE4QuestionStep = ORKQuestionStep(
            identifier: "04DayOfMonth",
            title: "Question 4",
            question: "What is today’s date?",
            answer: ORKValuePickerAnswerFormat(textChoices: MMSE4choices)
        )
        MMSE4QuestionStep.isOptional = false
        
        // Question 5
        let MMSE5AnswerFormat = ORKTextAnswerFormat(maximumLength: 50)
        MMSE5AnswerFormat.multipleLines = false
        
        let MMSE5QuestionStep = ORKQuestionStep(
            identifier: "05Time",
            title: "Question 5",
            question: "What time is it approximately? (e.g., 3:00 PM, 14:00, 4:30 PM, 19:45)",
            answer: MMSE5AnswerFormat
        )
        MMSE5QuestionStep.isOptional = false
        
        // Question 6
        let MMSE6choices = [
            ORKTextChoice(text: "Spring", value: "Spring" as NSString),
            ORKTextChoice(text: "Summer", value: "Summer" as NSString),
            ORKTextChoice(text: "Autumn", value: "Autumn" as NSString),
            ORKTextChoice(text: "Winter", value: "Winter" as NSString)
        ]
        
        let MMSE6QuestionStep = ORKQuestionStep(
            identifier: "06Season",
            title: "Question 6",
            question: "What season are we in?",
            answer: ORKValuePickerAnswerFormat(textChoices: MMSE6choices)
        )
        MMSE6QuestionStep.isOptional = false
        
        // Question 7
        let MMSE7AnswerFormat = ORKTextAnswerFormat(maximumLength: 50)
        MMSE7AnswerFormat.multipleLines = true
        let MMSE7QuestionStep = ORKQuestionStep(
            identifier: "07Location",
            title: "Question 7",
            question: "Where are you right now? (e.g., Home, Work, Park, School)",
            answer: MMSE7AnswerFormat
        )
        MMSE7QuestionStep.isOptional = false
        
        // Question 8
        let MMSE8AnswerFormat = ORKTextAnswerFormat(maximumLength: 50)
        MMSE8AnswerFormat.multipleLines = true
        let MMSE8QuestionStep = ORKQuestionStep(
            identifier: "08City",
            title: "Question 8",
            question: "What city are you in?",
            answer: MMSE8AnswerFormat
        )
        MMSE8QuestionStep.isOptional = false
        
        // Question 9
        let MMSE9AnswerFormat = ORKTextAnswerFormat(maximumLength: 50)
        MMSE9AnswerFormat.multipleLines = true
        let MMSE9QuestionStep = ORKQuestionStep(
            identifier: "09State",
            title: "Question 9",
            question: "What state are you in?",
            answer: MMSE9AnswerFormat
        )
        MMSE9QuestionStep.isOptional = false
        
        
        // Question 10
        let MMSE10QuestionStepTitle = "Question 10"
        let MMSE10AnswerFormat = ORKTextAnswerFormat(maximumLength: 50)
        MMSE10AnswerFormat.multipleLines = true
        let MMSE10QuestionStep = ORKQuestionStep(identifier: "10Country", title: MMSE10QuestionStepTitle, question: "Which country are you in?", answer: MMSE10AnswerFormat)
        MMSE10QuestionStep.isOptional = false
        
        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
        
        // Section 2
        let sec2 = ORKInstructionStep(identifier: "Sec2")
        sec2.title = "Fixation"
        sec2.detailText = "Without reviewing anything on your phone or any other device, answer the following questions"
        
        // Question 11
        let words1 = ORKInstructionStep(identifier: "words1")
        words1.title = "Reading"
        words1.detailText = "Read the following 3 words and click the (Next) button \n\nHORSE\n\nWEIGHT\n\nAPPLE"
        
        // Word 1
        let MMSE11QuestionStepTitle1 = "Question 11"
        let MMSE11QuestionStepAnswer1 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE11QuestionStepAnswer1.multipleLines = false
        let MMSE11QuestionStep1 = ORKQuestionStep(identifier: "11word1", title: MMSE11QuestionStepTitle1, question: "What was the first word?", answer: MMSE11QuestionStepAnswer1)
        MMSE11QuestionStep1.isOptional = false
        
        // Word 2
        let MMSE11QuestionStepTitle2 = "Question 11"
        let MMSE11QuestionStepAnswer2 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE11QuestionStepAnswer2.multipleLines = false
        let MMSE11QuestionStep2 = ORKQuestionStep(identifier: "11word2", title: MMSE11QuestionStepTitle2, question: "What was the second word?", answer: MMSE11QuestionStepAnswer2)
        MMSE11QuestionStep2.isOptional = false
        
        // Word 3
        let MMSE11QuestionStepTitle3 = "Question 11"
        let MMSE11QuestionStepAnswer3 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE11QuestionStepAnswer3.multipleLines = false
        let MMSE11QuestionStep3 = ORKQuestionStep(identifier: "11word3", title: MMSE11QuestionStepTitle3, question: "What was the third word?", answer: MMSE11QuestionStepAnswer3)
        MMSE11QuestionStep3.isOptional = false
        
        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
        
        // Section 3
        let sec3 = ORKInstructionStep(identifier: "Sec3")
        sec3.title = "Concentration and Calculation"
        sec3.detailText = "Without using a calculator, answer the following questions"
        
        // Question 12
        let subtract1 = ORKInstructionStep(identifier: "subtract1")
        subtract1.title = "Question 12"
        subtract1.detailText = "You have 100 pesos, and you will subtract 7 repeatedly"
        
        // Subtraction 1
        let MMSE12QuestionStepTitle1 = "Question 12"
        let MMSE12QuestionStepAnswer1 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE12QuestionStepAnswer1.multipleLines = false
        let MMSE12QuestionStep1 = ORKQuestionStep(identifier: "12subtract1", title: MMSE12QuestionStepTitle1, question: "100-7?", answer: MMSE12QuestionStepAnswer1)
        MMSE12QuestionStep1.isOptional = false
        
        // Subtraction 2
        let MMSE12QuestionStepTitle2 = "Question 12"
        let MMSE12QuestionStepAnswer2 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE12QuestionStepAnswer2.multipleLines = false
        let MMSE12QuestionStep2 = ORKQuestionStep(identifier: "12subtract2", title: MMSE12QuestionStepTitle2, question: "Your previous result - 7?", answer: MMSE12QuestionStepAnswer2)
        MMSE12QuestionStep2.isOptional = false
        
        // Subtraction 3
        let MMSE12QuestionStepTitle3 = "Question 12"
        let MMSE12QuestionStepAnswer3 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE12QuestionStepAnswer3.multipleLines = false
        let MMSE12QuestionStep3 = ORKQuestionStep(identifier: "12subtract3", title: MMSE12QuestionStepTitle3, question: "Your previous result - 7?", answer: MMSE12QuestionStepAnswer3)
        MMSE12QuestionStep3.isOptional = false
        
        // Subtraction 4
        let MMSE12QuestionStepTitle4 = "Question 12"
        let MMSE12QuestionStepAnswer4 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE12QuestionStepAnswer4.multipleLines = false
        let MMSE12QuestionStep4 = ORKQuestionStep(identifier: "12subtract4", title: MMSE12QuestionStepTitle4, question: "Your previous result - 7?", answer: MMSE12QuestionStepAnswer4)
        MMSE12QuestionStep4.isOptional = false
        
        // Subtraction 5
        let MMSE12QuestionStepTitle5 = "Question 12"
        let MMSE12QuestionStepAnswer5 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE12QuestionStepAnswer5.multipleLines = false
        let MMSE12QuestionStep5 = ORKQuestionStep(identifier: "12subtract5", title: MMSE12QuestionStepTitle5, question: "Your previous result - 7?", answer: MMSE12QuestionStepAnswer5)
        MMSE12QuestionStep5.isOptional = false
        
        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
        
        // Section 4
        let sec4 = ORKInstructionStep(identifier: "Sec4")
        sec4.title = "Memory"
        sec4.detailText = "Remember the 3 words we mentioned earlier, write them below"
        
        // Question 13
        
        // Word 1
        let MMSE13QuestionStepTitle1 = "Question 13"
        let MMSE13QuestionStepAnswer1 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE13QuestionStepAnswer1.multipleLines = false
        let MMSE13QuestionStep1 = ORKQuestionStep(identifier: "13word1", title: MMSE13QuestionStepTitle1, question: "What was the first word?", answer: MMSE13QuestionStepAnswer1)
        MMSE13QuestionStep1.isOptional = false
        
        // Word 2
        let MMSE13QuestionStepTitle2 = "Question 13"
        let MMSE13QuestionStepAnswer2 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE13QuestionStepAnswer2.multipleLines = false
        let MMSE13QuestionStep2 = ORKQuestionStep(identifier: "13word2", title: MMSE13QuestionStepTitle2, question: "What was the second word?", answer: MMSE13QuestionStepAnswer2)
        MMSE13QuestionStep2.isOptional = false
        
        // Word 3
        let MMSE13QuestionStepTitle3 = "Question 13"
        let MMSE13QuestionStepAnswer3 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE13QuestionStepAnswer3.multipleLines = false
        let MMSE13QuestionStep3 = ORKQuestionStep(identifier: "13word3", title: MMSE13QuestionStepTitle3, question: "What was the third word?", answer: MMSE13QuestionStepAnswer3)
        MMSE13QuestionStep3.isOptional = false
        
        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
        
        // Section 5
        let sec5 = ORKInstructionStep(identifier: "Sec5")
        sec5.title = "Language and Construction"
        sec5.detailText = "Images will be shown, try to tell me what they are"
        
        // Question 14
        
        // Image 1
        let MMSE14QuestionStepTitle1 = "Question 14"
        
        // Text answer format
        let MMSE14QuestionStepAnswer1 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE14QuestionStepAnswer1.multipleLines = false
        
        // Create the question step with image for MMSE14
        let MMSE14QuestionStep1 = ORKQuestionStep(
            identifier: "14imageQuestion1",
            title: MMSE14QuestionStepTitle1,
            question: "What do you see in this image?",
            answer: MMSE14QuestionStepAnswer1
        )
        MMSE14QuestionStep1.isOptional = false
        
        // Add image to the step
        MMSE14QuestionStep1.image = UIImage(named: "lapiz")
        
        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
        
        // Image 2
        let MMSE14QuestionStepTitle2 = "Question 14"
        
        // Text answer format
        let MMSE14QuestionStepAnswer2 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE14QuestionStepAnswer2.multipleLines = false
        
        // Create the question step with image for MMSE14
        let MMSE14QuestionStep2 = ORKQuestionStep(
            identifier: "14imageQuestion2",
            title: MMSE14QuestionStepTitle2,
            question: "What do you see in this image?",
            answer: MMSE14QuestionStepAnswer2
        )
        MMSE14QuestionStep2.isOptional = false
        
        // Add image to the step
        MMSE14QuestionStep2.image = UIImage(named: "reloj")
        
        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
        
        // Question 15
        let MMSE15QuestionStepTitle1 = "Question 15"
        let MMSE15QuestionStepAnswer1 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE15QuestionStepAnswer1.multipleLines = false
        let MMSE15QuestionStep1 = ORKQuestionStep(identifier: "15copy", title: MMSE15QuestionStepTitle1, question: "Repeat the phrase: No ifs, ands, or buts", answer: MMSE15QuestionStepAnswer1)
        MMSE15QuestionStep1.isOptional = false
        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
        
        // Question 16
        let ind16 = ORKInstructionStep(identifier: "ind16")
        ind16.title = "Question 16"
        ind16.detailText = "I will give you some instructions. Please follow them in the order I tell you: \n\n 1. Select the number 8 \n\n 2. Select all the words that start with P \n\n 3. Write the word 'hair'"
        
        // Instruction 1
        let MMSE16QuestionStepTitle1 = "Question 16"
        let MMSE16QuestionStepAnswer1 = ORKScaleAnswerFormat(maximumValue: 12, minimumValue: 0, defaultValue: 4, step: 1)
        let MMSE16QuestionStep1 = ORKQuestionStep(identifier: "16Instruction1", title: MMSE16QuestionStepTitle1, question: " ", answer: MMSE16QuestionStepAnswer1)
        MMSE16QuestionStep1.isOptional = false
        
        // Instruction 2
        let MMSE16QuestionStepTitle2 = "Question 16"
        let MMSE16QuestionChoices2 = [
            ORKTextChoice(text: "Read", value: "Read" as NSString),
            ORKTextChoice(text: "Dad", value: "Dad" as NSString),
            ORKTextChoice(text: "Play", value: "Play" as NSString),
            ORKTextChoice(text: "Bee", value: "Bee" as NSString),
            ORKTextChoice(text: "Fight", value: "Fight" as NSString),
            ORKTextChoice(text: "Drink", value: "Drink" as NSString),
            ORKTextChoice(text: "Paper", value: "Paper" as NSString),
            ORKTextChoice(text: "Popcorn", value: "Popcorn" as NSString),
            ORKTextChoice(text: "Write", value: "Write" as NSString),
            ORKTextChoice(text: "Dance", value: "Dance" as NSString),
            ORKTextChoice(text: "Travel", value: "Travel" as NSString),
            ORKTextChoice(text: "Comb", value: "Comb" as NSString)
        ]
        
        let MMSE16QuestionStepAnswer2 = ORKTextChoiceAnswerFormat(style: .multipleChoice, textChoices: MMSE16QuestionChoices2)
        MMSE13QuestionStepAnswer2.multipleLines = false
        let MMSE16QuestionStep2 = ORKQuestionStep(identifier: "16Indicacion2", title: MMSE16QuestionStepTitle2, question: " ", answer: MMSE16QuestionStepAnswer2)
        MMSE13QuestionStep2.isOptional = false
        
        // Instruction 3
        let MMSE16QuestionStepTitle3 = "Question 16"
        let MMSE16QuestionStepAnswer3 = ORKTextAnswerFormat(maximumLength: 30)
        MMSE16QuestionStepAnswer3.multipleLines = false
        let MMSE16QuestionStep3 = ORKQuestionStep(identifier: "16Instruction3", title: MMSE16QuestionStepTitle3, question: " ", answer: MMSE16QuestionStepAnswer3)
        MMSE16QuestionStep3.isOptional = false
        
        // Question 17
        let MMSE17QuestionStepTitle = "Question 17"
        
        // Text answer format
        let MMSE17QuestionStepAnswer = ORKTextAnswerFormat(maximumLength: 30)
        MMSE17QuestionStepAnswer.multipleLines = false
        
        // Create the image question step for MMSE17
        let MMSE17QuestionStep = ORKQuestionStep(
            identifier: "17ImageConstruction",
            title: MMSE17QuestionStepTitle,
            question: "Please do what the image says",
            answer: MMSE17QuestionStepAnswer
        )
        MMSE17QuestionStep.isOptional = false
        
        // Add the image to the step
        MMSE17QuestionStep.image = UIImage(named: "name1")
        
        // Question 18
        let MMSE18QuestionStep = ORKQuestionStep(identifier: "18Sentence")
        MMSE18QuestionStep.title = "Question 18"
        MMSE18QuestionStep.question = "Write a sentence as if you were writing a letter."
        MMSE18QuestionStep.answerFormat = ORKTextAnswerFormat(maximumLength: 200)
        MMSE18QuestionStep.isOptional = false
        
        // Question 19
        let MMSE19QuestionStepTitle1 = "Question 19"
        
        // Create the image-only question step
        let MMSE19QuestionStep1 = ORKInstructionStep(identifier: "19Drawing1")
        MMSE19QuestionStep1.title = MMSE19QuestionStepTitle1
        MMSE19QuestionStep1.detailText = "Memorize the image below and click (Next)"
        
        // Add the image to the step
        MMSE19QuestionStep1.image = UIImage(named: "dibujo")
        
        let MMSE19QuestionStep2 = ORKSignatureStep(identifier: "19Drawing")
        MMSE19QuestionStep2.title = "Draw"
        MMSE19QuestionStep2.text = "Draw the image above"
        
        // END
        let completionStep = ORKCompletionStep(identifier: "MMSECompletion")
        completionStep.title = "Thank you!"
        completionStep.iconImage = UIImage(systemName: "checkmark.seal")
        completionStep.text = "You have completed the MMSE test. Thank you for your participation!"
        
        // Retorna la tarea combinando los pasos
        return ORKOrderedTask(identifier: "MMSESurveyTask", steps: [mmseInfo, sec1, MMSE1QuestionStep, MMSE2QuestionStep, MMSE3QuestionStep, MMSE4QuestionStep, MMSE5QuestionStep, MMSE6QuestionStep, MMSE7QuestionStep, MMSE8QuestionStep, MMSE9QuestionStep, MMSE10QuestionStep, sec2, words1, MMSE11QuestionStep1, MMSE11QuestionStep2, MMSE11QuestionStep3, sec3, subtract1, MMSE12QuestionStep1, MMSE12QuestionStep2, MMSE12QuestionStep3, MMSE12QuestionStep4, MMSE12QuestionStep5, sec4, MMSE13QuestionStep1, MMSE13QuestionStep2, MMSE13QuestionStep3, sec5, MMSE14QuestionStep1, MMSE14QuestionStep2, MMSE15QuestionStep1, ind16, MMSE16QuestionStep1, MMSE16QuestionStep2, MMSE16QuestionStep3, MMSE17QuestionStep, MMSE18QuestionStep,MMSE19QuestionStep1, MMSE19QuestionStep2, completionStep])
    }
}
