import Vue from 'vue'
import Cookies from 'js-cookie'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI, { TableColumn } from 'element-ui'
import '@/styles/theme/dark/index.css'
import 'element-ui/lib/theme-chalk/index.css'
import 'element-ui/lib/theme-chalk/display.css'

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'
import i18nPlugin, { attachStore, setLocale } from './lang'
import '@/directive'
import '@/components-register'
import '@/permission' // permission control
import * as filters from './filters'
import { isMock } from '@/utils'
import { checkSwitchTheme } from '@/utils/theme'
import '@/plugin'
import { simpleUploadFlie, radmonFilename, getAccessImgUrl } from '@/api/oss'
import { application } from '@/settings'
import { sysOriginPlatforms } from '@/constant/origin'
import * as opsMessage from '@/utils/ops-message'
import videojs from 'video.js'

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
if (isMock()) {
  const { mockXHR } = require('../mock')
  mockXHR()
}

// initialize ElementUI and runtime locale
const DEFAULT_TABLE_COLUMN_MIN_WIDTH = 120

if (TableColumn && TableColumn.methods && !TableColumn.methods.__defaultMinWidthPatched__) {
  const originalSetColumnWidth = TableColumn.methods.setColumnWidth
  TableColumn.methods.setColumnWidth = function(column) {
    const nextColumn = originalSetColumnWidth.call(this, column)
    const hasExplicitWidth = this.width !== undefined && this.width !== null && this.width !== ''
    const hasExplicitMinWidth = this.minWidth !== undefined && this.minWidth !== null && this.minWidth !== ''
    const isSpecialType = ['selection', 'index', 'expand'].includes(nextColumn.type)

    if (!hasExplicitWidth && !hasExplicitMinWidth && !isSpecialType && nextColumn.minWidth < DEFAULT_TABLE_COLUMN_MIN_WIDTH) {
      nextColumn.minWidth = DEFAULT_TABLE_COLUMN_MIN_WIDTH
      nextColumn.realWidth = nextColumn.width === undefined ? nextColumn.minWidth : nextColumn.width
    }

    return nextColumn
  }
  TableColumn.methods.__defaultMinWidthPatched__ = true
}

Vue.use(ElementUI, { size: Cookies.get('size') || 'medium' })
Vue.use(i18nPlugin)
attachStore(store)
setLocale(store.getters.language || 'en')
// 如果想要中文版 element-ui，按如下方式声明
// Vue.use(ElementUI)

Vue.config.productionTip = false

// 加载过滤器
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})

Object.keys(opsMessage).forEach(key => {
  Vue.prototype['$' + key] = opsMessage[key]
})

// 适配单位换算
function resizeScreenFontSize() {
  const clientWidth = document.documentElement.clientWidth
  document.documentElement.style.margin = 'auto'
  document.documentElement.style.position = 'relative'
  document.documentElement.style.fontSize = (clientWidth / 10) + 'px'
  if (clientWidth >= 540) {
    document.documentElement.style.fontSize = '54px'
    return
  }
}
resizeScreenFontSize()
window.onresize = () => resizeScreenFontSize()

Vue.prototype.$simpleUploadFlie = simpleUploadFlie
Vue.prototype.$radmonFilename = radmonFilename
Vue.prototype.$getAccessImgUrl = getAccessImgUrl
Vue.prototype.$application = application
Vue.prototype.$sysOriginMap = toSysOriginMap()
Vue.prototype.$videoJS = videojs

function toSysOriginMap() {
  const sysOriginMap = {}
  sysOriginPlatforms.forEach(item => {
    sysOriginMap[item.value] = item.label
  })
  return sysOriginMap
}
checkSwitchTheme()
