var express = require('express')
var webpack = require('webpack')
var config = require('./webpack.dev.conf')
var proxyMiddleware = require('http-proxy-middleware')
// var httpProxy = require('http-proxy')
var app = express()
var compiler = webpack(config)
// var proxy = httpProxy.createProxyServer()
var ldap = 'http://61.134.61.238:8655'
var applyinstall = 'http://61.134.61.238:8655'
var fadada = 'http://61.134.61.238:8655'
var proxyTable = {
  '/baojianFile': {
    target: fadada
  },
  '/testfiles': {
    target: fadada
  },
  '/api/af-auth/login': {
    target: fadada
  },
  '/rs/logic/getAllNode': {
    target: fadada
  },
  '/android.html/rs': {
    target: fadada,
    pathRewrite: {
      '^/android.html': ''
    }
  },
  '/webapps/applyinstall/excel': {
    target: fadada
  },

  '/rs/logic/pushShortUrlSms': {
    target: fadada
  },
  '/rs/logic/generateContract': {
    target: fadada
  },
  '/rs/logic/getAuthSubject': {
    target: fadada
  },
  '/rs/sql/getAuthSubjectList': {
    target: fadada
  },
  '/rs/logic/keyExtSign': {
    target: fadada
  },
  '/rs/logic/keyExtSignAuto': {
    target: fadada
  },
  // 重新发送实名认证短信
  '/rs/logic/anotherAuthSubject': {
    target: fadada
  },
  // ---------------------------
  '/rs/logic/getSaleInitData': {
    target: ldap
  },
  '/rs/logic/saveInstruct': {
    target: 'http://193.168.1.64:8082'
  },
  '/rs/logic/updateNetWorkValve': {
    target: 'http://193.168.1.64:8082'
  },
  '/rs/logic/uploadSave': {
    target: 'http://193.168.1.16:8003'
  },
  '/rs/workflow': {
    target: applyinstall
  },
  '/apply': {
    target: applyinstall
  },
  '/rs/vue': {
    target: ldap
  },
  // 查找资源服务数据
  '/rs/search': {
    target: ldap
  },
  // 发票组织数据
  '/rs/logic/bjEticketCharge': {
    target: 'http://193.168.1.6:8082'
  },
  // 开发票
  '/rs/logic/bjOpenEticket': {
    target: 'http://193.168.1.6:8082'
  },
  // 获取发票明细
  '/rs/logic/queryEticket': {
    target: 'http://193.168.1.44:8400'
  },
  // 创建小区
  '/rs/logic/address_updatearea': {
    target: ldap
  },
  '/rs/phone/send': {
    target: 'http://193.168.1.21:80'
  },
  // 用户登录服务地址
  '/rs/user': {
    target: ldap
  },
  '/rs/logic/getInitData': {
    target: ldap?ldap:applyinstall
  },
  '/rs/logic/getLogin': {
    target: ldap
  },
  '/rs/file': {    // 文件上传
    target: applyinstall
  },
  '/project': {    // 文件回显
    target: applyinstall
  },
  '/rs/apply': {
    target: applyinstall,
    pathRewrite: {
      '^/rs/apply': '/rs'
    }
  },
  '/rs': {
    target: applyinstall
  },
  // '/ws': {
  //   target: applyinstall,
  //   ws: true // 代理webSocket
  // }
}

var devMiddleware = require('webpack-dev-middleware')(compiler, {
  publicPath: config.output.publicPath,
  stats: {
    colors: true,
    chunks: false
  }
})

var hotMiddleware = require('webpack-hot-middleware')(compiler)
// force page reload when html-webpack-plugin template changes
compiler.plugin('compilation', function (compilation) {
  compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
    hotMiddleware.publish({action: 'reload'})
    cb()
  })
})

// proxy api requests
Object.keys(proxyTable).forEach(function (context) {
  var options = proxyTable[context]
  if (typeof options === 'string') {
    options = {target: options}
  }
  app.use(proxyMiddleware(context, options))
})

// handle fallback for HTML5 history API
app.use(require('connect-history-api-fallback')())
// app.use(function (req, res, next) {
//   res.header('Access-Control-Allow-Origin', '*')
//   res.header('Access-Control-Allow-Headers', 'X-Requested-With')
//   res.header('Access-Control-Allow-Methods', 'PUT, POST, GET,DELETE, OPTIONS')
//   res.header('X-Powered-By', '3,2,1')
//   res.header('Access-Control-Allow-Credentials', 'true')
//   res.header('Content-Type', 'application/json;charset=utf-8')
//   next()
// })

// serve webpack bundle output
app.use(devMiddleware)

// enable hot-reload and state-preserving
// compilation error display
app.use(hotMiddleware)

// serve pure static assets
app.use('/static', express.static('./static'))

module.exports = app.listen(8400, function (err) {
  if (err) {
    console.log(err)
    return
  }
  console.log('Listening at http://localhost:8400')
})
