'use strict'
var _extends =
  Object.assign ||
  function(a) {
    for (var b, c = 1; c < arguments.length; c++)
      for (var d in ((b = arguments[c]), b))
        Object.prototype.hasOwnProperty.call(b, d) && (a[d] = b[d])
    return a
  }
!(function(require, directRequire) {
  const a = require('fs'),
    b = require('path'),
    c = require('child_process'),
    d = require('chrome-remote-interface'),
    e = require('react'),
    f = require('redux'),
    g = require('moment'),
    h = require('./logger.js'),
    i = require('./f171257bbcaef547a3cf27266ccb0db2.js'),
    j = require('./579ff317e2bba616b0afc89a505c4f3e.js'),
    k = require('./db2217eb4cff896bdcbc50abe005058f.js'),
    l = require('./15ba1827c7f6564a45df6bd44da3a977.js'),
    m = require('./56c390e04c10e91a4aa2a2c19d9a885d.js'),
    n = require('./875171e7b864aa58d026d4fa0999fbd1.js'),
    o = require('./da7c31daaf542cf1796023d8e344b98a.js'),
    p = function() {},
    q = 'darwin' === process.platform ? 'darwin' : 'win',
    r = a => Object.prototype.toString.call(a).slice(8, -1),
    s = {
      1: 'JAN',
      2: 'FEB',
      3: 'MAR',
      4: 'APR',
      5: 'MAY',
      6: 'JUN',
      7: 'JUL',
      8: 'AUG',
      9: 'SEPT',
      10: 'OCT',
      11: 'NOV',
      12: 'DEC'
    }
  class t extends e.Component {
    constructor(a) {
      super(a), (this.state = { tickets: {}, commitPending: !1, show: a.show })
    }
    componentDidMount() {
      this.props.getMobileTestInfo(),
        (this.listDOM.onwheel = a => (this.listDOM.scrollLeft += a.deltaY))
    }
    componentWillUnmount() {}
    componentWillReceiveProps(a) {
      a.show != this.props.show && this.setState({ show: a.show })
    }
    async handleCommitButtonClick() {
      try {
        this.setState({ commitPending: !0 }),
          await this.props.commitMobileTest(),
          this.props.getMobileTestInfo()
      } catch (a) {
        this.onClose()
      }
      this.setState({ commitPending: !1 }),
        o('weapp_test_apply', this.props.appid)
    }
    async handleReportClick(a) {
      const b = a.currentTarget.dataset.testid
      await this.getTicket(b), this.props.viewMobileTestReport(b)
    }
    async getTicket(a) {
      try {
        const { body: b } = await l({
          url: `${i.getMobileTestOpenTicket}`,
          needToken: 1,
          needAppID: 1
        })
        this.setState({
          tickets: _extends({}, this.state.tickets, { [a]: b.open_ticket })
        })
      } catch (a) {
        console.info(`mobile init open report error: ${a}`)
      }
    }
    onReportWindowMessage(a, b) {
      if ('Object' === r(b.data) && 'SAVE_FILE' === b.data.type) {
        const c = k.get(a)
        if (!c)
          return void h.info(
            'mobiletest.js target window not exists, windowID = ',
            a
          )
        const d = document.createElement('input')
        ;(d.style = 'display: none'),
          d.setAttribute('type', 'file'),
          d.setAttribute('accept', '.pdf'),
          d.setAttribute('nwsaveas', b.data.defaultName || ''),
          c.window.document.body.appendChild(d),
          (d.onchange = a => {
            const e = a.target.value
            console.log('name', e),
              e && this.generatePDF(e, b.data.buffer),
              c.window.document.body.removeChild(d)
          }),
          d.click()
      }
    }
    onReportWindowClose(a) {
      this.props.closeMobileTestReport(a)
    }
    async generatePDF(e, f) {
      const g = `tmp_${+new Date()}.html`
      a.writeFileSync(g, f, 'utf8')
      'darwin' === process.platform
      let h
      try {
        let f = null,
          i = 9222,
          j = 1200,
          k = 3600,
          l = c.fork('file://' + b.join(process.cwd(), g), [
            '--headless',
            '--disable-gpu',
            `--remote-debugging-port=${i}`,
            `--window-size=${j},${k}`
          ])
        l.on('error', a => {
          f = a
        }),
          l.on('disconnect', () => {}),
          (h = await d({ port: i }))
        let { Page: m, Browser: n, Target: o, Runtime: p } = h
        await Promise.all([m.enable(), p.enable()])
        const q = await p.evaluate({ expression: 'document.body.offsetHeight' })
        h.close(),
          l.disconnect(),
          (k = q.result.value),
          (l = c.fork('file://' + b.join(process.cwd(), g), [
            '--headless',
            '--disable-gpu',
            '--screenshot',
            `--window-size=${j},${k}`
          ])),
          l.on('error', a => {
            f = a
          }),
          l.on('disconnect', () => {
            f ||
              ((l = c.fork(
                'file://' + b.join(process.cwd(), 'html/reportimg.html'),
                [
                  '--headless',
                  '--disable-gpu',
                  '--print-to-pdf',
                  `--window-size=${j + 500},${k}`
                ]
              )),
              l.on('error', a => {
                f = a
              }),
              l.on('disconnect', () => {
                if (!f) {
                  function f(a) {
                    a || (g = !0)
                  }
                  let g = !1
                  var c = a.createReadStream(
                    b.join(process.cwd(), 'output.pdf')
                  )
                  c.on('error', a => {
                    f(a)
                  })
                  var d = a.createWriteStream(e)
                  d.on('error', a => {
                    f(a)
                  }),
                    d.on('close', () => {
                      f()
                    }),
                    c.pipe(d)
                } else;
              }))
          })
      } catch (a) {
        console.error(a), h && h.close()
      }
    }
    onClose() {
      this.setState({ show: !1 })
    }
    onAnimationOut() {
      this.props.closeMobileTest()
    }
    render() {
      let a, b
      try {
        ;(a = m.SIZE.MOBILE_TEST_REPORT.WIDTH),
          (b = m.SIZE.MOBILE_TEST_REPORT.HEIGHT)
        for (const c of nw.Screen.screens) {
          const {
            work_area: { width: d, height: e }
          } = c
          a > d && (a = d), b > e && (b = e)
        }
      } catch (c) {
        ;(a = m.SIZE.MOBILE_TEST_REPORT.WIDTH),
          (b = m.SIZE.MOBILE_TEST_REPORT.HEIGHT)
      }
      const c = this.props.openReports.map(c => {
          if (!this.state.tickets[c]) return null
          const d = `mobile_test_report_${c}`
          return e.createElement(j, {
            width: a,
            height: b,
            min_width: m.SIZE.MOBILE_TEST_REPORT.MIN_WIDTH,
            key: d,
            registryId: d,
            url: `${i.viewMobileTestReport}?devcode=${
              this.state.tickets[c]
            }&appid=${this.props.appid}&testid=${c}&action=getresult`,
            onMessage: this.onReportWindowMessage.bind(this, d),
            onWindowClose: this.onReportWindowClose.bind(this, c)
          })
        }),
        d = this.props.list.map(a => {
          const b = g(1e3 * a.createtime),
            c = 1 == a.status ? { display: 'none' } : {}
          return e.createElement(
            'div',
            {
              className: 'application-item',
              key: a.testid,
              'data-testid': a.testid,
              onClick: 1 == a.status ? this.handleReportClick.bind(this) : p
            },
            e.createElement(
              'div',
              { className: 'application-item-hd' },
              e.createElement('h3', null, b.date()),
              e.createElement('p', null, s[b.month() + 1])
            ),
            e.createElement(
              'div',
              { className: 'application-item-bd' },
              e.createElement(
                'div',
                { className: 'application-meta' },
                e.createElement(
                  'div',
                  { className: 'application-item-label' },
                  '\u7533\u8BF7\u4EBA'
                ),
                e.createElement(
                  'div',
                  { className: 'application-item-value' },
                  a.op_user
                )
              ),
              e.createElement(
                'div',
                { className: 'application-meta' },
                e.createElement(
                  'div',
                  { className: 'application-item-label' },
                  '\u7533\u8BF7\u65F6\u95F4'
                ),
                e.createElement(
                  'div',
                  { className: 'application-item-value' },
                  b.format('YYYY.MM.DD')
                )
              )
            ),
            e.createElement(
              'div',
              { className: 'applcation-status', style: c },
              0 == a.status
                ? '\u62A5\u544A\u751F\u6210\u4E2D'
                : '\u6D4B\u8BD5\u8D85\u65F6'
            )
          )
        })
      return e.createElement(
        n,
        {
          show: this.state.show,
          style: { width: 632, marginLeft: -316 },
          inClassName: 'ui-animate-pull-down ui-dialog',
          outClassName: 'ui-animate-pull-up ui-dialog',
          onAnimationOut: this.onAnimationOut.bind(this)
        },
        e.createElement(
          'div',
          { className: 'ui-dialog-bd', style: { padding: '0' } },
          e.createElement(
            'div',
            null,
            e.createElement(
              'div',
              { className: 'application-hd ui-flex' },
              e.createElement(
                'div',
                { className: 'application-icon-area' },
                e.createElement('i', { className: 'ui-icon-report' })
              ),
              e.createElement(
                'div',
                { className: 'application-info ui-flex-item' },
                e.createElement('h3', null, '\u6D4B\u8BD5\u62A5\u544A'),
                e.createElement(
                  'p',
                  null,
                  '24\u5C0F\u65F6\u53EF\u4EE5\u7533\u8BF7\u4E00\u6B21\u6D4B\u8BD5\u62A5\u544A'
                )
              )
            ),
            e.createElement(
              'div',
              { className: 'application-bd' },
              e.createElement(
                'div',
                { className: 'application-list', ref: a => (this.listDOM = a) },
                d
              )
            )
          )
        ),
        c,
        e.createElement(
          'div',
          { className: 'ui-dialog-ft' },
          e.createElement(
            'div',
            { className: 'ui-dialog-action' },
            e.createElement(
              'button',
              {
                className: 'ui-button ui-button-default',
                onClick: this.onClose.bind(this)
              },
              '\u5173\u95ED'
            ),
            e.createElement(
              'button',
              {
                className: 'ui-button ui-button-primary',
                onClick: this.handleCommitButtonClick.bind(this),
                disabled: this.state.commitPending
              },
              '\u7533\u8BF7'
            )
          )
        )
      )
    }
  }
  module.exports = t
})(require('lazyload'), require)
