/* MOCK DEV
 *
 * This is a utility module.
 * It initializes a minimalist browserifiable project
 * that contains the Metamask UI, with a local background process.
 *
 * Includes a state reset button for restoring to initial state.
 *
 * This is a convenient way to develop and test the plugin
 * without having to re-open the plugin or even re-build it.
 *
 * To use, run `npm run mock`.
 */

const render = require('react-dom').render
const h = require('react-hyperscript')
const Root = require('../ui/app/root')
const configureStore = require('../ui/app/store')
const actions = require('../ui/app/actions')
const states = require('./states')
const backGroundConnectionModifiers = require('./backGroundConnectionModifiers')
const Selector = require('./selector')
const MetamaskController = require('../app/scripts/metamask-controller')
const firstTimeState = require('../app/scripts/first-time-state')
const ExtensionPlatform = require('../app/scripts/platforms/extension')
const noop = function () {}

const log = require('loglevel')
window.log = log
log.setLevel('debug')

//
// Query String
//

const qs = require('qs')
const routerPath = window.location.href.split('#')[1]
let queryString = {}
let selectedView

if (routerPath) {
  queryString = qs.parse(routerPath.split('?')[1])
}

selectedView = queryString.view || 'first time'
const firstState = states[selectedView]
updateQueryParams(selectedView)

function updateQueryParams (newView) {
  queryString.view = newView
  const params = qs.stringify(queryString)
  const locationPaths = window.location.href.split('#')
  const routerPath = locationPaths[1] || ''
  const newPath = locationPaths[0] + '#' + routerPath.split('?')[0] + `?${params}`

  if (window.location.href !== newPath) {
    window.location.href = newPath
  }
}

//
// CSS
//

const MetaMaskUiCss = require('../ui/css')
const injectCss = require('inject-css')

//
// MetaMask Controller
//

const controller = new MetamaskController({
  // User confirmation callbacks:
  showUnconfirmedMessage: noop,
  unlockAccountMessage: noop,
  showUnapprovedTx: noop,
  platform: {},
  // initial state
  initState: firstTimeState,
})
global.metamaskController = controller
global.platform = new ExtensionPlatform()

//
// User Interface
//

actions._setBackgroundConnection(controller.getApi())
actions.update = function (stateName) {
  selectedView = stateName
  updateQueryParams(stateName)
  const newState = states[selectedView]
  return {
    type: 'GLOBAL_FORCE_UPDATE',
    value: newState,
  }
}

function modifyBackgroundConnection (backgroundConnectionModifier) {
  const modifiedBackgroundConnection = Object.assign({}, controller.getApi(), backgroundConnectionModifier)
  actions._setBackgroundConnection(modifiedBackgroundConnection)
}

var css = MetaMaskUiCss()
injectCss(css)

// parse opts
var store = configureStore(firstState)

// start app
startApp()

