#!/bin/bash

start_webapp_dev_env () {
    set -e

    start_nodes

    load_native_asset
    mint_usdt

    start_bobtimus

    start_webapp
}

start_extension_dev_env () {
    set -e

    start_nodes

    load_native_asset
    mint_usdt

    build_webapp
    start_bobtimus
}

stop_dev_env () {
    stop_nodes
    stop_bobtimus
}

start_nodes () {
    set -e

    # start elementsd, esplora (et al)
    nigiri start --liquid

    # wait for everything to be started up
    sleep 2
}

load_native_asset () {
    # get native asset ID.
    local native_asset_id=$(nigiri rpc --liquid dumpassetlabels | jq -r '.bitcoin')
    if [ "$native_asset_id" = "null" ]; then
      echo "Bitcoin asset id not found."
      exit 1
    fi

    echo "NATIVE_ASSET_ID=$native_asset_id"
    echo "$native_asset_id" > .native_asset_id
}

mint_usdt () {
    set -e

    local -r amount=1000000
    usdt_asset_id=$(docker exec liquid elements-cli -rpcuser=admin1 -rpcpassword=123 -rpcport=18884 issueasset $amount 1  | jq -r '.asset')

    # mine a block
    address=$(nigiri rpc --liquid getnewaddress)
    docker exec liquid elements-cli -rpcuser=admin1 -rpcpassword=123 -rpcport=18884 generatetoaddress 1 $address &> /dev/null

    # get usdt asset ID.
    if [ "$usdt_asset_id" = "null" ]; then
      echo "Usdt asset id not found."
      exit 1
    fi

    echo "USDT_ASSET_ID=$usdt_asset_id"
    echo "$usdt_asset_id" > .usdt_asset_id
}

start_bobtimus () {
    if [ -f ".bobtimus" ]; then
        echo "Are you sure Bobtimus is not already up?"
        exit 1
    fi

    if [ ! -f ".usdt_asset_id" ]; then
        echo "Usdt asset id not configured. Did you run mint_usdt?"
        exit 1
    fi

    usdt_asset_id=$(cat .usdt_asset_id)

    set -e

    __ensure_log_dir
    __read_env_file

    cargo build --bin bobtimus --features faucet

    local -r elementsd_rpc_user="admin1"
    local -r elementsd_rpc_password="123"

    RUST_LOG=debug,hyper=info,reqwest=info cargo run --bin bobtimus --features faucet -- \
            start \
            --http 127.0.0.1:3030 \
            --elementsd http://$elementsd_rpc_user:$elementsd_rpc_password@127.0.0.1:$LIQUID_NODE_PORT \
            --usdt $usdt_asset_id \
            --db-file="./.bobtimus.sqlite" > $log_dir/bobtimus 2>&1 &
    bobtimus_pid=$!

    sleep 1
    curl --fail http://127.0.0.1:3030 &> /dev/null
    if [ $? -ne 0 ]; then
        set +e
        pkill bobtimus_pid
        set -e

        echo "Failed to start Bobtimus"
        exit 1
    fi

    # save bobtimus pid to file for future teardown
    echo $bobtimus_pid > .bobtimus

    echo "Started bobtimus with pid $bobtimus_pid"
}

start_webapp () {
    set -e

    build_webapp

    cd waves
    yarn run start
}

build_webapp () {
    set -e

    __read_env_file

    cd waves
    yarn install
    export REACT_APP_BLOCKEXPLORER_URL="http://localhost:$LIQUID_ESPLORA_PORT"

    yarn run build

    cd ..
}


start_extension () {
    set -e

    cd extension
    yarn run dev
}

build_extension () {
    set -e

    if [ ! -f ".native_asset_id" ]; then
        echo "Native asset id not configured."
        exit 1
    fi

    __read_env_file

    cd extension
    yarn install
    export NATIVE_ASSET_ID=$(cat ../.native_asset_id)
    export USDT_ASSET_ID=$(cat ../.usdt_asset_id)
    export CHAIN=ELEMENTS
    echo "export NATIVE_ASSET_ID=$NATIVE_ASSET_ID"
    echo "export USDT_ASSET_ID=$USDT_ASSET_ID"
    echo "export CHAIN=$CHAIN"

    yarn run build

    cd ..
}

stop_nodes () {
    nigiri stop --delete
    rm .native_asset_id
    rm .usdt_asset_id
}

stop_bobtimus () {
    bobtimus_pid=$(cat .bobtimus)

    if [ $? -ne 0 ]; then
        echo "Are you sure Bobtimus is still running?"
        exit 1
    fi

    kill -9 $bobtimus_pid
    echo "Terminated bobtimus with pid $bobtimus_pid"

    if [ $? -ne 0 ]; then
        echo "Bobtimus is not running"
    fi

    rm .bobtimus
}

restart_bobtimus () {
  stop_bobtimus
  start_bobtimus
}

__read_env_file () {
    export $(cat ~/.nigiri/.env | xargs)
}

__ensure_log_dir () {
    readonly log_dir="./.log"
    mkdir -p $log_dir
}

"$@"