#!/bin/bash

# Function to get the current active mode id for the enabled output
get_current_mode_id() {
  get_screen_info | jq -r '.outputs[] | select(.enabled == true) | .currentModeId'
}

# Function to get the active mode information (width, height, refresh rate) based on the mode id for the enabled output
get_mode_info() {
  local mode_id="$1"
  get_screen_info | jq -r --arg mode_id "$mode_id" '.outputs[] | select(.enabled == true) | .modes[] | select(.id == $mode_id) | {width: .size.width, height: .size.height, refresh: .refreshRate}'
}

# Function to get the HDR state of the enabled display
get_hdr_state() {
  get_screen_info | jq -r '.outputs[] | select(.enabled == true) | .hdr'
}

# Function to get the VRR (adaptive sync) state of the enabled display
get_vrr_state() {
  get_screen_info | jq -r '.outputs[] | select(.enabled == true) | .vrrPolicy'
}

# Function to get current screen information in JSON format
get_screen_info() {
  kscreen-doctor -j
}

# Extract width, height, and refresh rate from mode info for the enabled output
get_width() {
  echo "$1" | jq -r '.width'
}

get_height() {
  echo "$1" | jq -r '.height'
}

get_refresh_rate() {
  echo "$1" | jq -r '.refresh'
}

# Main script logic

# Get the current mode ID and mode info for the enabled display
mode_id=$(get_current_mode_id)
mode_info=$(get_mode_info "$mode_id")

# Extract width, height, and refresh rate from the mode info
KDE_WIDTH=$(get_width "$mode_info")
KDE_HEIGHT=$(get_height "$mode_info")
KDE_REFRESH=$(get_refresh_rate "$mode_info")

# Get HDR and VRR (adaptive sync) states for the enabled display
HDR_STATE=$(get_hdr_state)
VRR_STATE=$(get_vrr_state)

# Build the gamescope command with dynamic flags based on HDR and VRR support
gamescope_cmd="gamescope -W $KDE_WIDTH -H $KDE_HEIGHT -r $KDE_REFRESH --framerate-limit 175 --fullscreen --force-grab-cursor --mangoapp"

# Append HDR flag if HDR is supported and enabled
if [[ "$HDR_STATE" == "true" ]]; then
  gamescope_cmd="$gamescope_cmd --hdr-enabled"
fi

# Append VRR (adaptive sync) flag if supported
if [[ "$VRR_STATE" == 1 || "$VRR_STATE" == 2 ]]; then
  gamescope_cmd="$gamescope_cmd --adaptive-sync"
fi

# Output the environment variables and the gamescope command
echo "Screen Width: $KDE_WIDTH"
echo "Screen Height: $KDE_HEIGHT"
echo "Screen Refresh Rate: $KDE_REFRESH"
echo "HDR State: $HDR_STATE"
echo "VRR State: $VRR_STATE"
echo "Running: $gamescope_cmd"

# Run the constructed gamescope command
$gamescope_cmd -- "$@"
