//
//  Encuesta_MARLE.swift
//  Created by Tatiana Castro on 1/18/19.
//  Copyright © 2019 Tatiana Castro. All rights reserved.
//

import UIKit
import ResearchKit
import Foundation

public var EncuestaMarle: ORKOrderedTask {
    var steps = [ORKStep]()
    
    let instruccion = ORKInstructionStep(identifier: "Instrucción")
    instruccion.title = "Proyecto MARLE"
    instruccion.text = descripcion
    steps += [instruccion]
        
    for i in 0..<cantidad_preguntas{
        switch tipo_preg[i] {
        case "3":
            let escala1Formato = ORKAnswerFormat.scale(withMaximumValue: Int(max_values[i]) ?? 0, minimumValue: 1, defaultValue: NSIntegerMax, step: 1, vertical: false, maximumValueDescription: "Máximo", minimumValueDescription: "Mínimo")
            let escala1Pregunta = ORKQuestionStep(identifier: id_preg[i] , title: "Pregunta \(i+1)", question: preguntas[i], answer: escala1Formato)
            steps += [escala1Pregunta]
            
        case "2":
            let respuestaFormato = ORKAnswerFormat.textAnswerFormat()
            let respuestaStep = ORKQuestionStep(identifier: id_preg[i], title: NSLocalizedString("Pregunta \(i+1)", comment: ""), question: preguntas[i], answer: respuestaFormato);
            steps += [respuestaStep]
            
        case "1":
            var textChoices = [ORKTextChoice]()
            for j in 1...(Int(max_values[i]) ?? 0){
                if j==1{
                    textChoices.append(ORKTextChoice(text: String(j)+"    "+min_text[i], value: String(j) as NSCoding & NSCopying & NSObjectProtocol))
                }
                else if j==(Int(max_values[i])){
                    textChoices.append(ORKTextChoice(text: String(j)+"    "+max_text[i], value: String(j) as NSCoding & NSCopying & NSObjectProtocol))
                }
                else{
                textChoices.append(ORKTextChoice(text: String(j), value: String(j) as NSCoding & NSCopying & NSObjectProtocol))
                }
            }
            let answerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: textChoices)
            let questionStep = ORKQuestionStep(identifier: id_preg[i], title: NSLocalizedString("Pregunta \(i+1)", comment: ""), question: preguntas[i], answer: answerFormat)
            steps += [questionStep]
            
        default:
            let respuestaFormato = ORKAnswerFormat.textAnswerFormat()
            let respuestaStep = ORKQuestionStep(identifier: id_preg[i], title: NSLocalizedString("Pregunta \(i+1)", comment: ""), question: preguntas[i], answer: respuestaFormato);
            steps += [respuestaStep]
        }
    }
    let completionStep = ORKCompletionStep(identifier: "CompletionStep")
    completionStep.title = "Thank you for answering."
    completionStep.text = "Your answers have been recorded."
    steps += [completionStep]
    getJsonFromUrl()
    return ORKOrderedTask(identifier: id_questionario, steps: steps)
}
