name: Windows

on: [push]

env:
  SDL2: 2.30.2
  SDL2_TTF: 2.22.0
  SDL2_MIXER: 2.8.0
  SDL2_IMAGE: 2.8.2
  SDL2_GFX: 1.0.4

permissions:
  contents: write

jobs:
  deploy:
    runs-on: windows-latest

    steps:
      - name: Checkout 
        uses: actions/checkout@v4

      - name: Download SDL2 Libraries
        run: |
          curl -L "https://github.com/libsdl-org/SDL/releases/download/release-${{ env.SDL2 }}/SDL2-devel-${{ env.SDL2 }}-VC.zip" -o "sdl2_devel.zip"
          curl -L "https://github.com/libsdl-org/SDL_mixer/releases/download/release-${{ env.SDL2_MIXER }}/SDL2_mixer-devel-${{ env.SDL2_MIXER }}-VC.zip" -o "sdl2_mixer_devel.zip"
          curl -L "https://github.com/libsdl-org/SDL_ttf/releases/download/release-${{ env.SDL2_TTF }}/SDL2_ttf-devel-${{ env.SDL2_TTF }}-VC.zip" -o "sdl2_ttf_devel.zip"
          curl -L "https://github.com/libsdl-org/SDL_image/releases/download/release-${{ env.SDL2_IMAGE }}/SDL2_image-devel-${{ env.SDL2_IMAGE }}-VC.zip" -o "sdl2_image_devel.zip"
        # curl -L "https://sourceforge.net/projects/sdl2gfx/files/SDL2_gfx-${{ env.SDL2_GFX }}.tar.gz/download" -o "sdl2_gfx.tar.gz"
      
      - name: Extract SDL2 DLLs
        run: |
          7z x ./sdl2_devel.zip -o"./tmp/"
          mv ./tmp/SDL2-${{ env.SDL2 }}/lib/x64/SDL2.dll ./
          mv ./tmp/SDL2-${{ env.SDL2 }}/lib/x64/SDL2.lib ./

          7z x ./sdl2_mixer_devel.zip -o"./tmp/"
          mv ./tmp/SDL2_mixer-${{ env.SDL2_MIXER }}/lib/x64/SDL2_mixer.dll ./
          mv ./tmp/SDL2_mixer-${{ env.SDL2_MIXER }}/lib/x64/SDL2_mixer.lib ./

          7z x ./sdl2_ttf_devel.zip -o"./tmp/"
          mv ./tmp/SDL2_ttf-${{ env.SDL2_TTF }}/lib/x64/SDL2_ttf.dll ./
          mv ./tmp/SDL2_ttf-${{ env.SDL2_TTF }}/lib/x64/SDL2_ttf.lib ./

          7z x ./sdl2_image_devel.zip -o"./tmp/"
          mv ./tmp/SDL2_image-${{ env.SDL2_IMAGE }}/lib/x64/SDL2_image.dll ./
          mv ./tmp/SDL2_image-${{ env.SDL2_IMAGE }}/lib/x64/SDL2_image.lib ./
      
      - name: Install SDL2_gfx
        run: |
          C:\vcpkg\vcpkg.exe install sdl2-gfx:x64-windows-release
          cp C:\vcpkg\packages\sdl2-gfx_x64-windows-release\bin\SDL2_gfx.dll ./
          cp C:\vcpkg\packages\sdl2-gfx_x64-windows-release\lib\SDL2_gfx.lib ./
      
      - name: Setup Rust (Windows)
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          target: x86_64-pc-windows-gnu
          override: true

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2

      - name: Build
        run: cargo build --release

      - name: Prepare Archive
        run: |
          New-Item -Type Directory ./release/
          Move-Item -Path ./target/release/pacman.exe -Destination ./release/
          Move-Item -Path ./SDL2.dll, ./SDL2_image.dll, ./SDL2_ttf.dll, ./SDL2_mixer.dll, ./SDL2_gfx.dll -Destination ./release/
          New-Item -Type Directory ./release/assets/
          Move-Item -Path ./assets/TerminalVector.ttf, ./assets/tetris.ogg, ./assets/fruit.png, -Destination ./release/assets/

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: windows
          path: ./release/
          retention-days: 7
          if-no-files-found: error