#!/bin/bash

# Setup Script for Hyprland Rice Configuration
# Creates symlinks and performs final setup

set -e

echo "🔧 Setting up Hyprland rice configuration..."

CONFIG_DIR="~/config"
USER_HOME="$HOME"
XDG_CONFIG_HOME="$USER_HOME/.config"

# Function to create symlink
create_symlink() {
    local source="$1"
    local target="$2"
    local description="$3"
    
    if [ -e "$source" ]; then
        echo "🔗 Creating symlink: $description"
        # Remove existing file/directory if it exists
        [ -e "$target" ] && rm -rf "$target"
        # Create parent directory if it doesn't exist
        mkdir -p "$(dirname "$target")"
        # Create symlink
        ln -sf "$source" "$target"
        echo "   ✅ $description configured"
    else
        echo "   ⚠️  Source not found: $source"
    fi
}

# Create configuration symlinks
echo "📁 Setting up configuration symlinks..."

# Hyprland configuration
create_symlink "$CONFIG_DIR/config/hypr" "$XDG_CONFIG_HOME/hypr" "Hyprland configuration"

# Waybar configuration
create_symlink "$CONFIG_DIR/config/waybar" "$XDG_CONFIG_HOME/waybar" "Waybar configuration"

# Wofi configuration
create_symlink "$CONFIG_DIR/config/wofi" "$XDG_CONFIG_HOME/wofi" "Wofi configuration"

# Dunst configuration
create_symlink "$CONFIG_DIR/config/dunst" "$XDG_CONFIG_HOME/dunst" "Dunst notification daemon"

# Ghostty configuration
create_symlink "$CONFIG_DIR/config/ghostty" "$XDG_CONFIG_HOME/ghostty" "Ghostty terminal configuration"

# Wlogout configuration
create_symlink "$CONFIG_DIR/config/wlogout" "$XDG_CONFIG_HOME/wlogout" "Wlogout configuration"

# Qt5CT configuration
create_symlink "$CONFIG_DIR/config/qt5ct" "$XDG_CONFIG_HOME/qt5ct" "Qt5CT configuration"

# Fastfetch configuration
create_symlink "$CONFIG_DIR/config/fastfetch" "$XDG_CONFIG_HOME/fastfetch" "Fastfetch configuration"

# XDG Desktop Portal configuration
create_symlink "$CONFIG_DIR/config/xdg-desktop-portal" "$XDG_CONFIG_HOME/xdg-desktop-portal" "XDG Desktop Portal configuration"

# Create fonts directory and symlink custom font
echo "🔤 Setting up fonts..."
FONT_DIR="$USER_HOME/.local/share/fonts"
mkdir -p "$FONT_DIR"
create_symlink "$CONFIG_DIR/config/dotfile_deps/DotfilesFont.otf" "$FONT_DIR/DotfilesFont.otf" "Custom Dotfiles font"

# Set up GTK configuration
echo "🎨 Setting up GTK configuration..."
GTK_CONFIG_DIR="$XDG_CONFIG_HOME/gtk-3.0"
mkdir -p "$GTK_CONFIG_DIR"
create_symlink "$CONFIG_DIR/config/gtk-3.0/settings.ini" "$GTK_CONFIG_DIR/settings.ini" "GTK3 settings"

# Install Arashi icon theme
echo "🎯 Installing Arashi icon theme..."
ARASHI_THEME_DIR="$USER_HOME/.local/share/icons/Arashi"
ARASHI_REPO="https://github.com/0hStormy/Arashi"

if [ ! -d "$ARASHI_THEME_DIR" ]; then
    mkdir -p "$USER_HOME/.local/share/icons/"
    echo "   📥 Cloning Arashi icon theme from GitHub..."
    git clone "$ARASHI_REPO" "$ARASHI_THEME_DIR"
    echo "   ✅ Arashi icon theme installed from GitHub"
else
    echo "   ✅ Arashi icon theme already installed"
fi

# Set up desktop environment
echo "🖥️ Configuring desktop environment..."

# Set GTK theme
if command -v gsettings &> /dev/null; then
    echo "🎨 Setting GTK themes..."
    gsettings set org.gnome.desktop.interface gtk-theme "Catppuccin-Mocha-Standard-Blue-Dark"
    gsettings set org.gnome.desktop.interface icon-theme "Arashi"
    gsettings set org.gnome.desktop.interface cursor-theme "RalseiCursors"
    gsettings set org.gnome.desktop.interface font-name "Noto Sans 11"
    gsettings set org.gnome.desktop.wm.preferences theme "Catppuccin-Mocha-Standard-Blue-Dark"
    echo "   ✅ GTK themes configured"
fi

# Set Qt theme
echo "🎨 Setting Qt themes..."
if command -v qt5ct &> /dev/null; then
    echo "   ✅ Qt5CT available for Qt5 theme configuration"
fi

if command -v qt6ct &> /dev/null; then
    echo "   ✅ Qt6CT available for Qt6 theme configuration"
fi

# Configure XDG directories
echo "📂 Setting up XDG directories..."
mkdir -p "$USER_HOME/.local/share/applications"
mkdir -p "$USER_HOME/.local/share/mime"
mkdir -p "$USER_HOME/.cache"

# Set up environment variables
echo "🌍 Setting up environment variables..."
sudo tee -a /etc/environment << EOF

# Hyprland rice environment variables
XDG_TERMINAL=ghostty
XDG_SYSTEM_MONITOR=htop
GTK_USE_PORTAL=1
QT_QPA_PLATFORMTHEME=qt5ct
EOF
echo "   ✅ Environment variables configured in /etc/environment"

# Create useful scripts
echo "📝 Creating useful scripts..."
SCRIPTS_DIR="$USER_HOME/.local/bin"
mkdir -p "$SCRIPTS_DIR"

# Roblox game scripts
cat > "$SCRIPTS_DIR/regretevator" << 'EOF'
#!/bin/bash
xdg-open roblox://placeId=4972273297
EOF

cat > "$SCRIPTS_DIR/kaijuparadise" << 'EOF'
#!/bin/bash
xdg-open roblox://placeId=6456351776
EOF

cat > "$SCRIPTS_DIR/sewh" << 'EOF'
#!/bin/bash
xdg-open roblox://placeId=16991287194
EOF

# Make scripts executable
chmod +x "$SCRIPTS_DIR/regretevator"
chmod +x "$SCRIPTS_DIR/kaijuparadise"
chmod +x "$SCRIPTS_DIR/sewh"

echo "   ✅ Roblox game scripts created"

# Update font cache
echo "🔄 Updating font cache..."
if command -v fc-cache &> /dev/null; then
    fc-cache -fv
    echo "   ✅ Font cache updated"
fi

# Update icon cache
echo "🔄 Updating icon cache..."
if command -v gtk-update-icon-cache &> /dev/null; then
    gtk-update-icon-cache -f -t "$USER_HOME/.local/share/icons" 2>/dev/null || true
    echo "   ✅ Icon cache updated"
fi
