#!/bin/bash
# Enhanced screen settings script with multi-monitor support
# Usage: 
#   ./screensetting.sh                          # Apply saved settings
#   ./screensetting.sh 1920x1080                # Set resolution (single monitor)
#   ./screensetting.sh HDMI-1 1920x1080 60      # Set specific monitor
#   ./screensetting.sh HDMI-1 1920x1080 60 normal  # With rotation
#   ./screensetting.sh save                     # Save current settings

CONFIG_FILE="/root/.screensetting"

# Function: Apply single monitor settings (legacy mode)
setting_simple() {
    local size=$1
    if ! xrandr -s $size -r 60; then
        echo "Error: Failed to set resolution" >&2
        exit 1
    fi
    echo "$size" > "$CONFIG_FILE"
    feh --bg-scale /usr/share/bizhi.jpg 2>/dev/null
    exit 0
}

# Function: Apply specific monitor settings
setting_monitor() {
    local output=$1
    local resolution=$2
    local rate=${3:-60}
    local rotation=${4:-normal}
    
    if ! xrandr --output "$output" --mode "$resolution" --rate "$rate" --rotate "$rotation"; then
        echo "Error: Failed to apply xrandr settings" >&2
        exit 1
    fi
    
    # Save configuration
    echo "OUTPUT=$output" > "$CONFIG_FILE"
    echo "RESOLUTION=$resolution" >> "$CONFIG_FILE"
    echo "RATE=$rate" >> "$CONFIG_FILE"
    echo "ROTATION=$rotation" >> "$CONFIG_FILE"
    
    feh --bg-scale /usr/share/bizhi.jpg 2>/dev/null
    exit 0
}

# Function: Save current xrandr settings
save_current() {
    local primary_output=$(xrandr | grep " connected primary" | awk '{print $1}')
    if [ -z "$primary_output" ]; then
        primary_output=$(xrandr | grep " connected" | head -1 | awk '{print $1}')
    fi
    
    if [ -z "$primary_output" ]; then
        echo "Error: No connected display found"
        exit 1
    fi
    
    local current_mode=$(xrandr | grep -A1 "^$primary_output" | grep '\*' | awk '{print $1}')
    local current_rate=$(xrandr | grep -A1 "^$primary_output" | grep '\*' | awk '{print $2}' | tr -d '*+')
    
    echo "OUTPUT=$primary_output" > "$CONFIG_FILE"
    echo "RESOLUTION=$current_mode" >> "$CONFIG_FILE"
    echo "RATE=$current_rate" >> "$CONFIG_FILE"
    echo "ROTATION=normal" >> "$CONFIG_FILE"
    
    echo "Saved: $primary_output @ $current_mode ${current_rate}Hz"
}

# Function: Load and apply saved settings
load_settings() {
    if [ ! -e "$CONFIG_FILE" ]; then
        echo "No saved settings found"
        exit 0
    fi
    
    source "$CONFIG_FILE"
    
    if [ -n "$OUTPUT" ] && [ -n "$RESOLUTION" ]; then
        echo "Applying saved settings: $OUTPUT @ $RESOLUTION ${RATE}Hz (${ROTATION})"
        setting_monitor "$OUTPUT" "$RESOLUTION" "$RATE" "$ROTATION"
    else
        # Legacy format (just resolution)
        local size=$(cat "$CONFIG_FILE")
        echo "Applying legacy settings: $size"
        setting_simple "$size"
    fi
}

# Main logic
case "$1" in
    "")
        # No arguments: load saved settings
        load_settings
        ;;
    "save")
        # Save current settings
        save_current
        ;;
    *)
        # Parse arguments
        if [ $# -eq 1 ]; then
            # Single argument: resolution only (legacy mode)
            setting_simple "$1"
        elif [ $# -ge 2 ]; then
            # Multiple arguments: output resolution [rate] [rotation]
            setting_monitor "$1" "$2" "${3:-60}" "${4:-normal}"
        fi
        ;;
esac
