import Vue from 'vue'
import DefaultPageView from './default-page.vue'
import defaultPageJson from './default-page.json'

const defaultOptions = {
  inline: false,
  large: true,
}

const DefaultPageConstructor = Vue.extend(DefaultPageView)
let instance = null
const getInstance = () =>
  new DefaultPageConstructor({
    el: document.createElement('div'),
  })

const DefaultPage = (options = {}) => {
  instance = getInstance()
  Object.assign(instance, defaultOptions, options)
  document.body.appendChild(instance.$el)
}

DefaultPage.common = (options = {}) => {
  const defaults = {}
  defaults.image = defaultPageJson[options.type].image
  defaults.text = defaultPageJson[options.type].text
  // eslint-disable-next-line
  new DefaultPage(Object.assign({}, defaults, options))
}

/* eslint-disable no-param-reassign */
Object.keys(defaultPageJson).forEach(key => {
  DefaultPage[key] = (options = {}) => {
    if (typeof options === 'string') {
      options = { text: options }
    }
    const defaults = {
      type: key,
      image: defaultPageJson[key].image,
      text: defaultPageJson[key].text,
    }
    options = Object.assign({}, defaults, options)
    // eslint-disable-next-line
    new DefaultPage(options)
  }
})
/* eslint-enable no-param-reassign */

DefaultPage.view = DefaultPageView
DefaultPage.remove = () => {
  if (instance) {
    instance.visible = false
  }
}
DefaultPage.hide = () => {
  if (instance) {
    instance.visible = false
  }
}
DefaultPage.install = vueGlobal => {
  vueGlobal.use(DefaultPageView)
  /* eslint no-param-reassign: ["Error", { "props": false }] */
  vueGlobal.prototype.$defaultPage = DefaultPage
}

export default DefaultPage
