class Question{
  constructor(){
    this.question = []
  }
  setQuestion(obj){
    this.question = obj
  }
  setQuestionByIndex(obj, index){
    this.question[index] = obj
  }
  setType(obj){
    if (obj.type === 1){
      obj.ansgroup = {
        value: '',
        answer: '',
        status: '', //it's wrong by Maiwan -> must be score not boolean
        correct: {
          eng: '',
          tha: ''
        }
      }
    } else if (obj.type === 2){
      obj.ansgroup = {
        answer: -1,
        choice: [{
          label: {
            tha: '',
            eng: ''
          },
          value: '',
          id: 0
        }]
      }
    }else if (obj.type === 3) {
      obj.ansgroup = {
        value: '',
        choice: [{
          label: {
            tha: '',
            eng: ''
          },
          isanswer: false, // answer status if 'false' -> not true answer else if 'true' -> is true answer
          answer: false // default = false -> selected = true
        }]
      }
    } else if (obj.type === 4){
      obj.ansgroup = {
        value: '',
        sentence: [{
          label: {
            eng: '',
            tha: ''
          },
          value: '', // default = 0 -> sort a number
          answer: '' // default = 0 -> input number here
        }]
      }
    } else if (obj.type === 5){
      obj.ansgroup = {
        left: [{
          label: {
            tha: '',
            eng: ''
          },
          value: 1, // keep id in right
          answer: ''
        }],
        right: [{
          id:1,
          label: {
            tha: '',
            eng: ''
          }
        }],
        value: ''
      }
    } else if (obj.type === 6){
      obj.ansgroup = {
        left: [{
          value: 1,
          picture: 'white.jpg',
          waitingload: false,
          answer: '',
          selected: {
            isselected: false,
            numpair: 0
          }
        }],
        right: [{
          picture: 'white.jpg',
          id: 1,
          waitingload: false,
          selected: {
            isselected: false,
            numpair: 0
          }
        }],
        value: ''
      }
    } else if (obj.type === 7){
      obj.ansgroup = {
        left: [{
          value: 1,
          picture: 'white.jpg',
          waitingload: false,
          answer: ''
        }],
        right: [{
          label: {
            eng: '',
            tha: ''
          },
          id: 1
        }],
        value: ''
      }
    } else if (obj.type === 8){
      obj.ansgroup = [{
        question: {
          tha: '',
          eng: ''
        },
        videoproperty: {
          isvideo: false,
          urlvideo: ''
        },
        audioproperty: {
          isaudio: false,
          urlaudio: ''
        },
        pictureproperty: {
          ispicture: false,
          urlpicture: 'defaultperson.jpg'
        },
        ansgroup: {},
        type: 0
      }]
    }
  }
  addQuestion(){
    this.question.push({
      question: { tha: '', eng: '' },
      videoproperty: { isvideo: false, urlvideo: '' },
      audioproperty: { isaudio: false, urlaudio: '' },
      pictureproperty: { ispicture: false, urlpicture: 'defaultperson.jpg' },
      type: ''
    })
  }
  deleteQuestion(index){
    this.question.splice(index, 1)
  }
  addSubQuestion(obj){
    obj.push({
      question: { tha: '', eng: '' },
      videoproperty: { isvideo: false, urlvideo: '' },
      audioproperty: { isaudio: false, urlaudio: '' },
      pictureproperty: { ispicture: false, urlpicture: 'defaultperson.jpg' },
      type: ''
    })
  }
  addSingleChoice(obj, inc){
    let increments = inc+1
    obj.push({
      label: {
        tha: '',
        eng: ''
      },
      value: '',
      id: increments
    })
  }
  addMultiChoice(obj){
    obj.choice.push({
      label: {
        tha: '',
        eng: ''
      },
      isanswer: false,
      answer: false
    })
  }
  addSentence(obj){
    obj.push({
      label: {
        tha: '',
        eng: ''
      },
      value: '',
      answer: ''
    })
  }
  addPairText(obj, inc){
    let increment = inc+1
    obj.left.push({
      label: {
        eng: '',
        tha: ''
      },
      value: increment, // keep id in left
      answer: ''
    })
    obj.right.push({
      id: increment,
      label: {
        eng: '',
        tha: ''
      }
    })
  }
  addPairPicture(obj, inc){
    let increment = inc+1
    obj.left.push({
      value: increment,
      answer: '',
      picture: 'white.jpg',
      waitingload: false,
      selected: {
        isselected: false,
        numpair: 0
      }
    })
    obj.right.push({
      picture: 'white.jpg',
      id: increment,
      waitingload: false,
      selected: {
        isselected: false,
        numpair: 0
      }
    })
  }
  addPairPictureText(obj, inc){
    let increment = inc+1
    obj.left.push({
      value: increment,
      picture: 'white.jpg',
      waitingload: false,
      answer: ''
    })
    obj.right.push({
      label: {
        eng: '',
        tha: ''
      },
      id: increment
    })
  }
  deleteArray(obj, index){
    obj.splice(index, 1)
  }
  getQuestion(){
    return this.question
  }
  getQuestionByIndex(index){
    return this.question[index]
  }
  getTotalScore(question){
    let result = 0

    if (question) {
      question.forEach((data) => {
        if (data.ansgroup != undefined){
          if (data.type != 2 && data.type != 8){
            if ((data.ansgroup.value != undefined) && (data.ansgroup.value != '') && (data.ansgroup.value != null)){
              result += this.getMaxScoreType1(data.ansgroup)
            }
          } else if (data.type === 2){
            result += this.getMaxScoreType2(data.ansgroup)
          } else if (data.type === 8){
            result += this.getTotalScore(data.ansgroup)
          }
        }
      })
    }
    return result
  }
  getRealScore(question){
    let result = 0
   if (question) {
     question.forEach((data) => {
       if (data.ansgroup != undefined){
         // NOTE: type 5-7 is same structure
         switch (data.type) {
           case 1:
             result += this.getScoreType1(data.ansgroup)
             break
           case 2:
             result += this.getScoreType2(data.ansgroup)
             break
           case 3:
             result += this.getScoreType3(data.ansgroup)
             break
           case 4:
             result += this.getScoreType4(data.ansgroup)
             break
           case 5:
             result += this.getScoreType5(data.ansgroup)
             break
           case 6:
             result += this.getScoreType5(data.ansgroup)
             break
           case 7:
             result += this.getScoreType5(data.ansgroup)
             break
           case 8:
             result += this.getRealScore(data.ansgroup)
             break
         }

       }
     })
   }
    return result
  }
  getMaxScoreType1(data){
    let result = 0
    if (data.value != undefined && data.value != '' && data.value != null){
      result = parseInt(data.value)
    }
    return result
  }
  getMaxScoreType2(data){
    return parseInt(Math.max.apply(null, data.choice.map((obj) => obj.value)))
  }
  getScoreType1(data){
    let result = 0
    if (data.status != undefined && data.status != ''){
      result = parseInt(data.status)
    }
    return result
  }
  getScoreType2(data){
    let result = 0
    let objsel = data.choice.find((ch) => ch.id === data.answer )

    if (this.getMaxScoreType2(data) != 0){
      if (objsel !== undefined && objsel.value){
        result = parseInt(objsel.value)
      }
    }

    return result
  }
  getScoreType3(data){
    let result = 0
    let chkobj = data.choice.find((ch) => ch.isanswer != ch.answer )

    if (this.getMaxScoreType1(data) != 0 && chkobj === undefined){
      result = parseInt(data.value)
    }

    return result
  }
  getScoreType4(data){
    let result = 0
    let chkobj = data.sentence.find((sen) => sen.answer != sen.value)

    if (this.getMaxScoreType1(data) != 0 && chkobj === undefined){
      result = parseInt(data.value)
    }

    return result
  }
  getScoreType5(data){
    let result = 0
    let chkobj = data.left.find((lf) => lf.answer != lf.value)

    if (this.getMaxScoreType1(data) != 0 && chkobj === undefined){
      result = parseInt(data.value)
    }

    return result
  }

