import { useEffect, useState } from "react";
import NodeTableComponent, {
  DataTemplateInterface,
} from "../NodeTableComponent/NodeTableComponent";
import { invoke } from "@tauri-apps/api/core";


const dataTemplate: DataTemplateInterface = {
  magic: {
    name: "magic",
    offset: 0,
    size: 2,
    value: null,
    data_type: "hex",
    description: "PE文件标志",
  },
  major_linker_version: {
    name: "major_linker_version",
    offset: 2,
    size: 1,
    value: null,
    data_type: "hex",
    description: "链接器主版本号",
  },
  minor_linker_version: {
    name: "minor_linker_version",
    offset: 3,
    size: 1,
    value: null,
    data_type: "hex",
    description: "链接器次版本号",
  },
  size_of_code: {
    name: "size_of_code",
    offset: 4,
    size: 4,
    value: null,
    data_type: "hex",
    description: "代码段大小",
  },
  size_of_initialized_data: {
    name: "size_of_initialized_data",
    offset: 8,
    size: 4,
    value: null,
    data_type: "hex",
    description: "初始化数据段大小",
  },
  size_of_uninitialized_data: {
    name: "size_of_uninitialized_data",
    offset: 12,
    size: 4,
    value: null,
    data_type: "hex",
    description: "未初始化数据段大小",
  },
  address_of_entry_point: {
    name: "address_of_entry_point",
    offset: 16,
    size: 4,
    value: null,
    data_type: "hex",
    description: "入口点地址",
  },
  base_of_code: {
    name: "base_of_code",
    offset: 20,
    size: 4,
    value: null,
    data_type: "hex",
    description: "代码段基址",
  },
  base_of_data: {
    name: "base_of_data",
    offset: 24,
    size: 4,
    value: null,
    data_type: "hex",
    description: "数据段基址",
  },
  image_base: {
    name: "image_base",
    offset: 28,
    size: 4,
    value: null,
    data_type: "hex",
    description: "映像基址",
  },
  section_alignment: {
    name: "section_alignment",
    offset: 32,
    size: 4,
    value: null,
    data_type: "hex",
    description: "节对齐",
  },
  file_alignment: {
    name: "file_alignment",
    offset: 36,
    size: 4,
    value: null,
    data_type: "hex",
    description: "文件对齐",
  },
  major_operating_system_version: {
    name: "major_operating_system_version",
    offset: 40,
    size: 2,
    value: null,
    data_type: "hex",
    description: "操作系统主版本号",
  },
  minor_operating_system_version: {
    name: "minor_operating_system_version",
    offset: 42,
    size: 2,
    value: null,
    data_type: "hex",
    description: "操作系统次版本号",
  },
  major_image_version: {
    name: "major_image_version",
    offset: 44,
    size: 2,
    value: null,
    data_type: "hex",
    description: "映像主版本号",
  },
  minor_image_version: {
    name: "minor_image_version",
    offset: 46,
    size: 2,
    value: null,
    data_type: "hex",
    description: "映像次版本号",
  },
  major_subsystem_version: {
    name: "major_subsystem_version",
    offset: 48,
    size: 2,
    value: null,
    data_type: "hex",
    description: "子系统主版本号",
  },
  minor_subsystem_version: {
    name: "minor_subsystem_version",
    offset: 50,
    size: 2,
    value: null,
    data_type: "hex",
    description: "子系统次版本号",
  },
  win32_version_value: {
    name: "win32_version_value",
    offset: 52,
    size: 4,
    value: null,
    data_type: "hex",
    description: "Win32版本值",
  },
  size_of_image: {
    name: "size_of_image",
    offset: 56,
    size: 4,
    value: null,
    data_type: "hex",
    description: "映像大小",
  },
  size_of_headers: {
    name: "size_of_headers",
    offset: 60,
    size: 4,
    value: null,
    data_type: "hex",
    description: "头部大小",
  },
  checksum: {
    name: "checksum",
    offset: 64,
    size: 4,
    value: null,
    data_type: "hex",
    description: "校验和",
  },
  subsystem: {
    name: "subsystem",
    offset: 68,
    size: 2,
    value: null,
    data_type: "hex",
    description: "子系统",
  },
  dll_characteristics: {
    name: "dll_characteristics",
    offset: 70,
    size: 2,
    value: null,
    data_type: "hex",
    description: "DLL特性",
  },
  size_of_stack_reserve: {
    name: "size_of_stack_reserve",
    offset: 72,
    size: 4,
    value: null,
    data_type: "hex",
    description: "堆栈保留大小",
  },
  size_of_stack_commit: {
    name: "size_of_stack_commit",
    offset: 76,
    size: 4,
    value: null,
    data_type: "hex",
    description: "堆栈提交大小",
  },
  size_of_heap_reserve: {
    name: "size_of_heap_reserve",
    offset: 80,
    size: 4,
    value: null,
    data_type: "hex",
    description: "堆保留大小",
  },
  size_of_heap_commit: {
    name: "size_of_heap_commit",
    offset: 84,
    size: 4,
    value: null,
    data_type: "hex",
    description: "堆提交大小",
  },
  loader_flags: {
    name: "loader_flags",
    offset: 88,
    size: 4,
    value: null,
    data_type: "hex",
    description: "加载器标志",
  },
  number_of_rva_and_sizes: {
    name: "number_of_rva_and_sizes",
    offset: 92,
    size: 4,
    value: null,
    data_type: "hex",
    description: "RVA和大小数量",
  },
};

