'use strict'
/* @flow */

import engine from '../engine'

export function enableNotifications () {
  console.log('setting up notification')

  const param = {
    channels: {
      session: true,
      users: true
    }
  }

  engine.listenOnConnect(() => {
    engine.rpc('notifyCtl.setNotifications', param, {}, (error, response) => {
      if (error != null) {
        console.error('error in toggling notifications: ', error)
      } else {
        console.log('Enabled Notifications')
      }
    })
  })
}

const listeners = {
  'keybase.1.NotifySession.loggedOut': logoutNotification
}

// Returns function that should be called to unbind listeners
export function bindNotifications () {
  Object.keys(listeners).forEach(k => engine.listenGeneralIncomingRpc(k, listeners[k]))
  return () => {
    Object.keys(listeners).forEach(k => engine.unlistenGeneralIncomingRpc(k, listeners[k]))
  }
}

function logoutNotification (param) {
  new Notification('Logged Out') // eslint-disable-line
}

@@ -49,16 +49,54 @@
 /engine'
+%0Aimport Server from '../engine/server'
 %0A%0Aexport
@@ -535,16 +535,1941 @@
 %7D%0A    %7D)
+%0A%0A    // TODO move this somewhere else%0A    engine.rpc('delegateUiCtl.registerIdentifyUI', %7B%7D, %7B%7D, (error, response) =%3E %7B%0A      if (error != null) %7B%0A        console.error('error in registering identify ui: ', error)%0A      %7D else %7B%0A        console.log('Registered identify ui')%0A      %7D%0A    %7D)%0A%0A    type IdentifyKey = any%0A%0A    const identifyUi = %7B%0A      start: (params: %7BsessionID: number, username: string%7D) =%3E %7B%0A        console.log('starting identify ui server instance')%0A      %7D,%0A      displayKey: (params: %7BsessionID: number, key: IdentifyKey%7D) =%3E %7B%0A        console.log('displaying key', params)%0A      %7D,%0A      reportLastTrack: (params: %7BsessionID: number, track: any%7D) =%3E %7B%0A        console.log('Report last track', params)%0A      %7D,%0A      launchNetworkChecks: (params: %7BsessionID: number, identity: any, user: any%7D) =%3E %7B%0A        console.log('network checks', params)%0A      %7D,%0A      displayTrackStatement: (params: %7BsessionID: number, stmt: string%7D) =%3E %7B%0A        console.log('display track statements', params)%0A      %7D,%0A%0A      finishWebProofCheck: (params: %7BsessionID: number, rp: any, lcr: any%7D) =%3E %7B%0A        console.log('finish web proof', params)%0A      %7D,%0A      finishSocialProofCheck: (params: %7BsessionID: number, rp: any, lcr: any%7D) =%3E %7B%0A        console.log('finish social proof', params)%0A      %7D,%0A      displayCryptocurrency: (params: %7BsessionID: number, c: any%7D) =%3E %7B%0A        console.log('finish displayCryptocurrency', params)%0A      %7D,%0A      confirm: (params: %7BsessionID: number, outcome: any%7D): bool =%3E %7B%0A        console.log('confirm', params)%0A        return true%0A      %7D,%0A      finish: (params: %7BsessionID: number%7D) =%3E %7B%0A        console.log('finish', params)%0A      %7D%0A    %7D%0A%0A    const identifyUIServer = new Server(%0A      engine,%0A      'keybase.1.identifyUi.delegateIdentifyUI',%0A      'keybase.1.identifyUi.finish',%0A      (params) =%3E %7B return %7B keybase: %7B 1: %7B identifyUi %7D %7D %7D %7D%0A    )%0A%0A    identifyUIServer.listen()
 %0A  %7D)%0A%7D%0A

Add temporary spot to register delegate identify UI and server for identify UI