import { app } from 'electron'
import FileSystem from 'fs'
import Ini from 'ini'

const iniPath = app.getAppPath() + '/app.ini'

/**
 * 
 */
const createSettings = () => {
    const config = {
        appName: 'Servidor de Impresión',
        appMode: 'production',
        server: {
            host: '127.0.0.1',
            port: '8070'
        },
        print: {
            alwaysPdf: false
        }
    }

    writeSettings(config)
}

/**
 * 
 */
const settingsExists = () => {
    return FileSystem.existsSync(iniPath)
}

/**
 * 
 */
export const readSettings = () => {
    if (!settingsExists()) {
        createSettings()
    }

    const fileStream = FileSystem.readFileSync(iniPath, 'utf-8')
    return Ini.parse(fileStream)
}

/**
 * 
 * @param {*} settings 
 */
export const writeSettings = settings => {
    const newSettings = Ini.stringify(settings)
    FileSystem.writeFileSync(iniPath, newSettings)
}