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

local db = require('ffxiv_db')
local enum = require('ffxiv_enum')
local label_data0_type = {
  [21] = "Status",
  [23] = "Status",
  [116] = "Fate",
  [518] = "Achievement",
}
local label_data1_type = {
  [23] = "Type",
  [300] = "Enabled",
}
local label_data2_type = {
  [23] = "Value",
}
local label_data3_type = {
  [23] = "ActorId",
}
local ffxiv_ipc_actor_control = Proto("ffxiv_ipc_actor_control", "FFXIV-IPC Actor Control")

local actor_control_fields = {
  type    = ProtoField.uint16("ffxiv_ipc_actor_control.type", "Type", base.DEC, enum.reverse.actor_control_type),
  unknown = ProtoField.uint16("ffxiv_ipc_actor_control.unknown", "Unknown", base.DEC),
  data0   = ProtoField.uint32("ffxiv_ipc_actor_control.data0", "Data0", base.DEC),
  data1   = ProtoField.uint32("ffxiv_ipc_actor_control.data1", "Data1", base.DEC),
  data2   = ProtoField.uint32("ffxiv_ipc_actor_control.data2", "Data2", base.DEC),
  data3   = ProtoField.uint32("ffxiv_ipc_actor_control.data3", "Data3", base.DEC),
  data4   = ProtoField.uint32("ffxiv_ipc_actor_control.data4", "Data4", base.DEC),
}

ffxiv_ipc_actor_control.fields = actor_control_fields

function ffxiv_ipc_actor_control.dissector(tvbuf, pktinfo, root)
  local tree = root:add(ffxiv_ipc_actor_control, tvbuf)
  pktinfo.cols.info:set("Actor Control")

  local len = tvbuf:len()

  -- dissect the type field
  local type_tvbr = tvbuf:range(0, 2)
  local type_val  = type_tvbr:le_uint()
  tree:add_le(actor_control_fields.type, type_tvbr, type_val)

  local type_display = ", Type: " .. (enum.reverse.actor_control_type[type_val] or "(unknown)")
  pktinfo.cols.info:append(type_display)
  tree:append_text(type_display)

  -- dissect the unknown field
  local unknown_tvbr = tvbuf:range(2, 2)
  local unknown_val  = unknown_tvbr:le_uint()
  tree:add_le(actor_control_fields.unknown, unknown_tvbr, unknown_val)

  -- dissect the data0 field
  local data0_tvbr = tvbuf:range(4, 4)
  local data0_val  = data0_tvbr:le_uint()
  local data0_label_key = "Data0"
  local data0_label_val = data0_val
  if type_val == 21 then
    data0_label_key = "Status"
    data0_label_val = (db.Status[data0_val] or "Unknown") .. " (" .. data0_val .. ")"
  elseif type_val == 23 then
    data0_label_key = "Status"
    data0_label_val = (db.Status[data0_val] or "Unknown") .. " (" .. data0_val .. ")"
  elseif type_val == 116 then
    data0_label_key = "Fate"
    data0_label_val = (db.Fate[data0_val] or "Unknown") .. " (" .. data0_val .. ")"
  elseif type_val == 518 then
    data0_label_key = "Achievement"
  end
  tree:add_le(actor_control_fields.data0, data0_tvbr, data0_val, data0_label_key .. ": " .. data0_label_val)

  -- dissect the data1 field
  local data1_tvbr = tvbuf:range(8, 4)
  local data1_val  = data1_tvbr:le_uint()
  local data1_label_key = "Data1"
  local data1_label_val = data1_val
  if type_val == 23 then
    data1_label_key = "Type"
  elseif type_val == 300 then
    data1_label_key = "Enabled"
  end
  tree:add_le(actor_control_fields.data1, data1_tvbr, data1_val, data1_label_key .. ": " .. data1_label_val)

  -- dissect the data2 field
  local data2_tvbr = tvbuf:range(12, 4)
  local data2_val  = data2_tvbr:le_uint()
  local data2_label_key = "Data2"
  local data2_label_val = data2_val
  if type_val == 23 then
    data2_label_key = "Value"
  end
  tree:add_le(actor_control_fields.data2, data2_tvbr, data2_val, data2_label_key .. ": " .. data2_label_val)

  -- dissect the data3 field
  local data3_tvbr = tvbuf:range(16, 4)
  local data3_val  = data3_tvbr:le_uint()
  local data3_label_key = "Data3"
  local data3_label_val = data3_val
  if type_val == 23 then
    data3_label_key = "ActorId"
  end
  tree:add_le(actor_control_fields.data3, data3_tvbr, data3_val, data3_label_key .. ": " .. data3_label_val)

  -- dissect the data4 field
  local data4_tvbr = tvbuf:range(20, 4)
  local data4_val  = data4_tvbr:le_uint()
  tree:add_le(actor_control_fields.data4, data4_tvbr, data4_val)

  return len
end