const dataTemplate64: DataTemplateInterface = {
  magic: {
    name: "magic",
    offset: 0,
    size: 2,
    value: null,
    data_type: "hex",
    description: "PE文件标志",
  },
  major_linker_version: {
    name: "major_linker_version",
    offset: 2,
    size: 1,
    value: null,
    data_type: "hex",
    description: "链接器主版本号",
  },
  minor_linker_version: {
    name: "minor_linker_version",
    offset: 3,
    size: 1,
    value: null,
    data_type: "hex",
    description: "链接器次版本号",
  },
  size_of_code: {
    name: "size_of_code",
    offset: 4,
    size: 4,
    value: null,
    data_type: "hex",
    description: "代码段大小",
  },
  size_of_initialized_data: {
    name: "size_of_initialized_data",
    offset: 8,
    size: 4,
    value: null,
    data_type: "hex",
    description: "初始化数据段大小",
  },
  size_of_uninitialized_data: {
    name: "size_of_uninitialized_data",
    offset: 12,
    size: 4,
    value: null,
    data_type: "hex",
    description: "未初始化数据段大小",
  },
  address_of_entry_point: {
    name: "address_of_entry_point",
    offset: 16,
    size: 4,
    value: null,
    data_type: "hex",
    description: "入口点地址",
  },
  base_of_code: {
    name: "base_of_code",
    offset: 20,
    size: 4,
    value: null,
    data_type: "hex",
    description: "代码段基址",
  },
  image_base: {
    name: "image_base",
    offset: 24,
    size: 4,
    value: null,
    data_type: "hex",
    description: "映像基址",
  },
  section_alignment: {
    name: "section_alignment",
    offset: 28,
    size: 4,
    value: null,
    data_type: "hex",
    description: "节对齐",
  },
  file_alignment: {
    name: "file_alignment",
    offset: 32,
    size: 4,
    value: null,
    data_type: "hex",
    description: "文件对齐",
  },
  major_operating_system_version: {
    name: "major_operating_system_version",
    offset: 36,
    size: 2,
    value: null,
    data_type: "hex",
    description: "操作系统主版本号",
  },
  minor_operating_system_version: {
    name: "minor_operating_system_version",
    offset: 38,
    size: 2,
    value: null,
    data_type: "hex",
    description: "操作系统次版本号",
  },
  major_image_version: {
    name: "major_image_version",
    offset: 40,
    size: 2,
    value: null,
    data_type: "hex",
    description: "映像主版本号",
  },
  minor_image_version: {
    name: "minor_image_version",
    offset: 42,
    size: 2,
    value: null,
    data_type: "hex",
    description: "映像次版本号",
  },
  major_subsystem_version: {
    name: "major_subsystem_version",
    offset: 44,
    size: 2,
    value: null,
    data_type: "hex",
    description: "子系统主版本号",
  },
  minor_subsystem_version: {
    name: "minor_subsystem_version",
    offset: 46,
    size: 2,
    value: null,
    data_type: "hex",
    description: "子系统次版本号",
  },
  win32_version_value: {
    name: "win32_version_value",
    offset: 48,
    size: 4,
    value: null,
    data_type: "hex",
    description: "Win32版本值",
  },
  size_of_image: {
    name: "size_of_image",
    offset: 52,
    size: 4,
    value: null,
    data_type: "hex",
    description: "映像大小",
  },
  size_of_headers: {
    name: "size_of_headers",
    offset: 56,
    size: 4,
    value: null,
    data_type: "hex",
    description: "头部大小",
  },
  checksum: {
    name: "checksum",
    offset: 60,
    size: 4,
    value: null,
    data_type: "hex",
    description: "校验和",
  },
  subsystem: {
    name: "subsystem",
    offset: 64,
    size: 2,
    value: null,
    data_type: "hex",
    description: "子系统",
  },
  dll_characteristics: {
    name: "dll_characteristics",
    offset: 66,
    size: 2,
    value: null,
    data_type: "hex",
    description: "DLL特性",
  },
  size_of_stack_reserve: {
    name: "size_of_stack_reserve",
    offset: 68,
    size: 8,
    value: null,
    data_type: "hex",
    description: "堆栈保留大小",
  },
  size_of_stack_commit: {
    name: "size_of_stack_commit",
    offset: 76,
    size: 8,
    value: null,
    data_type: "hex",
    description: "堆栈提交大小",
  },
  size_of_heap_reserve: {
    name: "size_of_heap_reserve",
    offset: 84,
    size: 8,
    value: null,
    data_type: "hex",
    description: "堆保留大小",
  },
  size_of_heap_commit: {
    name: "size_of_heap_commit",
    offset: 92,
    size: 8,
    value: null,
    data_type: "hex",
    description: "堆提交大小",
  },
  loader_flags: {
    name: "loader_flags",
    offset: 100,
    size: 4,
    value: null,
    data_type: "hex",
    description: "加载器标志",
  },
  number_of_rva_and_sizes: {
    name: "number_of_rva_and_sizes",
    offset: 104,
    size: 4,
    value: null,
    data_type: "hex",
    description: "RVA和大小数量",
  },
};

const columns = [
  {
    title: "字段名",
    dataIndex: "name",
    key: "name",
  },
  {
    title: "偏移",
    dataIndex: "offset",
    key: "offset",
    hexSwitch: true,
  },
  {
    title: "大小",
    dataIndex: "size",
    key: "size",
  },
  {
    title: "值",
    dataIndex: "value",
    key: "value",
    hexSwitch: true,
    editable: true,
  },
  {
    title: "描述信息",
    dataIndex: "description",
    key: "description",
  },
];

export default function OptionalHeader() {
  const [data, setData] = useState<any[]>([]);
  const [is64, setIs64] = useState(false);
  const command = "command_get_pe_data_optional_header";
  useEffect(() => {
    invoke(command).then((res) => {
      const {is_64_bit} = res as any;
      setIs64(is_64_bit);
      setData(res as any);

    });
  }, []);

  return (
    <NodeTableComponent
      dataTemplate={is64 ? dataTemplate64 : dataTemplate}
      defaultData={data as any}
      columns={columns as any}
    ></NodeTableComponent>
  );
}
