var app = getApp();
// var encode = require("./encoding.js");
import encode from '@/unit/CHITEN_SDK_APP/encoding.js'
var dlabelPrinter = {
	createNew: function() {
		var dlabelPrinter = {};
		var data = "";
		var command = []

		dlabelPrinter.name = "蓝牙打印机";

		dlabelPrinter.init = function() {};

		dlabelPrinter.addCommand = function(content) {
			var textencode = encode._this.TextEncoder(
				'gb18030', {
					NONSTANDARD_allowLegacyEncoding: true
				});
			var code = textencode.encode(content);
			for (var i = 0; i < code.length; ++i) {
				command.push(code[i])
			}
		};
		dlabelPrinter.setSize = function(pageWidght, pageHeight) {
			data = "SIZE " + pageWidght.toString() + " mm" + "," + pageHeight.toString() + " mm" + "\r\n";
			dlabelPrinter.addCommand(data)
		};
		dlabelPrinter.setGap = function(printGap) {
			data = "GAP " + printGap.toString() + " mm,0 mm\r\n";
			dlabelPrinter.addCommand(data)
		};
		dlabelPrinter.setBline = function(printBline) {
			data = "BLINE " + printBline.toString() + " mm,0 mm\r\n";
			dlabelPrinter.addCommand(data)
		};
		dlabelPrinter.setOffset = function(offset) {
			data = "OFFSET " + offset.toString() + " mm,0 mm\r\n";
			dlabelPrinter.addCommand(data)
		};
		dlabelPrinter.setSpeed = function(printSpeed) {
			data = "SPEED " + printSpeed.toString() + "\r\n";
			dlabelPrinter.addCommand(data)
		};
		dlabelPrinter.setDensity = function(printDensity) {
			data = "DENSITY " + printDensity.toString() + "\r\n";
			dlabelPrinter.addCommand(data)
		}
		dlabelPrinter.setDirection = function(direction) {
			data = "DIRECTION " + direction + "\r\n";
			dlabelPrinter.addCommand(data)
		};
		dlabelPrinter.setReference = function(x, y) {
			data = "REFERENCE " + x + "," + y + "\r\n";
			dlabelPrinter.addCommand(data)
		};
		dlabelPrinter.setShift = function(n) {
			data = "SHIFT " + n + "\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setCountry = function(country) {
			data = "COUNTRY " + country + "\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setCodepage = function(codepage) {
			data = "CODEPAGE " + codepage + "\r\n";
			dlabelPrinter.addCommand(data)
		}
		dlabelPrinter.setCls = function() {
			data = "CLS\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setFeed = function(feed) {
			data = "FEED " + feed + "\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setBackFeed = function(backup) {
			data = "BACKFEED " + backup + "\r\n";
			dlabelPrinter.addCommand(data)
		}

		dlabelPrinter.setFromfeed = function() {
			data = "FORMFEED\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setHome = function() {
			data = "HOME\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setPagePrint = function() {
			data = "PRINT 1,1\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setPrint = function(n) {
			data = "PRINT " + n + ",1\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setSound = function(level, interval) {
			data = "SOUND " + level + "," + interval + "\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setLimitfeed = function(limit) {
			data = "LIMITFEED " + limit + "mm\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setSelfTest = function() {
			data = "SELFTEST\r\n";
			dlabelPrinter.addCommand(data)
		}

		dlabelPrinter.setBar = function(x, y, width, height) {
			data = "BAR " + x + "," + y + "," + width + "," + height + "\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setBox = function(x_start, y_start, x_end, y_end, thickness) {
			data = "BOX " + x_start + "," + y_start + "," + x_end + "," + y_end + "," + thickness + "\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setBarCode = function(x, y, codetype, height, readable, rotation, narrow, wide, content) {
			data = "BARCODE " + x + "," + y + ",\"" + codetype + "\"," + height + "," + readable + "," +
				rotation + "," + narrow + "," + wide + ",\"" + content + "\"\r\n"
			dlabelPrinter.addCommand(data)
		};


		dlabelPrinter.setBitmap = function(x, y, mode, res) {
			console.log(res)
			var w = res.width;
			var h = res.height;
			var bitw = parseInt((w + 7) / 8) * 8;

			var pitch = parseInt(bitw / 8);
			var bits = new Uint8Array(h * pitch);
			console.log("w=" + w + ", h=" + h + ", bitw=" + bitw + ", pitch=" + pitch + ", bits=" + bits
				.length);
			var cmd = "BITMAP " + x + "," + y + "," + pitch + "," + h + "," + mode + ",";
			console.log("add cmd: " + cmd);
			dlabelPrinter.addCommand(cmd);

			for (y = 0; y < h; y++) {
				for (x = 0; x < w; x++) {
					var color = res.data[(y * w + x) * 4 + 1];
					if (color <= 128) {
						bits[parseInt(y * pitch + x / 8)] |= (0x80 >> (x % 8));
					}
				}
			}
			for (var i = 0; i < bits.length; i++) {
				command.push((~bits[i]) & 0xFF);
			}
			console.log(command);
		};


		dlabelPrinter.setErase = function(x_start, y_start, x_width, y_height) {
			data = "ERASE " + x_start + "," + y_start + "," + x_width + "," + y_height + "\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setReverse = function(x_start, y_start, x_width, y_height) {
			data = "REVERSE " + x_start + "," + y_start + "," + x_width + "," + y_height + "\r\n";
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setText = function(x, y, font, rotation, x_, y_, str) {
			data = "TEXT " + x + "," + y + ",\"" + font + "\"," + rotation + "," + x_ + "," + y_ + "," +
				"\"" + str + "\"\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setQrcode = function(x, y, level, width, mode, content) {
			data = "QRCODE " + x + "," + y + "," + level + "," + width + "," + mode + "," + 0 + ",\"" +
				content + "\"\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setKey1 = function(n) {
			data = "SET KYE1 " + n.toString + "\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setKey2 = function(n) {
			data = "SET KYE2 " + n.toString + "\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setPeel = function(n) {
			data = "SET PEEL " + n.toString + "\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setTear = function(n) {
			data = "SET TEAR " + n.toString + "\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setStripper = function(n) {
			data = "SET STRIPPER " + n.toString + "\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setHead = function(n) {
			data = "SET HEAD " + n.toString + "\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setReprint = function(n) {
			data = "SET REPRINT " + n.toString + "\r\n"
			dlabelPrinter.addCommand(data)
		};


		dlabelPrinter.setRibbon = function(n) {
			data = "SET RIBBON " + n.toString + "\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.setCut = function(n) {
			data = "SET CUTTER " + n.toString + "\r\n"
			dlabelPrinter.addCommand(data)
		};

		dlabelPrinter.getData = function() {
			return command;
		};
		return dlabelPrinter;
	}
};

export default {
	dlabelPrinter
}

// module.exports.dlabelPrinter = dlabelPrinter;