#!/bin/bash

# ANSI colours
CYAN='\033[0;36m'
DARK_CYAN='\033[1;36m' 
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'

echo -e "${CYAN}
 ██████ ██    ██  ██████  ██████     ███████ ██   ██ 
██       ██  ██  ██      ██    ██    ██      ██   ██ 
██        ████   ██      ██    ██    ███████ ███████ 
██         ██    ██      ██    ██         ██ ██   ██ 
 ██████    ██     ██████  ██████  ██ ███████ ██   ██ 
${DARK_CYAN}
                - CYBER COMPRESSOR -
 - // https://git.cyberwa.re/revengeday/cyco // -
${NC}"

# ImageMagick check
if ! command -v mogrify &> /dev/null; then
    echo -e "${RED}Oooooooops!${NC}"
    echo -e "${RED}ImageMagick not found.${NC}"
    echo -e "${YELLOW}Install using Homebrew:${NC}"
    echo -e "${YELLOW}1. /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"${NC}"
    echo -e "${YELLOW}2. brew install imagemagick${NC}"
    exit 1
fi

USE_CLI=false
CHOICE=""
FOLDER=""
FILE=""
QUALITY=""

# Argument Parsing
while [[ "$#" -gt 0 ]]; do
  case $1 in
    --cli) USE_CLI=true ;;
    --f) CHOICE="f"; FOLDER="$2"; shift ;;
    --i) CHOICE="i"; FILE="$2"; shift ;;
    --quality) QUALITY="$2"; shift ;;
    *) echo -e "${RED}Unknown parameter passed: $1${NC}"; exit 1 ;;
  esac
  shift
done

# If not in CLI mode, prompt for input
if [[ "$USE_CLI" == false ]]; then
  echo -e "${YELLOW}Would you like to compress a folder or a single image? (f/i)${NC}"
  read -p "Enter 'f' for folder or 'i' for image: " CHOICE
fi

# Handle folder or file selection
if [[ "$CHOICE" == "f" ]]; then
  if [[ -z "$FOLDER" ]]; then
    if [[ "$USE_CLI" == true ]]; then
      read -p "Enter the path to the folder: " FOLDER
    else
      echo -e "${YELLOW}Select a folder with images to compress.${NC}"
      FOLDER=$(osascript -e 'tell application "Finder" to POSIX path of (choose folder)')
    fi
  fi

  if [[ -z "$FOLDER" ]]; then
    echo -e "${RED}No folder selected.${NC}"
    exit 1
  fi

  shopt -s nullglob
  FILES=("$FOLDER"/*.{jpg,jpeg,png,JPG,JPEG,PNG})
  shopt -u nullglob

  if [ ${#FILES[@]} -eq 0 ]; then
    echo -e "${YELLOW}Yikes, no images found in the folder.${NC}"
    exit 0
  fi
elif [[ "$CHOICE" == "i" ]]; then
  if [[ -z "$FILE" ]]; then
    if [[ "$USE_CLI" == true ]]; then
      read -p "Enter the path to the image: " FILE
    else
      echo -e "${YELLOW}Select an image to compress.${NC}"
      FILE=$(osascript -e 'tell application "Finder" to POSIX path of (choose file)')
    fi
  fi

  if [[ -z "$FILE" ]]; then
    echo -e "${RED}No image selected.${NC}"
    exit 1
  fi

  FILES=("$FILE")
else
  echo -e "${RED}Invalid choice. Please enter 'f' or 'i'.${NC}"
  exit 1
fi

# Quality input
if [[ -z "$QUALITY" ]]; then
  read -p "Enter quality (1-100): " QUALITY
fi

if ! [[ "$QUALITY" =~ ^[1-9][0-9]?$|^100$ ]]; then
  echo -e "${RED}Invalid quality. Enter a number 1-100.${NC}"
  exit 1
fi

# Compression
for file in "${FILES[@]}"; do
  echo -e "${YELLOW}Compressing $file with quality $QUALITY...${NC}"
  mogrify -quality "$QUALITY" "$file" && \
  echo -e "${GREEN}Compressed: $file${NC}" || \
  echo -e "${RED}Failed: $file${NC}"
done

echo -e "${GREEN}Compression done.${NC}"