import 'package:blue_thermal_printer/blue_thermal_printer.dart';

class PrinterCommands {
  static const HT = 0x9;
  static const LF = 0x0A;
  static const CR = 0x0D;
  static const ESC = 0x1B;
  static const DLE = 0x10;
  static const GS = 0x1D;
  static const FS = 0x1C;
  static const STX = 0x02;
  static const US = 0x1F;
  static const CAN = 0x18;
  static const CLR = 0x0C;
  static const EOT = 0x04;

  static const initPrinter = [ESC, 0x40];
  static const FEED_LINE = [0x0A];

  ///设置左边距
  /// left 用 nL 和 nH 设定左边空白量为(nL+ Nh*256)*0.125 毫米。
  static printMarginLeft({int left = 0}) {
    List<int> result = [];
    result.add(0x1D);
    result.add(0x4C);
    result.add(left ~/ 0.125);
    result.add(0);
    return result;
  }

  ///字体变大为标准的n倍
  static fontSizeSetBig({int size = 0}) {
    var realSize = 1;
    switch (size) {
      case 1:
        realSize = 0x00;
        break;
      case 2:
        realSize = 0x11;
        break;
      case 3:
        realSize = 0x22;
        break;
      case 4:
        realSize = 0x33;
        break;
      case 5:
        realSize = 0x44;
        break;
      case 6:
        realSize = 0x55;
        break;
      case 7:
        realSize = 0x66;
        break;
      case 8:
        realSize = 0x77;
        break;
    }
    List<int> result = [];
    result.add(0x1D);
    result.add(0x21);
    result.add(realSize);
    return result;
  }

  /// 设置汉字字符左右间距
  static setCharHSpace({int charSpace = 0}) {
    List<int> result = [];
    result.add(0x1C);
    result.add(0x53);
    result.add(charSpace);
    result.add(charSpace);
    return result;
  }

  ///是否加粗
  static bold({bool bold = false}) {
    List<int> result = [];
    if (bold) {
      result.add(ESC);
      result.add(69);
      result.add(0xF);
    } else {
      result.add(ESC);
      result.add(69);
      result.add(0);
    }
    return result;
  }

  /// 打印条形码
  /// 73 CODE128
  static createBarCode(Uint8List infoByte) {
    List<int> result = [];
    var width = setBarCodeWidth(2);
    var height = setBarCodeHeight(120);
    var bar = [0x1D, 0x6B, 73, infoByte.length];
    result.addAll(width);
    result.addAll(height);
    result.addAll(bar);
    result.addAll(infoByte);
    return result;
  }

  /// 设置条形码 宽度
  /// width 2 3 4
  static setBarCodeWidth(width) {
    List<int> result = [];
    result.add(0x1D);
    result.add(0x77);
    result.add(width);
    return result;
  }

  /// 设置条形码 高度
  /// height 0≤n≤255,默认 n=162
  static setBarCodeHeight(height) {
    List<int> result = [];
    result.add(0x1D);
    result.add(0x68);
    result.add(height);
    return result;
  }
}