#!/bin/bash
# Install Sway configuration files only - Arch Linux version

set -e

echo "Installing Sway configuration files for Arch Linux..."
echo "======================================================="

# Create sway config directory
mkdir -p ~/.config/sway
echo "✓ Created ~/.config/sway directory"

# Backup existing sway config if it exists
if [ -f ~/.config/sway/config ]; then
    echo "⚠ Backing up existing sway config to ~/.config/sway/config.backup"
    cp ~/.config/sway/config ~/.config/sway/config.backup
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# === MAKO CONFIG ===
# Create mako config directory
mkdir -p ~/.config/mako
echo "✓ Created ~/.config/mako directory"
echo ""
echo "Installing mako config..."
cp $SCRIPT_DIR/home_dotfiles/.config/mako/config ~/.config/mako/config
echo "✓ mako config installed to ~/.config/mako/config"

# === SWAY CONFIG ===
# Comment out this section if you don't want to install the main config
echo ""
echo "Installing main Sway config..."
cp $SCRIPT_DIR/home_dotfiles/.config/sway/config ~/.config/sway/config
echo "✓ Sway config installed to ~/.config/sway/config"

# === SWAYIMG CONFIG ===
echo ""
echo "Installing swayimg config..."
mkdir -p ~/.config/swayimg
cp $SCRIPT_DIR/home_dotfiles/.config/swayimg/swayimgrc ~/.config/swayimg/swayimgrc
echo "✓ Swayimg config installed to ~/.config/swayimg/swayimgrc"

# === I3STATUS-RUST CONFIG ===
# Comment out this section if you don't want i3status-rust
echo ""
echo "Installing i3status-rust config..."
cp $SCRIPT_DIR/home_dotfiles/.config/sway/i3status-rust.toml ~/.config/sway/i3status-rust.toml
echo "✓ i3status-rust config installed to ~/.config/sway/i3status-rust.toml"

# === DISPLAY PROFILE TOGGLE (MODE / BAR HEIGHT / OUTPUT POSITION) ===
echo ""
echo "Installing display profile toggle scripts/config..."
mkdir -p ~/.config/sway/scripts/display-profile
for file in display-profile.conf display-profile-toggle.sh display-profile-status.sh; do
    if [ -f "$SCRIPT_DIR/home_dotfiles/.config/sway/scripts/display-profile/$file" ]; then
        cp "$SCRIPT_DIR/home_dotfiles/.config/sway/scripts/display-profile/$file" ~/.config/sway/scripts/display-profile/$file
        case "$file" in
            *.sh)
                chmod +x ~/.config/sway/scripts/display-profile/$file
                ;;
        esac
        echo "✓ Installed ~/.config/sway/scripts/display-profile/$file"
    else
        echo "⚠ $file not found, skipping..."
    fi
done

# === OSRS MODE SCRIPTS ===
echo ""
echo "Installing OSRS mode helper scripts..."
mkdir -p ~/.config/sway/scripts/osrs
for script in osrs-mode-status.sh osrs-mode-toggle.sh; do
    if [ -f "$SCRIPT_DIR/home_dotfiles/.config/sway/scripts/osrs/$script" ]; then
        cp "$SCRIPT_DIR/home_dotfiles/.config/sway/scripts/osrs/$script" ~/.config/sway/scripts/osrs/$script
        chmod +x ~/.config/sway/scripts/osrs/$script
        echo "✓ Installed ~/.config/sway/scripts/osrs/$script"
    else
        echo "⚠ $script not found, skipping..."
    fi
done

# === i3status BAR SCRIPTS ===
echo ""
echo "Installing i3status bar scripts..."
mkdir -p ~/.config/sway/scripts/i3status
for script in bluetooth.sh wttrin-weather.sh set-random-bg.sh get-focused-window.sh; do
    if [ -f "$SCRIPT_DIR/home_dotfiles/.config/sway/scripts/i3status/$script" ]; then
        cp "$SCRIPT_DIR/home_dotfiles/.config/sway/scripts/i3status/$script" ~/.config/sway/scripts/i3status/$script
        chmod +x ~/.config/sway/scripts/i3status/$script
        echo "✓ Installed ~/.config/sway/scripts/i3status/$script"
    else
        echo "⚠ $script not found, skipping..."
    fi
