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

local db = require('ffxiv_db')
local ffxiv_ipc_ward_land_info = Proto("ffxiv_ipc_ward_land_info", "FFXIV-IPC WardLandInfo")

local ward_land_info_fields = {
  unknown0       = ProtoField.uint16("ffxiv_ipc_ward_land_info.unknown0", "unknown0", base.DEC),
  section        = ProtoField.uint16("ffxiv_ipc_ward_land_info.section", "Section", base.DEC),
  territory_type = ProtoField.uint16("ffxiv_ipc_ward_land_info.territory_type", "TerritoryType", base.DEC, db.TerritoryType),
  world          = ProtoField.uint16("ffxiv_ipc_ward_land_info.world", "World", base.DEC, db.World),
}

ffxiv_ipc_ward_land_info.fields = ward_land_info_fields

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

  local len = tvbuf:len()

  -- dissect the unknown0 field
  local unknown0_tvbr = tvbuf:range(0, 2)
  local unknown0_val  = unknown0_tvbr:le_uint()
  tree:add_le(ward_land_info_fields.unknown0, unknown0_tvbr, unknown0_val)

  -- dissect the section field
  local section_tvbr = tvbuf:range(2, 2)
  local section_val  = section_tvbr:le_uint()
  tree:add_le(ward_land_info_fields.section, section_tvbr, section_val)

  local section_display = ", Section: " .. section_val
  pktinfo.cols.info:append(section_display)
  tree:append_text(section_display)

  -- dissect the territory_type field
  local territory_type_tvbr = tvbuf:range(4, 2)
  local territory_type_val  = territory_type_tvbr:le_uint()
  tree:add_le(ward_land_info_fields.territory_type, territory_type_tvbr, territory_type_val)

  local territory_type_display = ", TerritoryType: " .. (db.TerritoryType[territory_type_val] or "(unknown)")
  pktinfo.cols.info:append(territory_type_display)
  tree:append_text(territory_type_display)

  -- dissect the world field
  local world_tvbr = tvbuf:range(6, 2)
  local world_val  = world_tvbr:le_uint()
  tree:add_le(ward_land_info_fields.world, world_tvbr, world_val)

  -- dissect ward_land_item
  local ward_land_item_dissector = Dissector.get('ffxiv_ipc_ward_land_item')
  local ward_land_item_pos = 8
  local ward_land_item_len = 40
  local ward_land_item_count = 60

  while ward_land_item_pos + ward_land_item_len <= len do
    local ward_land_item_tvbr = tvbuf:range(ward_land_item_pos, 40)
    ward_land_item_dissector:call(ward_land_item_tvbr:tvb(), pktinfo, root)
    ward_land_item_pos = ward_land_item_pos + ward_land_item_len
    ward_land_item_count = ward_land_item_count - 1
    if ward_land_item_count <= 0 then
      break
    end
  end

  return len
end