  //แบบสำรวจบุคลิกภาพ DHAS
  getDHASPIT(router) {
    let isDHASPIT = router.examdata.topic.tha === "ข้อสอบที่ 1 แบบสำรวจบุคลิกภาพ Personality Inventory test" ||
      router.examdata.topic.eng === "ข้อสอบที่ 1 แบบสำรวจบุคลิกภาพ Personality Inventory test"

    if (true) {
      let result = {
        dominance : 0,
        assertiveness : 0,
        needForAchievement : 0,
        orderliness : 0,
        autonomy : 0,
        needForAffiliation : 0,
        nurturance :0,
        endurance : 0,
        needForRecognition : 0,
        initiativeness : 0,
        sociability : 0,
        responsibility : 0,
        honesty : 0,
        positiveAttitude :0,
        selfControl : 0,
        consistency : 0
      }
      if (router.question) {
        let group = {
          one : {
            dominance : [11,25,39,53,67,81,95],
            assertiveness : [4,19,33,47,61,75,89],
            needForAchievement : [5,18,20,48,62,96],
            orderliness : [12,14,28,42,76,90,104],
            autonomy : [32,49,63,77,91,103,105],
            needForAffiliation : [15,26,29,43,49,57,71,83],
            nurturance :[1,13,16,46,56,78,92],
            endurance : [22,30,40,44,51,65,97],
            needForRecognition : [2,27,60,70,79,85,93],
            initiativeness : [6,17,31,36,37,45,54],
            sociability : [41,52,66,74,84,86,99],
            responsibility : [21,24,50,68,80,94],
            honesty : [8,55,58,59,88,98,100,101],
            positiveAttitude :[10,35,38,64,82,87],
            selfControl : [3,7,9,23,69,72,73],
            consistency : [106,107,108,109,110,111,112,113,114,115,116,117,118,119,120]
          },
          two : {
            dominance : [18,32,46,60,74,88,102],
            assertiveness : [11,12,26,40,54,68,82],
            needForAchievement : [4,13,27,34,41,55,69,103],
            orderliness : [6,21,25,35,83,96,97],
            autonomy : [7,19,56,70,76,84,98],
            needForAffiliation : [5,22,36,39,50,64],
            nurturance :[8,15,23,33,85,90,99],
            endurance : [20,37,53,58,63,72,78],
            needForRecognition : [9,29,30,47,86,100,104],
            initiativeness : [24,34,38,67,77,79,99],
            sociability : [14,17,43,44,59,61,73],
            responsibility : [1,48,51,52,81,87,91,93],
            honesty : [3,28,31,57,75,80],
            positiveAttitude :[2,16,62,65,66,95,101,105],
            selfControl : [10,42,45,71,89,94,102],
            consistency : [106,107,108,109,110,111,112,113,114,115,116,117,118,119,120]
          }
        }
        router.question[0].ansgroup.forEach((data, index) => {
          if (data.ansgroup !== undefined){
            let groupUsed = ''
            if (data.ansgroup.answer === 0) {
              //ตอบข้อ 1
              groupUsed = 'one'
            }
            else if (data.ansgroup.answer === 1) {
              //ตอบข้อ 2
              groupUsed = 'two'
            }
            else {
              console.log('error'+ (index+1).toString(), data)
            }
            if (groupUsed) {
              switch (true) {
                case group[groupUsed].dominance.includes(index+1):
                  result.dominance += 1
                  break
                case group[groupUsed].assertiveness.includes(index+1):
                  result.assertiveness += 1
                  break
                case group[groupUsed].needForAchievement.includes(index+1):
                  result.needForAchievement += 1
                  break
                case group[groupUsed].orderliness.includes(index+1):
                  result.orderliness += 1
                  break
                case group[groupUsed].autonomy.includes(index+1):
                  result.autonomy += 1
                  break
                case group[groupUsed].needForAffiliation.includes(index+1):
                  result.needForAffiliation += 1
                  break
                case group[groupUsed].nurturance.includes(index+1):
                  result.nurturance += 1
                  break
                case group[groupUsed].endurance.includes(index+1):
                  result.endurance += 1
                  break
                case group[groupUsed].needForRecognition.includes(index+1):
                  result.needForRecognition += 1
                  break
                case group[groupUsed].initiativeness.includes(index+1):
                  result.initiativeness += 1
                  break
                case group[groupUsed].sociability.includes(index+1):
                  result.sociability += 1
                  break
                case group[groupUsed].responsibility.includes(index+1):
                  result.responsibility += 1
                  break
                case group[groupUsed].honesty.includes(index+1):
                  result.honesty += 1
                  break
                case group[groupUsed].positiveAttitude.includes(index+1):
                  result.positiveAttitude += 1
                  break
                case group[groupUsed].selfControl.includes(index+1):
                  result.selfControl += 1
                  break
                case group[groupUsed].consistency.includes(index+1):
                  let paired = ''
                  switch (index + 1) {
                    case 106 : paired = 13
                      break
                    case 107 : paired = 19
                      break
                    case 108 : paired = 34
                      break
                    case 109 : paired = 25
                      break
                    case 110 : paired = 8
                      break
                    case 111 : paired = 22
                      break
                    case 112 : paired = 49
                      break
                    case 113 : paired = 33
                      break
                    case 114 : paired = 30
                      break
                    case 115 : paired = 38
                      break
                    case 116 : paired = 44
                      break
                    case 117 : paired = 52
                      break
                    case 118 : paired = 75
                      break
                    case 119 : paired = 62
                      break
                    case 120 : paired = 69
                      break
                  }
                  if (paired && data.ansgroup.answer !== router.question[0].ansgroup[paired].ansgroup.answer) {
                    result.consistency += 1
                  }
                  break
              }
            }
          }
        })
      }
      return result
    }
    else {
      return 'Data in wrong format'
    }








  }
}