function startApp () {
  const body = document.body
  const container = document.createElement('div')
  container.id = 'test-container'
  body.appendChild(container)

  render(
    h('.super-dev-container', [

      h('button', {
        onClick: (ev) => {
          ev.preventDefault()
          store.dispatch(actions.update('terms'))
        },
        style: {
          margin: '19px 19px 0px 19px',
        },
      }, 'Reset State'),

      h(Selector, {
        actions,
        selectedKey: selectedView,
        states,
        store,
        modifyBackgroundConnection,
        backGroundConnectionModifiers,
      }),

      h('#app-content', {
        style: {
          height: '500px',
          width: '360px',
          boxShadow: 'grey 0px 2px 9px',
          margin: '20px',
        },
      }, [
        h(Root, {
         store: store,
        }),
      ]),

    ]
  ), container)
}
on>
<option value='branches/2016Q3'>branches/2016Q3</option>
<option value='branches/2016Q4'>branches/2016Q4</option>
<option value='branches/2017Q1'>branches/2017Q1</option>
<option value='branches/2017Q2'>branches/2017Q2</option>
<option value='branches/2017Q3'>branches/2017Q3</option>
<option value='branches/2017Q4'>branches/2017Q4</option>
<option value='branches/2018Q1'>branches/2018Q1</option>
<option value='branches/2018Q2'>branches/2018Q2</option>
<option value='branches/2018Q3'>branches/2018Q3</option>
<option value='branches/2018Q4'>branches/2018Q4</option>
<option value='branches/2019Q1'>branches/2019Q1</option>
<option value='branches/2019Q2'>branches/2019Q2</option>
<option value='branches/2019Q3'>branches/2019Q3</option>
<option value='branches/2019Q4'>branches/2019Q4</option>
<option value='branches/2020Q1'>branches/2020Q1</option>
<option value='branches/2020Q2'>branches/2020Q2</option>
<option value='branches/2020Q3'>branches/2020Q3</option>
<option value='branches/2020Q4'>branches/2020Q4</option>
<option value='branches/2021Q1'>branches/2021Q1</option>
<option value='branches/RELEASE_8_4_0'>branches/RELEASE_8_4_0</option>
<option value='branches/RELENG_2_1_0'>branches/RELENG_2_1_0</option>
<option value='branches/RELENG_2_2'>branches/RELENG_2_2</option>
<option value='branches/RELENG_9_1_0'>branches/RELENG_9_1_0</option>
<option value='branches/RELENG_9_2_0'>branches/RELENG_9_2_0</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash-4.17.15'>dependabot/npm_and_yarn/devel/electron4/files/lodash-4.17.15</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash.merge-4.6.2'>dependabot/npm_and_yarn/devel/electron4/files/lodash.merge-4.6.2</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash.template-4.5.0'>dependabot/npm_and_yarn/devel/electron4/files/lodash.template-4.5.0</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/minimist-1.2.2'>dependabot/npm_and_yarn/devel/electron4/files/minimist-1.2.2</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2' selected='selected'>dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2</option>
<option value='main'>main</option>
<option value='master'>master</option>
<option value='svn_head'>svn_head</option>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>FreeBSD Ports (https://github.com/freebsd/freebsd-ports)</td><td class='sub right'></td></tr></table>
<table class='tabs'><tr><td>
<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/about/?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2'>about</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2'>summary</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/refs/?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=efe92503d32d3d4feef6f152eb5ef8428f04ba5f'>refs</a><a class='active' href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2'>log</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/tree/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=efe92503d32d3d4feef6f152eb5ef8428f04ba5f'>tree</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=efe92503d32d3d4feef6f152eb5ef8428f04ba5f'>commit</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/diff/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=efe92503d32d3d4feef6f152eb5ef8428f04ba5f'>diff</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/stats/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/x11/xgamma'>
<input type='hidden' name='h' value='dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2'/><input type='hidden' name='id' value='efe92503d32d3d4feef6f152eb5ef8428f04ba5f'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=efe92503d32d3d4feef6f152eb5ef8428f04ba5f'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/x11?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=efe92503d32d3d4feef6f152eb5ef8428f04ba5f'>x11</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=efe92503d32d3d4feef6f152eb5ef8428f04ba5f'>xgamma</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=efe92503d32d3d4feef6f152eb5ef8428f04ba5f&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=a8613014074dbb57e6086657f6503612d8092bfa'>Update xkbevd to 1.1.4.</a></td><td>kwm</td><td><span title='2015-10-20 21:49:18 +0800'>2015-10-20</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=31ec8b2cec6c157bee77065dc46537ec1b4385ae'>Remove indefinite articles and trailing periods from COMMENT, plus</a></td><td>olgeni</td><td><span title='2014-08-08 16:42:44 +0800'>2014-08-08</span></td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=30e10e1d3c1d2aba115571f87754d3a229fa9391'>Switch FreeBSD CURRENT to use the new xorg stack (WITH_NEW_XORG=) [0]</a></td><td>zeising</td><td><span title='2013-12-16 19:11:09 +0800'>2013-12-16</span></td><td>1</td><td><span class='deletions'>-4</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/x11/xgamma?h=dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2&amp;id=75c703a072d2981400dc17a23ec971f3cf28a570'>Add NO_STAGE all over the place in preparation for the staging support (cat: ...</a></td><td>bapt</td><td><span title='2013-09-21 07:43:19 +0800'>2013-09-21</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+1</span></td></tr>