/* uglified */
var iconv=require("iconv-lite"),cmds=require("./commands"),node_printer=require("printer"),BufferHelper=require("bufferhelper"),printer=function(e,t){e||(e=node_printer.getDefaultPrinterName()),this.printer=e,this._queue=new BufferHelper,this._writeCmd("INITIAL_PRINTER"),this._writeCmd("CHN_TEXT"),t&&t.call(this,null,"Get printer success")};printer.prototype={text:function(e,t){return e&&(this._queue.concat(iconv.encode(e,"GBK")),t||this._writeCmd("NEW_LINE")),this},line:function(e){e=e||1;for(var t=0;t<e;t++)this._writeCmd("NEW_LINE");return this},setAlign:function(e){return this._writeCmd("TXT_ALIGN_"+e.toUpperCase()),this},setFont:function(e){return this._writeCmd("TXT_FONT_"+e.toUpperCase()),this},setLineheight:function(e){return this._writeCmd("LINE_HEIGHT"),e&&this._queue.concat(new Buffer("3"+e)),this},setStyle:function(e){switch(e.toUpperCase()){case"B":this._writeCmd("TXT_UNDERL_OFF"),this._writeCmd("TXT_BOLD_ON");break;case"U":this._writeCmd("TXT_BOLD_OFF"),this._writeCmd("TXT_UNDERL_ON");break;case"U2":this._writeCmd("TXT_BOLD_OFF"),this._writeCmd("TXT_UNDERL2_ON");break;case"BU":this._writeCmd("TXT_BOLD_ON"),this._writeCmd("TXT_UNDERL_ON");break;case"BU2":this._writeCmd("TXT_BOLD_ON"),this._writeCmd("TXT_UNDERL2_ON");break;case"NORMAL":default:this._writeCmd("TXT_BOLD_OFF"),this._writeCmd("TXT_UNDERL_OFF")}return this},setSize:function(e){switch(this._writeCmd("TXT_NORMAL"),this._writeCmd("LINE_HEIGHT"),parseInt(e)){case 1:this._queue.concat(new Buffer(cmds.TXT_SIZE_1));break;case 2:this._queue.concat(new Buffer(cmds.TXT_SIZE_2))}return this},barcode:function(e,t,r,i,n,s){return(r>=1||r<=255)&&this._writeCmd("BARCODE_WIDTH"),(i>=2||i<=6)&&this._writeCmd("BARCODE_HEIGHT"),this._writeCmd("BARCODE_FONT_"+(s||"A").toUpperCase()),this._writeCmd("BARCODE_TXT_"+(n||"BLW").toUpperCase()),this._writeCmd("BARCODE_"+(t||"EAN13").replace("-","_").toUpperCase()),this._queue.concat(new Buffer(e)),this},qrcode:function(e,t,r,i){return t=t||"",/^[\w\:\/\.\?\&\=]+$/.test(e)?(this._writeCmd("QRCODE_SIZE_MODAL"),this._queue.concat(new Buffer(cmds.QRCODE_SIZE+t)),this._writeCmd("QRCODE_ERROR"),this._queue.concat(new Buffer(cmds.QRCODE_AREA_LSB+r+i+cmds.QRCODE_AREA_MSB)),this._queue.concat(new Buffer(e)),this._writeCmd("QRCODE_PRINT"),this):(this.text("二维码请使用英文和数字打印"),this)},beep:function(e,t){return e=e||"\t",t=t||"",this._queue.concat(new Buffer(cmds.BEEP+e+t)),this},openCashbox:function(e){return this._writeCmd("CASHBOX_OPEN"),this.sendCmd(e),this},compile:function(e){if("string"!=typeof e)return console.log("必须为字符串"),this;var t=this,r=e.replace(/[\n\r]+/g,"/n").replace(/<%([\s\S]+?)%>/g,function(e,r){return'",true);\n'+t._renderFunc(r)+'\nobj.text("'}).replace(/\/n/g,'");\nobj.text("');return r='obj.text("'+r+'")',new Function("obj",r).call(this,this),this},_writeCmd:function(e){cmds[e]&&this._queue.concat(new Buffer(cmds[e]))},_renderFunc:function(e){var t=this,r=!0,i=(e=e.trim()).replace(/^([\S]+?):/,function(e,i){var n=t._escape(i);return t[n]||(r=!1,console.log("解析模板出错没有名为"+n+"的方法")),"obj."+n+":"}).replace(/:([\S]+?)$/,function(e,r){for(var i=t._escape(r).split(","),n="",s=i.length,_=0;_<s;_++){var u=i[_];/%u/.test(i[_])&&(u=u.replace(/%u/g,"u")),n+='"'+u+'",'}return"("+(n=n.replace(/\,$/,""))+");"});return r?i:""},_escape:function(e){return e=(e=unescape(e.replace(/\u/g,"%u"))).replace(/[\<\>\"\'\{\}]/g,"")},print:function(e){this._writeCmd("PAPER_CUTTING"),this._writeCmd("INITIAL_PRINTER"),this.sendCmd(e)},sendCmd:function(e){node_printer.printDirect({data:this._queue.toBuffer(),printer:this.printer,type:"RAW",success:()=>{"function"==typeof e&&e.call(this,null,"Print successed"),this._queue.empty()},error:t=>{"function"==typeof e&&e.call(this,t,"Print failed")}})},empty:function(){return this._queue.empty(),this}},module.exports=printer;