import { VideohubAdapterInterface, VideohubConfigInterface, InputInterface, OutputInterface, SerialInterface } from './Types'
import { VideohubState } from './VideohubState'
import { VideohubCommand } from './Command'
import { EventEmitter } from 'events'

export class TestServerState implements VideohubAdapterInterface {
    private state: VideohubState

    private events: EventEmitter

    constructor () {
        this.events = new EventEmitter()

        this.state = new VideohubState()
        this.state.config.inputCount = 72
        this.state.config.outputCount = 72

        this.state.setName('psBroadcastIo32')
        //this.state.setDeviceName('Blackmagic Universal Videohub 72')
        this.state.setDeviceName('Blackmagic Enterprise Videohub')
    }

    getConfig (): VideohubConfigInterface {
        return this.state.config
    }

    getName (): string {
        return this.state.name || ''
    }

    getDeviceName (): string {
        return this.state.deviceName || ''
    }

    getInput (id: number): InputInterface {
        return this.state.getInput(id)
    }

    getOutput (id: number): OutputInterface {
        return this.state.getOutput(id)
    }

    getMonitoringOutput (id: number): OutputInterface {
        return this.state.getMonitoringOutput(id)
    }

    getSerial (id: number): SerialInterface {
        return this.state.getSerial(id)
    }

    process (cmd: VideohubCommand, _clientId: string): boolean {
        setTimeout(() => this.events.emit('changes', cmd), 10)
        return true
    }

    onChanges (handler: (cmd: VideohubCommand) => void): void {
        this.events.on('changes', handler)
    }
}
