package service

// ESC/POS commands for 58mm thermal printer (EMD807 compatible)

const (
	ESC = 0x1B
	GS  = 0x1D
)

var (
	InitCmd       = []byte{ESC, '@'}        // 初始化打印机
	AlignLeftCmd  = []byte{ESC, 'a', 0}     // 左对齐
	AlignCenterCmd = []byte{ESC, 'a', 1}    // 居中对齐
	CutPaperCmd   = []byte{GS, 'V', 66, 0}  // 切纸
	DoubleHeightCmd = []byte{ESC, '!', 0x11} // 双倍高度
	NormalCmd     = []byte{ESC, '!', 0x00}   // 正常大小
	BoldOnCmd     = []byte{ESC, 'E', 1}      // 加粗
	BoldOffCmd    = []byte{ESC, 'E', 0}      // 取消加粗
)

// ESCPOS_Text 打印一行文字（自动加换行符）
func ESCPOS_Text(text string) []byte {
	return append([]byte(text), '\n')
}

// ESCPOS_QRCode 打印二维码（GS ( k 指令，兼容 EMD807）
func ESCPOS_QRCode(data string) []byte {
	// GS ( k pL pH cn fn [params]
	// cn=49 (QR Code), fn=65 (model), fn=67 (size), fn=80 (store), fn=81 (print)

	pL := byte(len(data) + 3)
	pH := byte((len(data) + 3) >> 8)

	cmd := []byte{GS, '('} // 模型选择

	cmd = append(cmd, 0x04, 0x00, 0x31, 0x41, 0x32, 0x00) // QR Model 2
	cmd = append(cmd, 0x03, 0x00, 0x31, 0x43, 0x08)         // Cell size 8

	// Store data
	cmd = append(cmd, GS, '(')
	cmd = append(cmd, pL, pH, 0x31, 0x50, 0x30) // fn=80 store
	cmd = append(cmd, []byte(data)...)

	// Print QR
	cmd = append(cmd, GS, '(')
	cmd = append(cmd, 0x03, 0x00, 0x31, 0x51, 0x30) // fn=81 print

	return cmd
}
