export enum ESerialPortParity {
  None = 0,
  Odd,
  Even,
}

export const kSerialPortParityMap = new Map<ESerialPortParity, string>([
  [ESerialPortParity.None, "ESerialPortParity::None"],
  [ESerialPortParity.Odd, "ESerialPortParity::Odd"],
  [ESerialPortParity.Even, "ESerialPortParity::Even"],
]);

export class ESerialPortParityHelper {
  static toString(e: ESerialPortParity) {
    if (kSerialPortParityMap.has(e)) {
      return kSerialPortParityMap.get(e);
    }
    return "ESerialPortParity::None";
  }

  static fromString(str: string) {
    let ret = ESerialPortParity.None;
    for (const item of kSerialPortParityMap.entries()) {
      if (item && item[1] == str) {
        ret = item[0];
        break;
      }
    }
    return ret;
  }
}

export enum ESerialPortStopBits {
  One = 0,
  OnePointFive,
  Two,
}

export const kSerialPortStopBitsMap = new Map<ESerialPortStopBits, string>([
  [ESerialPortStopBits.One, "ESerialPortStopBits::One"],
  [ESerialPortStopBits.OnePointFive, "ESerialPortStopBits::OnePointFive"],
  [ESerialPortStopBits.Two, "ESerialPortStopBits::Two"],
]);

export class ESerialPortStopBitsHelper {
  static toString(e: ESerialPortStopBits) {
    if (kSerialPortStopBitsMap.has(e)) {
      return kSerialPortStopBitsMap.get(e);
    }
    return "ESerialPortStopBits::One";
  }

  static fromString(str: string) {
    let ret = ESerialPortStopBits.One;
    for (const item of kSerialPortStopBitsMap.entries()) {
      if (item && item[1] == str) {
        ret = item[0];
        break;
      }
    }
    return ret;
  }
}

export enum ESerialPortFlowControl {
  None = 0,
  Software,
  Hardware,
}

export const kSerialPortFlowControlMap = new Map<
  ESerialPortFlowControl,
  string
>([
  [ESerialPortFlowControl.None, "ESerialPortFlowControl::None"],
  [ESerialPortFlowControl.Software, "ESerialPortFlowControl::Software"],
  [ESerialPortFlowControl.Hardware, "ESerialPortFlowControl::Hardware"],
]);

export class ESerialPortFlowControlHelper {
  static toString(e: ESerialPortFlowControl) {
    if (kSerialPortFlowControlMap.has(e)) {
      return kSerialPortFlowControlMap.get(e);
    }
    return "ESerialPortStopBits::One";
  }

  static fromString(str: string) {
    let ret = ESerialPortFlowControl.None;
    for (const item of kSerialPortFlowControlMap.entries()) {
      if (item && item[1] == str) {
        ret = item[0];
        break;
      }
    }
    return ret;
  }
}

export class SerialPortConfigEntity {
  baud_rate = 9600;
  character_size = 8;
  parity: ESerialPortParity = ESerialPortParity.None;
  stop_bits: ESerialPortStopBits = ESerialPortStopBits.One;
  flow_control: ESerialPortFlowControl = ESerialPortFlowControl.None;
}