-- This file is generated by tools/json2lua.js

local db = require('ffxiv_db')
local ffxiv_ipc_examine_item_data = Proto("ffxiv_ipc_examine_item_data", "FFXIV-IPC ExamineItemData")

local examine_item_data_fields = {
  catalog_id           = ProtoField.uint32("ffxiv_ipc_examine_item_data.catalog_id", "CatalogId", base.DEC, db.Item),
  appearance           = ProtoField.uint32("ffxiv_ipc_examine_item_data.appearance", "Appearance", base.DEC, db.Item),
  crafter_id           = ProtoField.uint64("ffxiv_ipc_examine_item_data.crafter_id", "CrafterId", base.DEC),
  quality              = ProtoField.uint8("ffxiv_ipc_examine_item_data.quality", "Quality", base.DEC),
  unknown              = ProtoField.uint8("ffxiv_ipc_examine_item_data.unknown", "Unknown", base.DEC),
  unknown2             = ProtoField.uint16("ffxiv_ipc_examine_item_data.unknown2", "Unknown2", base.DEC),
}

ffxiv_ipc_examine_item_data.fields = examine_item_data_fields

function ffxiv_ipc_examine_item_data.dissector(tvbuf, pktinfo, root)
  local tree = root:add(ffxiv_ipc_examine_item_data, tvbuf)
  pktinfo.cols.info:set("ExamineItemData")

  local len = tvbuf:len()

  -- dissect the catalog_id field
  local catalog_id_tvbr = tvbuf:range(0, 4)
  local catalog_id_val  = catalog_id_tvbr:le_uint()
  tree:add_le(examine_item_data_fields.catalog_id, catalog_id_tvbr, catalog_id_val)

  local catalog_id_display = ", CatalogId: " .. (db.Item[catalog_id_val] or "(unknown)")
  pktinfo.cols.info:append(catalog_id_display)
  tree:append_text(catalog_id_display)

  -- dissect the appearance field
  local appearance_tvbr = tvbuf:range(4, 4)
  local appearance_val  = appearance_tvbr:le_uint()
  tree:add_le(examine_item_data_fields.appearance, appearance_tvbr, appearance_val)

  local appearance_display = ", Appearance: " .. (db.Item[appearance_val] or "(unknown)")
  pktinfo.cols.info:append(appearance_display)
  tree:append_text(appearance_display)

  -- dissect the crafter_id field
  local crafter_id_tvbr = tvbuf:range(8, 8)
  local crafter_id_val  = crafter_id_tvbr:le_uint64()
  tree:add_le(examine_item_data_fields.crafter_id, crafter_id_tvbr, crafter_id_val)

  -- dissect the quality field
  local quality_tvbr = tvbuf:range(16, 1)
  local quality_val  = quality_tvbr:le_uint()
  tree:add_le(examine_item_data_fields.quality, quality_tvbr, quality_val)

  -- dissect the unknown field
  local unknown_tvbr = tvbuf:range(17, 1)
  local unknown_val  = unknown_tvbr:le_uint()
  tree:add_le(examine_item_data_fields.unknown, unknown_tvbr, unknown_val)

  -- dissect examine_item_materia
  local examine_item_materia_dissector = Dissector.get('ffxiv_ipc_examine_item_materia')
  local examine_item_materia_pos = 18
  local examine_item_materia_len = 4
  local examine_item_materia_count = 5

  while examine_item_materia_pos + examine_item_materia_len <= len do
    local examine_item_materia_tvbr = tvbuf:range(examine_item_materia_pos, 4)
    examine_item_materia_dissector:call(examine_item_materia_tvbr:tvb(), pktinfo, root)
    examine_item_materia_pos = examine_item_materia_pos + examine_item_materia_len
    examine_item_materia_count = examine_item_materia_count - 1
    if examine_item_materia_count <= 0 then
      break
    end
  end

  -- dissect the unknown2 field
  local unknown2_tvbr = tvbuf:range(38, 2)
  local unknown2_val  = unknown2_tvbr:le_uint()
  tree:add_le(examine_item_data_fields.unknown2, unknown2_tvbr, unknown2_val)

  if catalog_id_val == 0 then
    tree:set_hidden(true)
  end

  return len
end