import Linter from 'eslint/lib/linter'
import monkeypatch from './monky'

/* eslint-disable global-require */
const allRules = {
//   'react/jsx-no-comment-textnodes': require('eslint-plugin-react/lib/rules/jsx-no-comment-textnodes'),
//   'react/jsx-no-duplicate-props': require('eslint-plugin-react/lib/rules/jsx-no-duplicate-props'),
//   'react/jsx-no-target-blank': require('eslint-plugin-react/lib/rules/jsx-no-target-blank'),
  'react/jsx-no-undef': require('eslint-plugin-react/lib/rules/jsx-no-undef'),
//   'react/jsx-pascal-case': require('eslint-plugin-react/lib/rules/jsx-pascal-case'),
  'react/jsx-uses-vars': require('eslint-plugin-react/lib/rules/jsx-uses-vars'),
//   'react/no-danger-with-children': require('eslint-plugin-react/lib/rules/no-danger-with-children'),
//   'react/no-deprecated': require('eslint-plugin-react/lib/rules/no-deprecated'),
//   'react/no-direct-mutation-state': require('eslint-plugin-react/lib/rules/no-direct-mutation-state'),
//   'react/no-is-mounted': require('eslint-plugin-react/lib/rules/no-is-mounted'),
//   'react/require-render-return': require('eslint-plugin-react/lib/rules/require-render-return'),
//   'react/style-prop-object': require('eslint-plugin-react/lib/rules/style-prop-object'),
//   'jsx-a11y/accessible-emoji': require('eslint-plugin-jsx-a11y/lib/rules/accessible-emoji'),
//   'jsx-a11y/alt-text': require('eslint-plugin-jsx-a11y/lib/rules/alt-text'),
//   'jsx-a11y/anchor-has-content': require('eslint-plugin-jsx-a11y/lib/rules/anchor-has-content'),
//   'jsx-a11y/aria-activedescendant-has-tabindex': require('eslint-plugin-jsx-a11y/lib/rules/aria-activedescendant-has-tabindex'),
//   'jsx-a11y/aria-props': require('eslint-plugin-jsx-a11y/lib/rules/aria-props'),
//   'jsx-a11y/aria-proptypes': require('eslint-plugin-jsx-a11y/lib/rules/aria-proptypes'),
//   'jsx-a11y/aria-role': require('eslint-plugin-jsx-a11y/lib/rules/aria-role'),
//   'jsx-a11y/aria-unsupported-elements': require('eslint-plugin-jsx-a11y/lib/rules/aria-unsupported-elements'),
//   'jsx-a11y/heading-has-content': require('eslint-plugin-jsx-a11y/lib/rules/heading-has-content'),
//   'jsx-a11y/href-no-hash': require('eslint-plugin-jsx-a11y/lib/rules/href-no-hash'),
//   'jsx-a11y/iframe-has-title': require('eslint-plugin-jsx-a11y/lib/rules/iframe-has-title'),
//   'jsx-a11y/img-redundant-alt': require('eslint-plugin-jsx-a11y/lib/rules/img-redundant-alt'),
//   'jsx-a11y/no-access-key': require('eslint-plugin-jsx-a11y/lib/rules/no-access-key'),
//   'jsx-a11y/no-distracting-elements': require('eslint-plugin-jsx-a11y/lib/rules/no-distracting-elements'),
//   'jsx-a11y/no-redundant-roles': require('eslint-plugin-jsx-a11y/lib/rules/no-redundant-roles'),
//   'jsx-a11y/role-has-required-aria-props': require('eslint-plugin-jsx-a11y/lib/rules/role-has-required-aria-props'),
//   'jsx-a11y/role-supports-aria-props': require('eslint-plugin-jsx-a11y/lib/rules/role-supports-aria-props'),
  // 'jsx-a11y/scope': require('eslint-plugin-jsx-a11y/lib/rules/scope'),
};

const restrictedGlobals = [
  'addEventListener',
  'blur',
  'close',
  'closed',
  'confirm',
  'defaultStatus',
  'defaultstatus',
  'event',
  'external',
  'find',
  'focus',
  'frameElement',
  'frames',
  'history',
  'innerHeight',
  'innerWidth',
  'length',
  'location',
  'locationbar',
  'menubar',
  'moveBy',
  'moveTo',
  'name',
  'onblur',
  'onerror',
  'onfocus',
  'onload',
  'onresize',
  'onunload',
  'open',
  'opener',
  'opera',
  'outerHeight',
  'outerWidth',
  'pageXOffset',
  'pageYOffset',
  'parent',
  'print',
  'removeEventListener',
  'resizeBy',
  'resizeTo',
  'screen',
  'screenLeft',
  'screenTop',
  'screenX',
  'screenY',
  'scroll',
  'scrollbars',
  'scrollBy',
  'scrollTo',
  'scrollX',
  'scrollY',
  'self',
  'status',
  'statusbar',
  'stop',
  'toolbar',
  'top',
];

const defaultConfig = {
  parserOptions: {
    ecmaVersion: 8,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
      globalReturn: false,
      impliedStrict: true,
      experimentalObjectRestSpread: true,
      modules: true,
    },
  },
  plugins: ['react'],
  parser: 'babel-eslint',
  globals: {
    React: true,
    PropTypes: true,
    classNames: true,
    _: true,
  },
  env: {
    browser: true,
    commonjs: true,
    es6: true,
    node: true,
  },
  rules: {
    'no-eval': 'warn',
    'no-undef': 'error',
    'no-unused-labels': 'warn',
    'no-unused-vars': 'warn',
    'no-redeclare': 'error',
    'no-unreachable': 'warn',
    'no-var': 'warn',
    'prefer-const': 'error',
    'no-unexpected-multiline': 'error',
    'react/jsx-uses-vars': 'warn',
    'react/jsx-no-undef': 'error',
  }
};

monkeypatch({}, defaultConfig.parserOptions);
const linter = new Linter()

linter.defineParser('babel-eslint', {
  parse: require('babel-eslint').parseNoPatch,
})

linter.defineRules(allRules);

function getPos(error, from) {
  let line = error.line - 1;
  let ch = from ? error.column : error.column + 1;

  if (ch > error.source.length) {
    ch -= 1;
  }

  if (error.node && error.node.loc) {
    line = from ? error.node.loc.start.line - 1 : error.node.loc.end.line - 1;
    ch = from ? error.node.loc.start.column : error.node.loc.end.column;
  }
  return { line: line + 1, column: ch };
}

function getSeverity(error) {
  switch (error.severity) {
    case 1:
      return 2;
    case 2:
      return 3;
    default:
      return 3;
  }
}

// Respond to message from parent thread
export default ({code}) => {

  const validations = linter.verify(code, defaultConfig);

  const markers = validations.map(error => {
    const { line: startL, column: startCol } = getPos(error, true);
    const { line: endL, column: endCol } = getPos(error, false);

    return {
      severity: getSeverity(error),
      startColumn: startCol,
      startLineNumber: startL,
      endColumn: endCol,
      endLineNumber: endL,
      message: `${error.message} (${error.ruleId})`,
      source: 'eslint',
    }
  })
  return markers
}
