param([Parameter(Mandatory=$true)][string]$Directory)
$ErrorActionPreference='Stop'
Add-Type -AssemblyName System.IO.Compression.FileSystem
$ns='http://schemas.openxmlformats.org/wordprocessingml/2006/main'

function Set-Attr($node,$name,$value,$xml){
  $attr=$xml.CreateAttribute('w',$name,$ns); $attr.Value=[string]$value
  [void]$node.Attributes.SetNamedItem($attr)
}
function Ensure-Child($parent,$local,$xml){
  $child=$parent.SelectSingleNode("w:$local",$script:nsmgr)
  if(-not $child){$child=$xml.CreateElement('w',$local,$ns); [void]$parent.AppendChild($child)}
  return $child
}
function Set-Style($xml,$styleId,$font,$eastAsia,$sizeHalf,$color,$bold,$before,$after,$line){
  $s=$xml.SelectSingleNode("//w:style[@w:styleId='$styleId']",$script:nsmgr); if(-not $s){return}
  $rPr=Ensure-Child $s 'rPr' $xml
  $fonts=Ensure-Child $rPr 'rFonts' $xml; Set-Attr $fonts 'ascii' $font $xml; Set-Attr $fonts 'hAnsi' $font $xml; Set-Attr $fonts 'eastAsia' $eastAsia $xml
  $sz=Ensure-Child $rPr 'sz' $xml; Set-Attr $sz 'val' $sizeHalf $xml
  $szCs=Ensure-Child $rPr 'szCs' $xml; Set-Attr $szCs 'val' $sizeHalf $xml
  $c=Ensure-Child $rPr 'color' $xml; Set-Attr $c 'val' $color $xml
  $b=$rPr.SelectSingleNode('w:b',$script:nsmgr)
  if($bold -and -not $b){$b=$xml.CreateElement('w','b',$ns); [void]$rPr.AppendChild($b)} elseif(-not $bold -and $b){[void]$rPr.RemoveChild($b)}
  $pPr=Ensure-Child $s 'pPr' $xml
  $spacing=Ensure-Child $pPr 'spacing' $xml; Set-Attr $spacing 'before' $before $xml; Set-Attr $spacing 'after' $after $xml; Set-Attr $spacing 'line' $line $xml; Set-Attr $spacing 'lineRule' 'auto' $xml
}

foreach($file in Get-ChildItem -LiteralPath $Directory -Filter '*.docx'){
  $tmp=Join-Path $Directory ('.tmp_'+[guid]::NewGuid().ToString('N'))
  [IO.Compression.ZipFile]::ExtractToDirectory($file.FullName,$tmp)
  try{
    [xml]$styles=Get-Content -Raw -Encoding UTF8 (Join-Path $tmp 'word\styles.xml')
    $script:nsmgr=New-Object Xml.XmlNamespaceManager($styles.NameTable); $nsmgr.AddNamespace('w',$ns)
    Set-Style $styles 'Normal' 'Calibri' 'Microsoft YaHei' 21 '000000' $false 0 120 300
    Set-Style $styles 'Title' 'Calibri' 'Microsoft YaHei' 50 '17365D' $true 1200 240 240
    Set-Style $styles 'Subtitle' 'Calibri' 'Microsoft YaHei' 28 '666666' $false 0 360 280
    Set-Style $styles 'Heading1' 'Calibri' 'Microsoft YaHei' 32 '2E74B5' $true 320 160 280
    Set-Style $styles 'Heading2' 'Calibri' 'Microsoft YaHei' 26 '2E74B5' $true 240 120 280
    Set-Style $styles 'Heading3' 'Calibri' 'Microsoft YaHei' 23 '1F4D78' $true 180 80 280
    $styles.Save((Join-Path $tmp 'word\styles.xml'))

    [xml]$doc=Get-Content -Raw -Encoding UTF8 (Join-Path $tmp 'word\document.xml')
    $script:nsmgr=New-Object Xml.XmlNamespaceManager($doc.NameTable); $nsmgr.AddNamespace('w',$ns)
    foreach($sect in $doc.SelectNodes('//w:sectPr',$nsmgr)){
      $pgSz=Ensure-Child $sect 'pgSz' $doc; Set-Attr $pgSz 'w' 12240 $doc; Set-Attr $pgSz 'h' 15840 $doc
      $pgMar=Ensure-Child $sect 'pgMar' $doc; Set-Attr $pgMar 'top' 1224 $doc; Set-Attr $pgMar 'right' 1296 $doc; Set-Attr $pgMar 'bottom' 1152 $doc; Set-Attr $pgMar 'left' 1296 $doc; Set-Attr $pgMar 'header' 504 $doc; Set-Attr $pgMar 'footer' 504 $doc; Set-Attr $pgMar 'gutter' 0 $doc
    }
    foreach($tbl in $doc.SelectNodes('//w:tbl',$nsmgr)){
      $tblPr=Ensure-Child $tbl 'tblPr' $doc
      $style=Ensure-Child $tblPr 'tblStyle' $doc; Set-Attr $style 'val' 'TableGrid' $doc
      $layout=Ensure-Child $tblPr 'tblLayout' $doc; Set-Attr $layout 'type' 'autofit' $doc
      $first=$tbl.SelectSingleNode('w:tr[1]',$nsmgr)
      if($first){
        $trPr=Ensure-Child $first 'trPr' $doc; $hdr=Ensure-Child $trPr 'tblHeader' $doc; Set-Attr $hdr 'val' 1 $doc
        foreach($cell in $first.SelectNodes('w:tc',$nsmgr)){
          $tcPr=Ensure-Child $cell 'tcPr' $doc; $shd=Ensure-Child $tcPr 'shd' $doc; Set-Attr $shd 'fill' '17365D' $doc
          foreach($rPr in $cell.SelectNodes('.//w:rPr',$nsmgr)){ $c=Ensure-Child $rPr 'color' $doc; Set-Attr $c 'val' 'FFFFFF' $doc; if(-not $rPr.SelectSingleNode('w:b',$nsmgr)){[void]$rPr.AppendChild($doc.CreateElement('w','b',$ns))} }
        }
      }
    }
    $doc.Save((Join-Path $tmp 'word\document.xml'))

    foreach($hf in Get-ChildItem -LiteralPath (Join-Path $tmp 'word') -Filter 'header*.xml' -ErrorAction SilentlyContinue){
      [xml]$hx=Get-Content -Raw -Encoding UTF8 $hf.FullName
      $script:nsmgr=New-Object Xml.XmlNamespaceManager($hx.NameTable); $nsmgr.AddNamespace('w',$ns)
      foreach($t in $hx.SelectNodes('//w:t',$nsmgr)){$t.InnerText='大规模多智能体网络流量建模与预测'}
      foreach($rPr in $hx.SelectNodes('//w:rPr',$nsmgr)){
        $fonts=Ensure-Child $rPr 'rFonts' $hx; Set-Attr $fonts 'eastAsia' 'Microsoft YaHei' $hx
        $sz=Ensure-Child $rPr 'sz' $hx; Set-Attr $sz 'val' 17 $hx
        $c=Ensure-Child $rPr 'color' $hx; Set-Attr $c 'val' '777777' $hx
      }
      $hx.Save($hf.FullName)
    }

    $new=$file.FullName+'.new'
    if(Test-Path $new){Remove-Item -LiteralPath $new}
    [IO.Compression.ZipFile]::CreateFromDirectory($tmp,$new)
    Copy-Item -Force -LiteralPath $new -Destination $file.FullName
    Remove-Item -LiteralPath $new
  } finally { Remove-Item -Recurse -Force -LiteralPath $tmp }
}