done

# === SWAY CONFIG SCRIPTS ===
echo ""
echo "Installing sway config scripts..."
mkdir -p ~/.config/sway/scripts/sway
for script in volume-notify.sh; do
    if [ -f "$SCRIPT_DIR/home_dotfiles/.config/sway/scripts/sway/$script" ]; then
        cp "$SCRIPT_DIR/home_dotfiles/.config/sway/scripts/sway/$script" ~/.config/sway/scripts/sway/$script
        chmod +x ~/.config/sway/scripts/sway/$script
        echo "✓ Installed ~/.config/sway/scripts/sway/$script"
    else
        echo "⚠ $script not found, skipping..."
    fi
done

# === SET THUNAR AS DEFAULT FILE MANAGER ===
echo ""
echo "Setting thunar as default file manager..."
xdg-mime default thunar.desktop inode/directory application/x-gnome-saved-search
echo "✓ thunar set as default file manager"

# === SET SWAYIMG AS DEFAULT IMAGE VIEWER ===
echo ""
echo "Setting swayimg as default image viewer..."
xdg-mime default swayimg.desktop image/jpeg image/jpg image/png image/gif image/webp image/bmp image/tiff image/svg+xml
echo "✓ swayimg set as default image viewer"

# === SET FIREFOX AS DEFAULT BROWSER ===
echo ""
echo "Setting Firefox as default browser..."
if command -v firefox >/dev/null 2>&1; then
    xdg-settings set default-web-browser firefox.desktop
    echo "✓ Firefox set as default browser"
else
    echo "⚠ Firefox not installed, skipping default browser setup"
fi

# === SET WALLPAPER ===
echo ""
echo "Setting wallpaper..."
WALLPAPER_NAME="chris-czermak-PamFFHL6fVY-unsplash.jpg"
#WALLPAPER_NAME="lucas-gallone-2dClJIAR404-unsplash.jpg"
#WALLPAPER_NAME="1742471076220983.jpg"
WALLPAPER_PATH="$SCRIPT_DIR/home_dotfiles/wallpapers/$WALLPAPER_NAME"
cp $WALLPAPER_PATH ~/.config/sway/wallpaper.jpg
echo "✓ Wallpaper installed to ~/.config/sway/wallpaper.jpg"

echo ""
echo "=========================================="
echo "✅ Sway configuration installation complete!"
echo "=========================================="
echo ""
echo "Next steps:"
echo "1. Run ./install-arch-sway-requirements.sh to install packages"
echo "2. Log out and select Sway from your display manager"
echo "3. Or start Sway from TTY with: sway"
echo ""
echo "Config files installed:"
echo "- ~/.config/mako/config (notification styling)"
echo "- ~/.config/sway/config (main Sway config)"
echo "- ~/.config/swayimg/swayimgrc (image viewer config)"
echo "- ~/.config/sway/i3status-rust.toml (status bar config)"
echo "- ~/.config/sway/scripts/display-profile/display-profile.conf (display profile state/overrides)"
echo "- ~/.config/sway/scripts/display-profile/display-profile-toggle.sh (click-to-toggle display profile)"
echo "- ~/.config/sway/scripts/display-profile/display-profile-status.sh (status bar label for display profile)"
echo "- ~/.config/sway/scripts/i3status/wttrin-weather.sh (weather script)"
echo "- thunar set as default file manager"
echo "- swayimg set as default image viewer"
if [ -f ~/.config/sway/scripts/sway/volume-notify.sh ]; then
    echo "- ~/.config/sway/scripts/sway/volume-notify.sh (volume notifications)"
fi 

makoctl reload
