unit SetArmorToClothing;

function Process(e: IInterface): Integer;
var
  dnam: IInterface;
  keywords, keyword, linked: IInterface;
  i: Integer;
  formID: Cardinal;
begin
  if Signature(e) <> 'ARMO' then begin
    AddMessage('スキップ: ' + Name(e));
    Exit;
  end;

  // Armor TypeをClothingへ変更
  SetElementEditValues(e, 'BOD2\Armor Type', 'Clothing');

  // Value, Weightを0へ変更
  SetElementEditValues(e, 'DATA\Value', '0');
  SetElementEditValues(e, 'DATA\Weight', '0');

  // KWDA から ArmorHeavyとArmorLightを削除
  keywords := ElementByPath(e, 'KWDA');
  if Assigned(keywords) then begin
    for i := ElementCount(keywords) - 1 downto 0 do begin
      keyword := ElementByIndex(keywords, i);
      linked := LinksTo(keyword);
      formID := GetLoadOrderFormID(linked);
      if (formID = $0006BBD2) or (formID = $0006BBD3) then begin
        RemoveElement(keywords, keyword);
      end;
    end;
  end;

  // DNAM再計算
  if ElementExists(e, 'DNAM') then begin
    RemoveElement(e, 'DNAM');
  end;

  dnam := Add(e, 'DNAM', true);
  if Assigned(dnam) then begin
    SetElementNativeValues(dnam, 'Armor Rating', 0);
  end;

  AddMessage('Modified: ' + Name(e));
end;

end.
