var editor = require('./editor');
var m = require('mithril');
var keyboard = require('./keyboard');

module.exports = function(cfg) {

  this.cfg = cfg;
  this.data = editor.init(cfg);
  this.options = cfg.options || {};
  this.embed = cfg.embed;

  this.trans = lichess.partial(editor.trans, this.data.i18n);

  this.vm = {
    redirecting: false,
    selected: m.prop('pointer')
  };

  this.extraPositions = [{
    fen: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -',
    name: this.trans('startPosition')
  }, {
    fen: '8/8/8/8/8/8/8/8 w - -',
    name: this.trans('clearBoard')
  }, {
    fen: 'prompt',
    name: this.trans('loadPosition')
  }];

  this.positionIndex = {};
  cfg.positions && cfg.positions.forEach(function(p, i) {
    this.positionIndex[p.fen.split(' ')[0]] = i;
  }.bind(this));

  this.chessground; // will be set from the view when instanciating chessground

  this.onChange = function() {
    this.options.onChange && this.options.onChange(this.computeFen());
  }.bind(this);

  this.computeFen = function() {
    return this.chessground ?
    editor.computeFen(this.data, this.chessground.getFen()) :
    cfg.fen;
  }.bind(this);

  this.bottomColor = function() {
    return this.chessground ?
    this.chessground.state.orientation :
    this.options.orientation || 'white';
  }.bind(this);

  this.setColor = function(letter) {
    this.data.color(letter);
    onChange();
  }.bind(this);

  this.setCastle = function(id, value) {
    this.data.castles[id](value);
    onChange();
  }.bind(this);

  this.startPosition = function() {
    this.chessground.set({
      fen: 'start'
    });
    this.data.castles = editor.castlesAt(true);
    this.data.color('w');
    onChange();
  }.bind(this);

  this.clearBoard = function() {
    this.chessground.set({
      fen: '8/8/8/8/8/8/8/8'
    });
    this.data.castles = editor.castlesAt(false);
    onChange();
  }.bind(this);

  this.loadNewFen = function(fen) {
    if (fen === 'prompt') {
      fen = prompt('Paste FEN position').trim();
      if (!fen) return;
    }
    this.changeFen(fen);
  }.bind(this);

  this.changeFen = function(fen) {
    this.vm.redirecting = true;
    window.location = editor.makeUrl(this.data.baseUrl, fen);
  }.bind(this);

  this.positionLooksLegit = function() {
    if (!this.chessground) return false;
    var kings = {
      white: 0,
      black: 0
    };
    var pieces = this.chessground.state.pieces;
    for (var pos in pieces) {
      if (pieces[pos] && pieces[pos].role === 'king') kings[pieces[pos].color]++;
    }
    return kings.white === 1 && kings.black === 1;
  }.bind(this);

  this.setOrientation = function(o) {
    this.options.orientation = o;
    if (this.chessground.state.orientation !== o)
    this.chessground.toggleOrientation();
    m.redraw();
  }.bind(this);

  keyboard(this);
};

@@ -991,16 +991,32 @@
 Fen());%0A
+    m.redraw();%0A
   %7D.bind
@@ -1015,32 +1015,32 @@
   %7D.bind(this);%0A
-
 %0A  this.computeF
@@ -1408,24 +1408,29 @@
 etter);%0A    
+this.
 onChange();%0A
@@ -1517,32 +1517,37 @@
 id%5D(value);%0A    
+this.
 onChange();%0A  %7D.
@@ -1723,24 +1723,29 @@
 r('w');%0A    
+this.
 onChange();%0A
@@ -1907,20 +1907,25 @@
 false);%0A
-
     
+this.
 onChange

fix editor onChange callbacks