<commit_before>import { h } from 'snabbdom'
import { Hooks } from 'snabbdom/hooks'
import { VNode } from 'snabbdom/vnode'
import AnalyseController from '../../ctrl';
import { bind } from '../../util';
import { MaybeVNodes } from '../../interfaces';
import { throttle } from 'common';
import { path as treePath } from 'tree';

export default function(ctrl: AnalyseController): VNode {

  const study = ctrl.study!,
  isMyMove = ctrl.turnColor() === ctrl.data.orientation,
  isCommented = !!(ctrl.node.comments || []).find(c => c.text.length > 2),
  hasVariation = ctrl.tree.nodeAtPath(treePath.init(ctrl.path)).children.length > 1;

  let content: MaybeVNodes;

  function commentButton(text: string = 'comment') {
    return h('a.button.thin', {
      hook: bind('click', () => {
        study.commentForm.open(study.vm.chapterId, ctrl.path, ctrl.node);
      }, ctrl.redraw),
    }, text);
  }

  if (!ctrl.path) content = [
    h('div.legend.todo', { class: { done: isCommented } }, [
      'Help the player find the initial move, with a ',
      commentButton(),
      '.'
    ])
  ];
  else if (ctrl.onMainline) {
    if (isMyMove) content = [
      h('div.legend.todo', { class: { done: isCommented } }, [
        'Comment the opponent move, and help the player find the next move, with a ',
        commentButton(),
        '.'
      ])
    ];
    else content = [
      h('div.legend.todo', { class: { done: isCommented } }, [
        'Congratulate the player for this correct move, with a ',
        commentButton(),
        '.'
      ]),
      hasVariation ? null : h('div.legend', {
        attrs: { 'data-icon': '' }
      }, 'Add variation moves to explain why specific other moves are wrong.'),
      renderDeviation(ctrl),
      renderHint(ctrl)
    ];
  }
  else content = [
    h('div.legend.todo', { class: { done: isCommented } }, [
      'Explain why this move is wrong in a ',
      commentButton(),
      '.'
    ]),
    h('div.legend',
      'Or promote it as the mainline if it is the right move.')
  ];

  return h('div.gamebook', {
    hook: {
      insert: _ => window.lichess.loadCss('/assets/stylesheets/gamebook.editor.css')
    }
  }, [
    h('div.editor', content)
  ]);
}

function renderDeviation(ctrl: AnalyseController): VNode {
  const field = 'deviation';
  return h('div.deviation.todo', { class: { done: nodeGamebookValue(ctrl.node, field).length > 2 } }, [
    h('label', {
      attrs: { for: 'gamebook-deviation' }
    }, 'Or, when any other move is played:'),
    h('textarea#gamebook-deviation', {
      attrs: { placeholder: 'Explain why all other moves are wrong' },
      hook: textareaHook(ctrl, field)
    })
  ]);
}

function renderHint(ctrl: AnalyseController): VNode {
  const field = 'hint';
  return h('div.hint', [
    h('label', {<commit_msg>tweak gamebook editor<commit_after>import { h } from 'snabbdom'
import { Hooks } from 'snabbdom/hooks'
import { VNode } from 'snabbdom/vnode'
import AnalyseController from '../../ctrl';
import { bind } from '../../util';
import { MaybeVNodes } from '../../interfaces';
import { throttle } from 'common';
import { path as treePath } from 'tree';

export default function(ctrl: AnalyseController): VNode {

  const study = ctrl.study!,
  isMyMove = ctrl.turnColor() === ctrl.data.orientation,
  isCommented = !!(ctrl.node.comments || []).find(c => c.text.length > 2),
  hasVariation = ctrl.tree.nodeAtPath(treePath.init(ctrl.path)).children.length > 1;

  let content: MaybeVNodes;

  function commentButton(text: string = 'comment') {
    return h('a.button.thin', {
      hook: bind('click', () => {
        study.commentForm.open(study.vm.chapterId, ctrl.path, ctrl.node);
      }, ctrl.redraw),
    }, text);
  }

  if (!ctrl.path) content = [
    h('div.legend.todo', { class: { done: isCommented } }, [
      'Help the player find the initial move, with a ',
      commentButton(),
      '.'
    ])
  ];
  else if (ctrl.onMainline) {
    if (isMyMove) content = [
      h('div.legend.todo', { class: { done: isCommented } }, [
        'Comment the opponent move, and help the player find the next move, with a ',
        commentButton(),
        '.'
      ])
    ];
    else content = [
      h('div.legend', [
        'Reflect on the player\'s correct move, with a ',
        commentButton(),
        '; or leave empty to jump immediately to the next move.'
      ]),
      hasVariation ? null : h('div.legend', {
        attrs: { 'data-icon': '' }
      }, 'Add variation moves to explain why specific other moves are wrong.'),
      renderDeviation(ctrl),
      renderHint(ctrl)
    ];
  }
  else content = [
    h('div.legend.todo', { class: { done: isCommented } }, [
      'Explain why this move is wrong in a ',
      commentButton(),
      '.'
    ]),
    h('div.legend',
      'Or promote it as the mainline if it is the right move.')
  ];

  return h('div.gamebook', {
    hook: {
      insert: _ => window.lichess.loadCss('/assets/stylesheets/gamebook.editor.css')
    }
  }, [
    h('div.editor', content)
  ]);
}

function renderDeviation(ctrl: AnalyseController): VNode {
  const field = 'deviation';
  return h('div.deviation.todo', { class: { done: nodeGamebookValue(ctrl.node, field).length > 2 } }, [
    h('label', {
      attrs: { for: 'gamebook-deviation' }
    }, 'When any other wrong move is played:'),
    h('textarea#gamebook-deviation', {
      attrs: { placeholder: 'Explain why all other moves are wrong' },
      hook: textareaHook(ctrl, field)
    })
  ]);
}

function renderHint(ctrl: AnalyseController): VNode {
  const field = 'hint';
  return h('div.hint', [
    h('label', {
{'fraction_non_alphanumeric': 0.16209476309226933, 'fraction_numerical': 0.0010687566797292483, 'mean_word_length': 2.7862440997977074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6712435', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1785, 'n_words': 466}