const { database } = require('../database')
const { URL } = require('./Config')

/**
 * send data etudiants in server
 */
const sendEtudiants = async () => {
  const XHR = new XMLHttpRequest()
  const queryEtudiants = await database.prepare('SELECT * FROM etudiants').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryEtudiants))

  XHR.open('POST', `${URL}/etudiants`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send data matieres to server
 */
const sendMatieres = async () => {
  const XHR = new XMLHttpRequest()
  const queryMatieres = await database.prepare('SELECT * FROM matieres').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryMatieres))

  XHR.open('POST', `${URL}/matieres`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send data users to server
 */
const sendUsers = async () => {
  const XHR = new XMLHttpRequest()
  const queryUsers = database.prepare('SELECT * FROM users').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryUsers))

  XHR.open('POST', `${URL}/users`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send status to server
 */
const sendStatus = async () => {
  const XHR = new XMLHttpRequest()
  const queryStatus = database.prepare('SELECT * FROM status').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryStatus))

  XHR.open('POST', `${URL}/status`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send mention to server
 */
const sendMentions = async () => {
  const XHR = new XMLHttpRequest()
  const queryMentions = database.prepare('SELECT * FROM mentions').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryMentions))

  XHR.open('POST', `${URL}/mentions`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send niveaus to server
 */
const sendNiveaus = async () => {
  const XHR = new XMLHttpRequest()
  const queryNiveaus = database.prepare('SELECT * FROM niveaus').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryNiveaus))

  XHR.open('POST', `${URL}/niveaus`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send semestre to server
 */
const sendSemestres = async () => {
  const XHR = new XMLHttpRequest()
  const querySemestres = database.prepare('SELECT * FROM semestres').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(querySemestres))

  XHR.open('POST', `${URL}/semestres`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send matiereMention to server
 */
const sendMatiereMention = async () => {
  const XHR = new XMLHttpRequest()
  const queryMatieremantion = database.prepare('SELECT * FROM matiere_mention').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryMatieremantion))

  XHR.open('POST', `${URL}/matiere_mention`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send matiereSemestre to server
 */
const sendMatiereSemestre = async () => {
  const XHR = new XMLHttpRequest()
  const queryMatieresemestre = database.prepare('SELECT * FROM matiere_semestre').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryMatieresemestre))

  XHR.open('POST', `${URL}/matiere_semestre`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send notes to server
 */
const sendNotes = async () => {
  const XHR = new XMLHttpRequest()
  const queryNotes = database.prepare('SELECT * FROM notes').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryNotes))

  XHR.open('POST', `${URL}/notes`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send note repechage to server
 */
const sendNotesRepech = async () => {
  const XHR = new XMLHttpRequest()
  const queryNotesRepech = database.prepare('SELECT * FROM notesrepech').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryNotesRepech))

  XHR.open('POST', `${URL}/notesrepech`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send noteSystem to server
 */
const sendNoteSystem = async () => {
  const XHR = new XMLHttpRequest()
  const queryNoteSystem = database.prepare('SELECT * FROM notesystems').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryNoteSystem))

  XHR.open('POST', `${URL}/notesystems`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send annee scolaire to server
 */
const sendAnnee = async () => {
  const XHR = new XMLHttpRequest()
  const queryAnnee = database.prepare('SELECT * FROM anneescolaire').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryAnnee))

  XHR.open('POST', `${URL}/anneescolaire`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send traitement system to server
 */
const sendTraitmentSystem = async () => {
  const XHR = new XMLHttpRequest()
  const queryAnnee = database.prepare('SELECT * FROM traitmentsystem').all()

  XHR.onreadystatechange = () => {
    if (XHR.readyState === 4) {
      if (XHR.status === 200) {
        let response = XHR.responseText

        console.log(response)
      } else {
        console.log('impossible de contacter le serveur')
      }
    }
  }

  const data = new FormData()

  data.append('Verification', JSON.stringify(queryAnnee))

  XHR.open('POST', `${URL}/traitmentsystem`, true)
  XHR.setRequestHeader('x-requested-with', 'xmlhttprequest')
  XHR.send(data)
}

/**
 * send all request to server
 */
async function sendAll() {
  try {
    await sendEtudiants()
    await sendMatieres()
    await sendUsers()
    await sendStatus()
    await sendMentions()
    await sendNiveaus()
    await sendSemestres()
    await sendMatiereMention()
    await sendMatiereSemestre()
    await sendNotes()
    await sendNotesRepech()
    await sendNoteSystem()
    await sendAnnee()
    await sendTraitmentSystem()
  } catch (error) {
    return error
  }
}

module.exports = {
  sendAll
}
