#!/usr/bin/env bash

WEBHOOK_URL="https://open.larksuite.com/open-apis/bot/v2/hook/3acf274a-1828-494b-a4a2-a3185f5e466d"

set -u

EVENT=""
SERVICE_NAME=""
NAMESPACE=""
IMAGE_REPO=""
IMAGE_TAG=""
VERSION_TAG=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --event)
      EVENT="$2"
      shift 2
      ;;
    --service-name)
      SERVICE_NAME="$2"
      shift 2
      ;;
    --namespace)
      NAMESPACE="$2"
      shift 2
      ;;
    --image-repo)
      IMAGE_REPO="$2"
      shift 2
      ;;
    --image-tag)
      IMAGE_TAG="$2"
      shift 2
      ;;
    --version-tag)
      VERSION_TAG="$2"
      shift 2
      ;;
    *)
      echo "Unknown argument: $1"
      exit 1
      ;;
  esac
done

escape_json() {
  python3 -c 'import json,sys; print(json.dumps(sys.stdin.read())[1:-1])'
}

commit_message() {
  git log -1 --pretty=%s
}

header_color() {
  case "$CIRCLE_BRANCH" in
    dev)
      echo "green"
      ;;
    staging)
      echo "blue"
      ;;
    prod)
      echo "red"
      ;;
    onprem-dev)
      echo "orange"
      ;;
    onprem-release)
      echo "purple"
      ;;
    *)
      echo "grey"
      ;;
  esac
}

build_deploy_payload() {
  local color="$1"
  local short_sha="${CIRCLE_SHA1:0:8}"
  local message
  local content
  message=$(commit_message)
  content=$(printf '**触发人**: %s\n**分支**: %s\n**Job**: %s\n**服务**: %s\n**命名空间**: %s\n**Commit**: %s\n**完整 SHA**: %s\n**提交信息**: %s' "$CIRCLE_USERNAME" "$CIRCLE_BRANCH" "$CIRCLE_JOB" "$SERVICE_NAME" "$NAMESPACE" "$short_sha" "$CIRCLE_SHA1" "$message" | escape_json)

  cat <<EOF
{
  "msg_type": "interactive",
  "card": {
    "config": {
      "wide_screen_mode": true
    },
    "header": {
      "template": "${color}",
      "title": {
        "tag": "plain_text",
        "content": "catalog-agent 部署成功"
      }
    },
    "elements": [
      {
        "tag": "div",
        "text": {
          "tag": "lark_md",
          "content": "${content}"
        }
      },
      {
        "tag": "action",
        "actions": [
          {
            "tag": "button",
            "text": {
              "tag": "plain_text",
              "content": "查看构建"
            },
            "type": "primary",
            "url": "${CIRCLE_BUILD_URL}"
          }
        ]
      }
    ]
  }
}
EOF
}

build_docker_hub_payload() {
  local color="$1"
  local short_sha="${CIRCLE_SHA1:0:8}"
  local message
  local content
  message=$(commit_message)
  content=$(printf '**触发人**: %s\n**分支**: %s\n**Job**: %s\n**镜像**: %s:%s\n**版本标签**: %s\n**Commit**: %s\n**完整 SHA**: %s\n**提交信息**: %s' "$CIRCLE_USERNAME" "$CIRCLE_BRANCH" "$CIRCLE_JOB" "$IMAGE_REPO" "$IMAGE_TAG" "$VERSION_TAG" "$short_sha" "$CIRCLE_SHA1" "$message" | escape_json)

  cat <<EOF
{
  "msg_type": "interactive",
  "card": {
    "config": {
      "wide_screen_mode": true
    },
    "header": {
      "template": "${color}",
      "title": {
        "tag": "plain_text",
        "content": "catalog-agent Docker Hub 推送成功"
      }
    },
    "elements": [
      {
        "tag": "div",
        "text": {
          "tag": "lark_md",
          "content": "${content}"
        }
      },
      {
        "tag": "action",
        "actions": [
          {
            "tag": "button",
            "text": {
              "tag": "plain_text",
              "content": "查看构建"
            },
            "type": "primary",
            "url": "${CIRCLE_BUILD_URL}"
          }
        ]
      }
    ]
  }
}
EOF
}

send_payload() {
  local payload="$1"
  curl -X POST -H "Content-Type: application/json" -d "$payload" "$WEBHOOK_URL" || echo "WARN: Lark notify failed"
}

case "$EVENT" in
  deploy)
    case "$CIRCLE_BRANCH" in
      dev|staging|prod|onprem-dev)
        send_payload "$(build_deploy_payload "$(header_color)")"
        ;;
    esac
    ;;
  docker_hub)
    if [[ "$CIRCLE_BRANCH" == "onprem-release" && "$IMAGE_TAG" == "latest" ]]; then
      send_payload "$(build_docker_hub_payload "$(header_color)")"
    fi
    ;;
  *)
    echo "Unsupported event: $EVENT"
    exit 1
    ;;
esac
