#!/bin/bash
# IMPORTANT: 
# To make this block work, you have to edit your block-command.
# It should point to this file. For best performance, only use it on blocks
# you want to monitor.
# 
# Example:
# command=~/.i3-blocks/blocks/notifier $BLOCK_NAME

BLOCK_NAME="${1}"

if [[ "${BLOCK_NAME}" == "" ]] || [[ "${BLOCK_NAME}" == "notifier" ]]; then
  exit 1
fi

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

if [[ ! -f "${BLOCK_PATH}" ]]; then
  exit 1
fi

BLOCK_OUT=$(bash "${BLOCK_PATH}")
BLOCK_URGENT=$?

# Nothing to display.
if [[ "${BLOCK_OUT}" == "" ]]; then
  exit "${BLOCK_URGENT}"
fi

NOTIFY=0

NOTIFY_CHANGES="weather display"
NOTIFY_ALWAYS=""
NOTIFY_NEVER="launcher playerctl scroll"

if [[ "${NOTIFY_NEVER}" != *${BLOCK_NAME}* ]]; then
  BLOCK_UCFIRST=$(echo "${BLOCK_NAME}" | head -c 1 | tr [a-z] [A-Z]; echo "${BLOCK_NAME}" | tail -c +2)

  CHECK_SUM=$(echo "${BLOCK_NAME}-${BLOCK_INSTANCE}" | md5sum | awk '{print $1}')
  CACHE_FILE="/tmp/.notify-cache-${CHECK_SUM}"

  BLOCK_DESC=$(echo "${BLOCK_OUT}" | head -n 1)
  BLOCK_VALUE=$(echo "${BLOCK_OUT}" | tail -n 1)
  BLOCK_ICON=""

  if [[ "${NOTIFY_ALWAYS}" == *${BLOCK_NAME}* ]]; then
    NOTIFY=1
  elif [[ "${BLOCK_URGENT}" -gt 0 ]]; then
    NOTIFY=1
  fi

  VALUE_CHANGED=0
  HAS_CACHEFILE=0
  if [[ -f "${CACHE_FILE}" ]]; then
    HAS_CACHEFILE=1
    CACHED_VALUE=$(cat "${CACHE_FILE}")
    #MODIFIED_TIME=$(stat -c '%Y' "${CACHE_FILE}")
    #CURRENT_TIME=$(date +%s)
    #ELAPSED_TIME=$((MODIFIED_TIME - CURRENT_TIME))
    if [[ "${BLOCK_VALUE}" != "${CACHED_VALUE}" ]]; then
      VALUE_CHANGED=1
      if [[ "${NOTIFY_CHANGES}" == *${BLOCK_NAME}* ]]; then
        NOTIFY=1
      fi
    fi
  elif [[ "${NOTIFY_CHANGES}" == *${BLOCK_NAME}* ]]; then
    NOTIFY=1
  fi

  if [[ "${NOTIFY}" -eq 1 ]]; then
    if [[ "${BLOCK_NAME}" == "audio" ]]; then
      BLOCK_MESSAGE="Audio level changed to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "bandwidth" ]]; then
      BLOCK_MESSAGE="Traffic went to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "battery" ]]; then
      BLOCK_MESSAGE="Energy changed to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "countdown" ]]; then
      BLOCK_MESSAGE="Remaining time is now ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "cpu" ]]; then
      BLOCK_MESSAGE="CPU utilization is now ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "feed" ]]; then
      BLOCK_MESSAGE="Newest article is ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "mail" ]]; then
      BLOCK_MESSAGE="Unread emails went to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "memory" ]]; then
      BLOCK_MESSAGE="Memory utilization is now ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "packages" ]]; then
      BLOCK_MESSAGE="${BLOCK_VALUE} packages are now ready to update."
    elif [[ "${BLOCK_NAME}" == "space" ]]; then
      BLOCK_MESSAGE="Space utilization is now ${BLOCK_INSTANCE}."
    elif [[ "${BLOCK_NAME}" == "ssid" ]]; then
      BLOCK_MESSAGE="SSID changed to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "temperature" ]]; then
      BLOCK_MESSAGE="Sensor temperature changed to ${BLOCK_VALUE}."
    elif [[ "${BLOCK_NAME}" == "trash" ]]; then
      BLOCK_MESSAGE="${BLOCK_VALUE} items are now in the trash."
    else
      BLOCK_MESSAGE="${BLOCK_UCFIRST} is now ${BLOCK_VALUE}."
    fi

    if [[ "${BLOCK_NAME}" == "audio" ]]; then
      BLOCK_ICON="audio-volume-high-symbolic"
    elif [[ "${BLOCK_NAME}" == "bandwidth" ]]; then
      BLOCK_ICON="network-wireless-acquiring-symbolic"
    elif [[ "${BLOCK_NAME}" == "battery" ]]; then
      BLOCK_ICON="battery-symbolic"
    elif [[ "${BLOCK_NAME}" == "bluetooth" ]]; then
      BLOCK_ICON="bluetooth-active-symbolic"
    elif [[ "${BLOCK_NAME}" == "brightness" ]]; then
      BLOCK_ICON="display-brightness-symbolic"
    elif [[ "${BLOCK_NAME}" == "cmus" ]]; then
      BLOCK_ICON="emblem-music-symbolic"
    elif [[ "${BLOCK_NAME}" == "countdown" ]]; then
      BLOCK_ICON="document-open-recent-symbolic"
    elif [[ "${BLOCK_NAME}" == "cpu" ]]; then
      BLOCK_ICON="indicator-cpufreq"
    elif [[ "${BLOCK_NAME}" == "datetime" ]]; then
      BLOCK_ICON="system-run-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "device" ]]; then
      BLOCK_ICON="system-run-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "display" ]]; then
      BLOCK_ICON="video-display-symbolic"
    elif [[ "${BLOCK_NAME}" == "feed" ]]; then
      BLOCK_ICON="folder-documents-symbolic"
    elif [[ "${BLOCK_NAME}" == "firewall" ]]; then
      BLOCK_ICON="changes-prevent-symbolic.svg" # TODO
    elif [[ "${BLOCK_NAME}" == "ip-address" ]]; then
      BLOCK_ICON="avatar-default-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "keystate" ]]; then
      BLOCK_ICON="format-text-underline-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "launcher" ]]; then
      BLOCK_ICON="system-run-symbolic"
    elif [[ "${BLOCK_NAME}" == "load" ]]; then
      BLOCK_ICON="indicator-cpufreq" # TODO
    elif [[ "${BLOCK_NAME}" == "locale" ]]; then
      BLOCK_ICON="format-text-larger-symbolic"
    elif [[ "${BLOCK_NAME}" == "mail" ]]; then
      BLOCK_ICON="mail-send-symbolic"
    elif [[ "${BLOCK_NAME}" == "memory" ]]; then
      BLOCK_ICON="drive-harddisk-solidstate-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "microphone" ]]; then
      BLOCK_ICON="audio-input-microphone-symbolic"
    elif [[ "${BLOCK_NAME}" == "network" ]]; then
      BLOCK_ICON="network-wireless-connected-symbolic"
    elif [[ "${BLOCK_NAME}" == "packages" ]]; then
      BLOCK_ICON="view-refresh-symbolic"
    elif [[ "${BLOCK_NAME}" == "playerctl" ]]; then
      BLOCK_ICON="emblem-music-symbolic"
    elif [[ "${BLOCK_NAME}" == "process" ]]; then
      BLOCK_ICON="process-stop-symbolic"
    elif [[ "${BLOCK_NAME}" == "space" ]]; then
      BLOCK_ICON="drive-harddisk-solidstate-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "ssid" ]]; then
      BLOCK_ICON="system-users-symbolic"
    elif [[ "${BLOCK_NAME}" == "temperature" ]]; then
      BLOCK_ICON="object-inverse" # TODO
    elif [[ "${BLOCK_NAME}" == "trash" ]]; then
      BLOCK_ICON="user-trash-symbolic"
    elif [[ "${BLOCK_NAME}" == "usb" ]]; then
      BLOCK_ICON="drive-removable-media-usb-symbolic-1"
    elif [[ "${BLOCK_NAME}" == "vpn" ]]; then
      BLOCK_ICON="dialog-password-symbolic" # TODO
    elif [[ "${BLOCK_NAME}" == "weather" ]]; then
      BLOCK_ICON="weather-few-clouds-symbolic"
    elif [[ "${BLOCK_NAME}" == "webcam" ]]; then
      BLOCK_ICON="camera-web-symbolic"
    elif [[ "${BLOCK_NAME}" == "window" ]]; then
      BLOCK_ICON="view-dual-symbolic" # TODO
    fi
    
    if [[ "${BLOCK_URGENT}" -gt 0 ]]; then
      notify-send -u critical -t 5000 -i "${BLOCK_ICON}" -a "i3block-${BLOCK_NAME}" "${BLOCK_UCFIRST}" "${BLOCK_MESSAGE}"
    else
      notify-send -u normal -t 3500 -i "${BLOCK_ICON}" -a "i3block-${BLOCK_NAME}" "${BLOCK_UCFIRST}" "${BLOCK_MESSAGE}"
    fi
  fi

  if [[ "${VALUE_CHANGED}" -eq 1 ]] || [[ "${HAS_CACHEFILE}" -eq 0 ]]; then
    echo "${BLOCK_VALUE}" > "${CACHE_FILE}"
  fi
fi

echo "${BLOCK_OUT}"
exit "${BLOCK_URGENT}"