#!/usr/bin/env bash
set -e

# Colors and formatting
GREEN='\033[1;32m'
BLUE='\033[1;34m'
BOLD='\033[1m'
NC='\033[0m' # No Color

# Temporary file for shell environment lines
if [ -z "${SHELL_ENV_FILE:-}" ]; then
  SHELL_ENV_FILE=$(mktemp)
  # delete the file after the script is finished
  trap 'rm -f "$SHELL_ENV_FILE"' EXIT
fi

function ok() {
  echo -e "${BOLD}${GREEN}[OK]${NC} ${BLUE}$1${NC} ${BOLD}${2:-}${NC}"
}

function install::jenv() {
  if ! command -v jenv &>/dev/null; then
    echo "Installing jenv for Java version management..."
    brew install jenv

    # Initialize JEnv
    mkdir -p ~/.jenv/versions

    # shellcheck disable=SC2016
    echo 'export PATH="$(brew --prefix jenv)/bin:$PATH"' >>"$SHELL_ENV_FILE"
    # shellcheck disable=SC2016
    echo 'eval "$(jenv init -)"' >>"$SHELL_ENV_FILE"
  fi

  export PATH="$(brew --prefix jenv)/bin:$PATH"

  eval "$(jenv init -)"
  jenv enable-plugin export # exports JAVA_HOME automatically in shells

  ok "jenv" "$(jenv --version | cut -d' ' -f2)"
}

function install::zulu_jdk() {
  local java_version="17.0.16"
  if ! jenv versions | grep -q "${java_version}"; then
    echo "Installing Java OpenJDK ${java_version} (Temurin)..."
    brew install --cask temurin@17

    # Add Zulu 17 to JEnv
    jenv add "$(/usr/libexec/java_home -v ${java_version})"
  fi

  jenv global ${java_version}

  ok "Java OpenJDK (Temurin)" "${java_version}"
}

function install::rbenv() {
  if ! command -v rbenv &>/dev/null; then
    echo "Installing rbenv for Ruby version management..."
    brew install rbenv || echo ""

    # shellcheck disable=SC2016
    echo 'export PATH="$(brew --prefix rbenv)/bin:$PATH"' >>"$SHELL_ENV_FILE"
    # shellcheck disable=SC2016
    echo 'eval "$(rbenv init -)"' >>"$SHELL_ENV_FILE"
  fi

  export PATH="$(brew --prefix rbenv)/bin:$PATH"
  eval "$(rbenv init -)"

  ok "rbenv" "$(rbenv --version | cut -d' ' -f2)"
}

function install::ruby() {
  # Read Ruby version from .ruby-version file
  RUBY_VERSION=$(cat "./.ruby-version" | tr -d '[:space:]')
  # Install Ruby version from .ruby-version if not already installed
  if ! rbenv versions | grep -q "${RUBY_VERSION}"; then
    echo "Installing Ruby ${RUBY_VERSION}..."
    rbenv install "${RUBY_VERSION}"
  fi

  ok "Ruby" "${RUBY_VERSION}"
}

function install::bundler() {
  if ! command -v bundle &>/dev/null; then
    echo "Installing Bundler..."
    gem install bundler
  fi

  ok "Ruby bundler" "$(bundle -v | cut -d' ' -f3)"
}

function install::bundle_dependencies() {
  # check if all gems are installed
  if ! bundle check &>/dev/null; then
    echo "Installing Ruby gems..."
    bundle install
  fi

  ok "Ruby gems"
}

function install::shfmt() {
  if ! command -v shfmt &>/dev/null; then
    echo "Installing shfmt..."
    brew install shfmt
  fi

  ok "shfmt" "$(shfmt --version | cut -d' ' -f2)"
}

function install::pre_commit() {
  if ! command -v pre-commit &>/dev/null; then
    echo "Installing pre-commit..."
    brew install pre-commit
  fi

  # Check if pre-commit hooks are already installed
  if [ ! -f ".git/hooks/pre-commit" ]; then
    echo "Setting up pre-commit hooks..."
    pre-commit install
  fi

  ok "pre-commit" "$(pre-commit --version | cut -d' ' -f2)"
}

function install::ktlint() {
  ok "ktlint" "$(./tools/ktlint.sh --version | cut -d' ' -f3)"
}

function install::detekt() {
  ok "detekt" "$(./tools/detekt.sh --version | cut -d' ' -f2)"
}

function main() {
  echo -e "${GREEN}------------------------------------${NC}"
  echo -e "${GREEN}Android Development Setup           ${NC}"
  echo -e "${GREEN}------------------------------------${NC}"

  # Java environment setup
  install::jenv
  install::zulu_jdk

  # Ruby environment setup
  install::rbenv
  install::ruby
  install::bundler
  install::bundle_dependencies

  # Development tools setup
  install::pre_commit
  install::shfmt
  install::ktlint
  install::detekt

  if [[ "$(git remote get-url origin 2>/dev/null)" == *"suno-ai/app-android"* ]]; then
    echo -e "\n${GREEN}✅ Setup Completed! ${NC}"

    if [ -f "$SHELL_ENV_FILE" ] && [ -s "$SHELL_ENV_FILE" ]; then
      echo -e "\n${GREEN}Please add these lines to your shell's RC file (${SHELL:-bash}):"
      echo -e "${GREEN}----------------------------------------${NC}"
      cat "$SHELL_ENV_FILE"
      echo -e "${GREEN}----------------------------------------${NC}"
      echo -e "${GREEN}Then restart your shell or source your RC file${NC}\n"
      rm -f "$SHELL_ENV_FILE"
    fi
  fi
}

# Execute main function
main
