class Ecu::Festwerteblock

def bytesize

def bytesize
  xdim * ydim * BYTESIZE[:number]
end

def equality_properties

def equality_properties
  [:name, :value]
end

def initialize(name:,

def initialize(name:,
               xdim:,
               ydim: 1,
               value:,
               unit: nil,
               function: nil,
               description: nil)
  value        = reshape_if_needed(value, xdim, ydim)
  ydim         = ydim == 0 ? 1 : ydim
  @name        = name
  @xdim        = xdim
  @ydim        = ydim
  @value       = value
  @unit        = unit
  @function    = function
  @description = description
  init_error "Dimensions for value don't match ydim" if value.size != ydim
  init_error "Dimensions for value don't match xdim" if value.any? { |row| row.is_a?(Array) && row.size != xdim }
end

def properties

def properties
  [:name, :xdim, :ydim, :value, :unit, :function, :description]
end

def reshape_if_needed(value, xdim, ydim)

def reshape_if_needed(value, xdim, ydim)
  if value.all? { |row| row.is_a?(Array) }
    value
  else
    value.each_slice(xdim).to_a
  end
end

def round_to(n)

def round_to(n)
  return self if value.any? { |row| row.any? { |e| e.is_a?(String) }}
  self.with \
    value: value.map { |row| row.map { |x| x.round(n) }}
end

def to_dcm(indented=false)

def to_dcm(indented=false)
  fmtstr = indented ? "%-25s%s %s" : "%s %s %d"
  sprintf(fmtstr, type.upcase, name, xdim).tap do |str|
    str << " @ #{ydim}" if ydim != 1
    str << "\n"
    str << "  LANGNAME #{description.enquote}\n" if description
    str << "  FUNKTION #{function}\n"            if function
    str << "  EINHEIT_W #{unit.enquote}\n"       if unit
    value.each do |row|
      str << case row.first
             when Numeric then "  WERT #{row.join("   ")}\n"
             when String  then "  TEXT #{row.map(&:enquote).join("   ")}\n"
             end
    end
    str << "END\n"
  end
end

def to_mfile

def to_mfile
  "#{name} = [\n".tap do |str|
    value.each do |row|
      str << row.join("  ").indent << "\n"
    end
    str << "];\n"
  end
end

def to_s(detail: false)

def to_s(detail: false)
  if detail == :value
    "#{name}:\n#{to_s(detail: :justvalue)}"
  elsif detail == :justvalue
    ValuePrinter.call(self)
  elsif detail == :onelinefull
    "#{name} #{to_s(detail: :oneline)}"
  elsif detail == :oneline
    "(#{xdim}x#{ydim}) Z: #{valuestats(value, show_avg: true)}"
  else
    "#{name}: #{xdim}x#{ydim} #{type}".tap do |str|
      if detail
        str << "\n"
        str << "  Unit: \"#{unit}\"\n"
        str << "  Value:\n"
        str << ValuePrinter.call(self).indent(4)
        str << "\n"
      end
    end
  end
end