import gameStatus from './status';
import { secondsToMinutes } from '../utils';
import settings from '../settings';
import getVariant from './variant';
import i18n from '../i18n';

function parsePossibleMoves(possibleMoves) {
  if (!possibleMoves) return {};
  var r = {};
  var keys = Object.keys(possibleMoves);
  for (var i = 0, ilen = keys.length; i < ilen; i++) {
    var m = possibleMoves[keys[i]];
    var a = [];
    for (var j = 0, jlen = m.length; j < jlen; j += 2) {
      a.push(m.substr(j, 2));
    }
    r[keys[i]] = a;
  }
  return r;
}

function playable(data) {
  return data.game.status.id < gameStatus.ids.aborted;
}

function isPlayerPlaying(data) {
  return playable(data) && !data.player.spectator;
}

function isPlayerTurn(data) {
  return isPlayerPlaying(data) && data.game.player === data.player.color;
}

function isOpponentTurn(data) {
  return isPlayerPlaying(data) && data.game.player !== data.player.color;
}

function mandatory(data) {
  return !!data.tournament;
}

function playedTurns(data) {
  return data.game.turns - data.game.startedAtTurn;
}

function abortable(data) {
  return playable(data) && playedTurns(data) < 2 && !mandatory(data);
}

function takebackable(data) {
  return playable(data) && data.takebackable && !data.tournament && playedTurns(data) > 1 && !data.player.proposingTakeback && !data.opponent.proposingTakeback;
}

function drawable(data) {
  return playable(data) && data.game.turns >= 2 && !data.player.offeringDraw && !data.opponent.ai && !data.opponent.offeringDraw;
}

function resignable(data) {
  return playable(data) && !abortable(data);
}

function forceResignable(data) {
  return !data.opponent.ai && data.clock && data.opponent.isGone && resignable(data);
}

function moretimeable(data) {
  return data.clock && isPlayerPlaying(data) && !mandatory(data);
}

function replayable(data) {
  return data.game.variant.key === 'standard' && gameStatus.finished(data);
}

function userAnalysable(data) {
  return settings.analyse.supportedVariants.indexOf(data.game.variant.key) !== -1 && playable(data) && (!data.clock || !isPlayerPlaying(data));
}

function getPlayer(data, color) {
  if (data.player.color === color) return data.player;
  if (data.opponent.color === color) return data.opponent;
  return null;
}

function setIsGone(data, color, isGone) {
  var player = getPlayer(data, color);
  isGone = isGone && !player.ai;
  player.isGone = isGone;
  if (!isGone && player.user) player.user.online = true;
}

function setOnGame(data, color, onGame) {
  var player = getPlayer(data, color);
  onGame = onGame || player.ai;
  player.onGame = onGame;
  if (onGame) setIsGone(data, color, false);
}

function nbMoves(data, color) {
  return Math.floor((data.game.turns + (color === 'white' ? 1 : 0)) / 2);
}

function result(data) {
  if (gameStatus.finished(data)) switch (data.game.winner) {
    case 'white':
      return '1-0';
    case 'black':
      return '0-1';
    default:
      return '½-½';
  }
  return '*';
}

function time(data) {
  if (data.clock) {
    const min = secondsToMinutes(data.clock.initial);
    const t = min === 0.5 ? '½' : min === 0.75 ? '¾' : min.toString();
    return t + '+' + data.clock.increment;
  }
  else if (data.correspondence) {
    return i18n('nbDays', data.correspondence.daysPerTurn);
  }
  else {
    return '∞';
  }
}

function title(data) {
  const mode = data.game.offline ? i18n('offline') :
    data.game.rated ? i18n('rated') : i18n('casual');
  const variant = getVariant(data.game.variant.key);
  const name = variant ? (variant.shortName || variant.name) : '?';
  return `${time(data)} • ${name} • ${mode}`;
}

function publicUrl(data) {
  return 'http://lichess.org/' + data.game.id;
}

function isSupportedVariant(data) {
  return settings.game.supportedVariants.indexOf(data.game.variant.key) !== -1;
}

export default {
  isPlayerPlaying,
  isPlayerTurn,
  isOpponentTurn,
  playable,
  abortable,
  takebackable,
  drawable,
  resignable,
  forceResignable,
  moretimeable,
  mandatory,
  replayable,
  userAnalysable,
  getPlayer,
  parsePossibleMoves,
  nbMoves,
  setOnGame,
  setIsGone,
  result,
  time,
  title,
  publicUrl,
  isSupportedVariant
};

@@ -1834,26 +1834,24 @@
 unction 
-replayable
+imported
 (data) %7B
@@ -1874,37 +1874,86 @@
 ame.
-variant.key === 'standard' &&
+source === 'import';%0A%7D%0A%0Afunction replayable(data) %7B%0A  return imported(data) %7C%7C
  gam

Allow analysis for all variants that app support