using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ButcherFactory.Controls
{
  internal class JST8180DataFormat : DataFormatBase
  {
    public override int DataLength => 8;
    public override char Beginchar => (char)0x3D; // = 开头
    public override char Endchar => (char)0xFF;
    public override short Bufsize => 7;

    public override string ParseData(string buf, out bool isStatic)
    {
      // 3D 34 39 2E 36 30 30 30 
      // 6.94

      isStatic = true;
      try
      {
        // 1. 去掉 = 和 .  例：=49.6000 → 496000
        string raw = buf.TrimStart('=').Replace(".", "");

        // 2. 反转数字  496000 → 000694
        char[] arr = raw.ToCharArray();
        Array.Reverse(arr);
        string reversed = new string(arr);

        // 3. 插入小数点（固定2位小数）
        decimal weight = decimal.Parse(reversed) / 100m;

        // 4. 输出仪表显示值
        return weight.ToString("0.00");
      }
      catch
      {
        return "0.00";
      }
    }

    public override bool ParseAscii(string buf, out string weight, out bool isStatic)
    {
      weight = string.Empty;
      isStatic = false;
      string frame = FindDataFrame(buf, DataLength);
      if (string.IsNullOrEmpty(frame)) return false;

      weight = ParseData(frame, out isStatic);
      return true;
    }

    public override string FindDataFrame(string buf, int fSize)
    {
      if (buf == null || buf.Length < fSize) return "";
      int idx = buf.IndexOf(Beginchar);
      if (idx < 0 || idx + fSize > buf.Length) return "";
      return buf.Substring(idx, fSize);
    }

    public override bool ParseAscii(string buf, out string weight, out bool isStatic, out string subStr)
    {
      weight = ""; isStatic = false; subStr = ""; return false;
    }
  }

   
}
