import React from 'react'
import PropTypes from 'prop-types'
import {reducer as reducerUtil} from 'tocco-util'
import {appFactory, externalEvents} from 'tocco-app-extensions'

import reducers, {sagas} from './modules/reducers'
import ResourceSchedulerContainer from './containers/ResourceSchedulerContainer'

const packageName = 'resource-scheduler'

const initApp = (id, input, events, publicPath) => {
  const content = <ResourceSchedulerContainer/>

  const store = appFactory.createStore(reducers, sagas, input, packageName)
  externalEvents.addToStore(store, events)

  return appFactory.createApp(
    packageName,
    content,
    store,
    {
      input,
      events,
      actions: [],
      publicPath,
      textResourceModules: ['component', 'common', packageName]
    }
  )
}

(() => {
  if (__DEV__ && __PACKAGE_NAME__ === 'resource-scheduler') {
    const input = require('./dev/input.json')

    if (!__NO_MOCK__) {
      const fetchMock = require('fetch-mock')

      const setupFetchMocks = require('./dev/fetchMocks').default
      setupFetchMocks(packageName, fetchMock)
    }

    const app = initApp('dev', input)

    if (module.hot) {
      module.hot.accept('./modules/reducers', () => {
        const reducers = require('./modules/reducers').default
        reducerUtil.hotReloadReducers(app.store, reducers)
      })
    }

    appFactory.renderApp(app.component)
  } else {
    appFactory.registerAppInRegistry(packageName, initApp)
  }
})()

export class ResourceSchedulerApp extends React.Component {
  constructor(props) {
    super(props)
    this.app = initApp('id', props)
  }

  render() {
    return this.app.component
  }
}

ResourceSchedulerApp.propTypes = {
  locale: PropTypes.string,
  onEventClick: PropTypes.func
}

@@ -167,16 +167,58 @@
 ensions'
+%0Aimport %7Bhot%7D from 'react-hot-loader/root'
 %0A%0Aimport
@@ -382,24 +382,70 @@
 scheduler'%0A%0A
+const EXTERNAL_EVENTS = %5B%0A  'onEventClick'%0A%5D%0A%0A
 const initAp
@@ -1654,16 +1654,183 @@
 (props)%0A
+%0A    const events = EXTERNAL_EVENTS.reduce((events, event) =%3E %7B%0A      if (props%5Bevent%5D) %7B%0A        events%5Bevent%5D = props%5Bevent%5D%0A      %7D%0A      return events%0A    %7D, %7B%7D)%0A%0A
     this
@@ -1848,19 +1848,31 @@
 App(
-'
+props.
 id
-'
 , props
+, events
 )%0A  
@@ -1959,16 +1959,40 @@
 pes = %7B%0A
+  id: PropTypes.string,%0A
   locale
@@ -2042,10 +2042,52 @@
 es.func%0A
-
 %7D%0A
+%0Aexport default hot(ResourceSchedulerApp)%0A

improve exported app