import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
const { getNessesarytable } = require('../../database/function/System')
const { getAllUsers } = require('../../database/Models/Users')
const { getAllEtudiants, getDataToDashboard } = require('../../database/Models/Etudiants')
const { verifyEtudiantIfHeHasNotes, blockShowMoyene } = require('../../database/Models/Notes')
const { getMatiere, getSemestre, getEnseignants } = require('../../database/Models/Matieres')
const { getSysteme } = require('../../database/Models/NoteSysrem')
const { getStatus } = require('../../database/Models/Status')
const { getAnneeScolaire, getInterval } = require('../../database/Models/AnneeScolaire')
const { getMentions } = require('../../database/Models/Mentions')
const { getAll } = require('../../database/api/Get')
const { getParcours } = require('../../database/Models/Parcours')
const { getNiveau } = require('../../database/Models/Niveau')
const { getIPConfig } = require('../../database/Models/IpConfig')

// Custom APIs for renderer
const api = {}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)

    /**
     * contextBridge for Tray
     */
    contextBridge.exposeInMainWorld('Tray', {
      onNavigate: (callback) => {
        ipcRenderer.on('navigateToRoute', (event, route) => {
          callback(route) // Pass the route to the renderer callback
        })
      }
    })

    /**
     * contextBridge for users
     */
    contextBridge.exposeInMainWorld('allUser', {
      users: () => getAllUsers(),
      login: (credentials) => ipcRenderer.invoke('login', credentials),
      insertUsers: (credentials) => ipcRenderer.invoke('insertUser', credentials),
      forgotPassword: (credentials) => ipcRenderer.invoke('forgotPassword', credentials),
      quit: () => ipcRenderer.invoke('quit'),
      minimize: () => ipcRenderer.invoke('minimize'),
      updateUsers: (credentials) => ipcRenderer.invoke('updateUsers', credentials)
    })

    contextBridge.exposeInMainWorld('syncro', {
      getall: () => getAll()
    })

    // syncronisation des donner
    window.addEventListener('online', async () => {
      if (navigator.onLine) {
        // synchronizeData()
        // synchronizeDataEtudiants()
        // synchronizeDataNotes()
        await getAll()
      }
    })
    // send data
    getAll()

    /**
     * contextBridge for etudiants
     */
    contextBridge.exposeInMainWorld('etudiants', {
      insertEtudiant: (credentials) => ipcRenderer.invoke('insertEtudiant', credentials),
      getEtudiants: () => getAllEtudiants(),
      FilterDataByNiveau: (credential) => ipcRenderer.invoke('getByNiveau', credential),
      getSingle: (credential) => ipcRenderer.invoke('single', credential),
      updateEtudiants: (credentials) => ipcRenderer.invoke('updateETudiants', credentials),
      getDataToDashboards: () => getDataToDashboard(),
      updateEtudiantsPDP: (credentials) => ipcRenderer.invoke('updateETudiantsPDP', credentials),
      importExcel: (credentials) => ipcRenderer.invoke('importexcel', credentials),
      changeParcours: (credentials) => ipcRenderer.invoke('changeParcours', credentials),
      createTranche: (credentials) => ipcRenderer.invoke('createTranche', credentials),
      getTranche: (credentials) => ipcRenderer.invoke('getTranche', credentials),
      updateTranche: (credentials) => ipcRenderer.invoke('updateTranche', credentials),
      deleteTranche: (credentials) => ipcRenderer.invoke('deleteTranche', credentials),
      deleteEtudiant: (id) => ipcRenderer.invoke('deleteEtudiant', id),
      getSingleTranche: (credentials) => ipcRenderer.invoke('getSingleTranche', credentials)
    })

    /**
     * cobtextBridge for niveaus
     */
    contextBridge.exposeInMainWorld('niveaus', {
      getNiveau: () => getNiveau(),
      getSingleNiveau: (credential) => ipcRenderer.invoke('singleNiveau', credential),
      insertNiveau: (credentials) => ipcRenderer.invoke('insertNiveau', credentials),
      updateSingleNiveau: (credentials) => ipcRenderer.invoke('updateSingleNiveau', credentials),
      importNiveau: (credentials) => ipcRenderer.invoke('importNiveau', credentials),
      deleteNiveaus: (credentials) => ipcRenderer.invoke('deleteNiveaus', credentials)
    })

    /**
     * contextBridge for notes
     */
    contextBridge.exposeInMainWorld('notes', {
      getNotes: (credentials) => ipcRenderer.invoke('getSingleNote', credentials),
      insertNote: (credentials) => ipcRenderer.invoke('insertNote', credentials),
      updateNote: (credentials) => ipcRenderer.invoke('updatetNote', credentials),
      getMoyenne: (credentials) => ipcRenderer.invoke('getMoyene', credentials),
      noteMatiere: (credentials) => ipcRenderer.invoke('noteMatiere', credentials),
      noteRelerer: (credentials) => ipcRenderer.invoke('noteRelerer', credentials),
      getMoyenneVerify: () => verifyEtudiantIfHeHasNotes(),
      getblockNote: () => blockShowMoyene()
    })

    /**
     * contextbridge for note repechage
     */
    contextBridge.exposeInMainWorld('noteRepech', {
      getNotesRepech: (credentials) => ipcRenderer.invoke('getNotesRepech', credentials),
      updateNoteRepech: (credentials) => ipcRenderer.invoke('updatetNoteRepech', credentials),
      getMoyenneRepech: (credentials) => ipcRenderer.invoke('getMoyenneRepech', credentials)
    })

    /**
     * contextBridge for matieres
     */
    contextBridge.exposeInMainWorld('matieres', {
      getMatiere: () => getMatiere(),
      createMatiere: (credentials) => ipcRenderer.invoke('createMatiere', credentials),
      getMatiereByID: (credentials) => ipcRenderer.invoke('getMatiereByID', credentials),
      updateMatiere: (credentials) => ipcRenderer.invoke('updateMatiere', credentials),
      updateMatiereNiveau: (credentials) => ipcRenderer.invoke('updateMatiereNiveau', credentials),
      importExcel: (credentials) => ipcRenderer.invoke('importExcelMatiere', credentials),
      displayMatiereFromForm: (credentials) =>
        ipcRenderer.invoke('displayMatiereFromForm', credentials),
        deleteMatiere: (id) => ipcRenderer.invoke('deleteMatiere', id),
      asign: (credentials) => ipcRenderer.invoke('asign', credentials),
      getAsign: (credentials) => ipcRenderer.invoke('getAsign', credentials),
      asignSemestre: (credentials) => ipcRenderer.invoke('asignSemestre', credentials),
      getSemestreMatiere: (credentials) => ipcRenderer.invoke('getSemestreMatiere', credentials),
      getSemestre: () => getSemestre(),
      getNessesary: () => getNessesarytable(),
      getENseignant: () => getEnseignants(),
      insertUpdateMentionSemestre: (credentials) =>
        ipcRenderer.invoke('insertUpdateMentionSemestre', credentials),
      updateNessesary: (credentials) => ipcRenderer.invoke('updateNessesary', credentials),
      insertProf: (credentials) => ipcRenderer.invoke('insertProf', credentials),
      getSingleProf: (credentials) => ipcRenderer.invoke('getSingleProf', credentials),
      updateProf: (credentials) => ipcRenderer.invoke('updateProf', credentials)
    })

    /**
     * contextBridge for note systeme
     */
    contextBridge.exposeInMainWorld('notesysteme', {
      getSysteme: () => getSysteme(),
      updateNoteSysteme: (credentials) => ipcRenderer.invoke('updateNoteSysteme', credentials),
      insertParcours: (credentials) => ipcRenderer.invoke('insertParcours', credentials),
      deleteParcours: (id) => ipcRenderer.invoke('deleteParcours', id),
      getSingleParcours: (credentials) => ipcRenderer.invoke('getSingleParcours', credentials),
      updateParcours: (credentials) => ipcRenderer.invoke('updateParcours', credentials),
      parcourMatiere: (credentials) => ipcRenderer.invoke('parcourMatiere', credentials),
      getParcours: () => getParcours(),
      extractFiches: (credentials) => ipcRenderer.invoke('extractFiches', credentials),
      getParcourMatiere: (credentials) => ipcRenderer.invoke('getParcourMatiere', credentials),
      createIPConfig: (credentials) => ipcRenderer.invoke('createIPConfig', credentials),
      getIPConfig: () => getIPConfig(),
      updateIPConfig: (credentials) => ipcRenderer.invoke('updateIPConfig', credentials)
    })

    /**
     * contextbridge for status
     */
    contextBridge.exposeInMainWorld('statuss', {
      getStatus: () => getStatus()
    })

    /**
     * contextbridge for annee scolaire
     */
    contextBridge.exposeInMainWorld('anneescolaire', {
      getAnneeScolaire: () => getAnneeScolaire(),
      getInterval: () => getInterval(),
      createAnneeScolaire: (credentials) => ipcRenderer.invoke('createAnneeScolaire', credentials),
      deleteAnneeScolaire: (credentials) => ipcRenderer.invoke('deleteAnneeScolaire', credentials),
      getSingleAnneeScolaire: (credentials) =>
        ipcRenderer.invoke('getSingleAnneeScolaire', credentials),
      updateAnneeScolaire: (credentials) => ipcRenderer.invoke('updateAnneeScolaire', credentials),
      setCurrent: (credentials) => ipcRenderer.invoke('setCurrent', credentials)
    })

    /**
     * contextbridge for mention
     */
    contextBridge.exposeInMainWorld('mention', {
      createMention: (credentials) => ipcRenderer.invoke('createMention', credentials),
      getMention: () => getMentions(),
      getSingleMention: (credentials) => ipcRenderer.invoke('getSingleMention', credentials),
      updateMention: (credentials) => ipcRenderer.invoke('updateMention', credentials),
      deleteMention: (credentials) => ipcRenderer.invoke('deleteMention', credentials)
    })
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}
