"use strict";

var sprintf  = require('../string/format');
var repeat   = require('mout/string/repeat');
var truncate = require('nyks/string/truncate');

var LF = "\n";
var COLS       = 76;
var STR_PAD_LEFT  = "left";
var STR_PAD_RIGHT = "right";
var STR_PAD_BOTH  = "both";



var pad = function(str, pad, mode, mask, pad_len){
  if(!pad_len) pad_len = COLS;
  if(!mask)    mask    = '%s';
  if(!pad)     pad     = '─';
  if(!mode)    mode    = STR_PAD_BOTH;

  pad_len -= sprintf(mask, str).length;

  var left = (mode == STR_PAD_BOTH ) ? Math.floor(pad_len/2) : 0;

  return sprintf(mask,
      repeat(pad, Math.max(left, 0)) + str + repeat(pad, Math.max(pad_len - left, 0)));
}



var box = function(/*[title, msg]*/) {
  var args = [].slice.call(arguments);
  var resp = "";

  if(!args.length)
    return resp;

  var dotrim = true, pad_len = COLS;

  for(var msg, a=1 ; a < args.length ; a+=2) {
    msg = args[a];
    if(typeof msg !== "string")
        msg = JSON.stringify(msg, null, 2);
    
    msg = msg.trim().replace("	", "    ").split(/\r?\n/);//use 4 tab indent

    msg.forEach(function(tmp_line, i) {
      if(dotrim)
        msg[i] = truncate(tmp_line,  COLS);
      //msg[i] = preg_replace('#&[^;]*?#m','…',);
      pad_len = Math.max(pad_len, msg[i].length + 2); //2 chars enclosure
    })
    args[a] = msg;
  }


  for(var a=0; a < args.length; a+=2) {
    resp += pad(" "+args[a]+" ", "═", STR_PAD_BOTH, a ? "╠%s╣":"╔%s╗", pad_len) + LF;
    args[a+1].forEach(function(line) {
      resp += pad(line, " ", STR_PAD_RIGHT, "║%s║", pad_len) + LF;
    });
  }
  resp += pad('', "═", STR_PAD_BOTH, "╚%s╝", pad_len) + LF;

  return resp;
}



module.exports = box;

@@ -161,15 +161,19 @@
 var 
+CR
 LF = %22%5C
+r%5C
 n%22;%0D
@@ -1505,32 +1505,34 @@
 s%E2%95%97%22, pad_len) + 
+CR
 LF;%0D%0A    args%5Ba+
@@ -1619,24 +1619,26 @@
  pad_len) + 
+CR
 LF;%0D%0A    %7D);
@@ -1700,16 +1700,18 @@
 _len) + 
+CR
 LF;%0D%0A%0D%0A 

Use CRLF as box LF