'use strict';

// var binding = require('node-cmake')('tessocr');
var path = require('path');
var binary = require('node-pre-gyp');
var binding_path = binary.find(path.resolve(path.join(__dirname, '../package.json')));
var binding = require(binding_path);

var os = require('os');
var async = require('async');
var Jimp = require("jimp");

var utils = require('./utils');

var TESSDATA = utils.findExists([
  '/usr/local/share/tessdata',
  '/usr/share/tesseract-ocr/tessdata'
]);

var DEFAULT_OPTIONS = {
  lang: 'eng',
  psm: 3,
  tessdata: TESSDATA
};

/**
 *
 * @returns {Tess}
 * @constructor
 */
function Tess() {
  if (!(this instanceof Tess)) {
    return new Tess();
  }
}

utils.merge(Tess, binding);

Tess.prototype.segment = function (image, options, cb) {
  if (typeof options === 'function') {
    cb = options;
    options = null;
  } else if (typeof options === 'number') {
    options = {threshold: options}
  }
  options = utils.merge({
    threshold: 10
  }, options);

  Jimp.read(image, function (err, image) {
    var result = {
      dimensions: [image.bitmap.width, image.bitmap.height],
      boxes: []
    };
    var boxes = [];
    var line = 0;
    var blank = true;

    image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, idx) {
      var dot = isDot(this.bitmap.data, idx);
      if (y === line) {
        if (blank && dot) {

        }
      }
    });
  });

  function isDot(data, idx) {
    return (data[ idx + 0 ] < 0x7F || data[ idx + 1 ] < 0x7F || data[ idx + 2 ] < 0x7F) && data[ idx + 3 ] > 0;
  }
};

/**
 *
 * @param image
 * @param {Object|Number|Boolean} [options]
 * @param {String} [options.language] Language
 * @param {String} [options.lang] Language
 * @param {String} [options.l] Language
 * @param {String} [options.tessdata] tessdata path
 * @param {Number} [options.psm] page segment mode
 * @param {Array} [options.rects] Multi image rects
 * @param {Number} [options.level]
 *  0: RIL_BLOCK,     // Block of text/image/separator line.
 *  1: RIL_PARA,      // Paragraph within a block.
 *  2: RIL_TEXTLINE,  // Line within a paragraph.
 *  3: RIL_WORD,      // Word within a textline.
 *  4: RIL_SYMBOL     // Symbol/character within a word.
 * @param {Boolean} [options.textOnly]
 * @param cb function (err, result)
 *
 * Example for result
 * {
 *   dimensions: [ 1486, 668 ],   // image width and height
 *   boxes: [
 *     { x: 38, y: 0, w: 1448, h: 114, confidence: 77 },
 *     { x: 99, y: 108, w: 1387, h: 156, confidence: 77 },
 *     { x: 34, y: 185, w: 1437, h: 88, confidence: 77 },
 *     { x: 101, y: 256, w: 1356, h: 81, confidence: 77 },
 *     { x: 31, y: 336, w: 1395, h: 74, confidence: 77 },
 *     { x: 99, y: 412, w: 1337, h: 83, confidence: 77 },
 *     { x: 28, y: 487, w: 1417, h: 82, confidence: 77 },
 *     { x: 96, y: 563, w: 1306, h: 84, confidence: 77 }
 *   ]
 * }
 */
Tess.prototype.tokenize = function (image, options, cb) {
  if (typeof options === 'function') {
    cb = options;
    options = cb;
  }
  if (typeof options === 'number') {
    options = {level: options};
  } else if (typeof options === 'boolean') {
    options = {textOnly: true};
  }

  options = parseOptions(options);
  cb = cb || utils.noop;

  return binding.tokenize(image, options, cb);
};

/**
 *
 * @param {Object|String} image The image data or image filename
 * @param {Object|Function} [options] Options
 * @param {String} [options.language] Language
 * @param {String} [options.lang] Language
 * @param {String} [options.l] Language
 * @param {String} [options.tessdata] tessdata path
 * @param {Number} [options.psm] page segment mode
 * @param {Array} [options.rects] Multi image rects
 * @param {Function} [cb] Callback
 */
Tess.prototype.recognize = function (image, options, cb) {
  if (typeof options === 'function') {
    cb = options;
    options = cb;
  }
  options = parseOptions(options);
  cb = cb || utils.noop;

  return binding.recognize(image, options, cb);
};

/**
 *
 * @param {Object|String} image The image data or image filename
 * @param {Object|Function} [options] Options
 * @param {String} [options.language] Language
 * @param {String} [options.lang] Language
 * @param {String} [options.l] Language
 * @param {String} [options.tessdata] tessdata path
 * @param {Number} [options.psm] page segment mode
 * @param {Array} [options.rects] Multi image rects
 * @param {Number} [options.jobs] Jobs to run recognize. It is work only when has rects
 * @param {Function} [cb] Callback
 */
Tess.prototype.ocr = function (image, options, cb) {
  if (typeof options === 'function') {
    cb = options;
    options = cb;
  }
  options = parseOptions(options);
  cb = cb || utils.noop;

  var rects = options.rects;
  if (!Array.isArray(rects)) {
    return this.recognize(image, options, cb);
  }

  var jobs = utils.arrange(rects, options.jobs || os.cpus());

  var that = this;
  async.map(jobs, function (job, cb) {
    options.rects = jobTransform(job);
    that.recognize(image, options, cb);
  }, cb);
};

function parseOptions(options) {
  options = options || {};
  options.language = options.lang || options.l || options.language;
  if (options.rect && !options.rects) {
    options.rects = [options.rect];
  }
  return utils.merge({}, DEFAULT_OPTIONS, options);
}

function jobTransform(job) {
  return job.map(function (rect) {
    if (Array.isArray(rect)) return rect;
    if (rect && typeof rect === 'object') {
      return [rect.x, rect.y, rect.w, rect.h];
    }
  })
}

exports.Tess = exports.tess = Tess;


@@ -5071,16 +5071,46 @@
  %7C%7C %7B%7D;%0A
+  if (!options.language ) %7B%0A  
   option
@@ -5151,29 +5151,13 @@
 ns.l
- %7C%7C options.language;
+;%0A  %7D
 %0A  i

Change lang to language in options