const spawn = require('child_process').spawn
const events = require('events')
const fs = require('fs')
const path = require('path')

class FFMPEG_TEXT extends events.EventEmitter {
    constructor (opts, title, subtitle) {
        super()
        this.args = [
            '-threads', opts.threads,
            '-f', 'lavfi',
            '-re',
            '-stream_loop', '-1',
            '-i', `color=c=black:s=${opts.videoResolution}`,
            '-f', 'lavfi',
            '-i', 'anullsrc',
            '-vf', `drawtext=fontfile=${process.env.DATABASE}/font.ttf:fontsize=30:fontcolor=white:x=(w-text_w)/2:y=(h-text_h)/2:text='${title}',drawtext=fontfile=${process.env.DATABASE}/font.ttf:fontsize=20:fontcolor=white:x=(w-text_w)/2:y=(h+text_h+20)/2:text='${subtitle}'`,
            '-c:v', opts.videoEncoder,
            '-c:a', opts.audioEncoder,
            '-f', 'mpegts',
            'pipe:1'
        ]

        this.ffmpeg = spawn(opts.ffmpegPath, this.args)

        this.ffmpeg.stdout.on('data', (chunk) => {
            this.emit('data', chunk)
        })

        if (opts.logFfmpeg) {
            this.ffmpeg.stderr.on('data', (chunk) => {
                process.stderr.write(chunk)
            })
        }

        this.ffmpeg.on('close', (code) => {
            if (code === null)
                this.emit('close', code)
            else if (code === 0)
                this.emit('close', code)
            else if (code === 255)
                this.emit('close', code)
            else
                this.emit('error', { code: code, cmd: `${opts.ffmpegPath} ${this.args.join(' ')}` })
        })
    }
    kill() {
        this.ffmpeg.kill()
    }
}

module.exports = FFMPEG_TEXT