  #!/bin/bash
local UI_VERBOSE_YARN=""
local UI_VERBOSE_LEARNA=""

ui::description() {
  echo "Syndesis UI tasks"
}

ui::usage() {
  cat <<"EOT"
    By default the 'ui' does nothing unless you pass one or more of the following parameters:

    --install           Install UI package dependencies
    --lint              Run lint task
    --build             Build the UI code
    --run-tests         Run the tests task
    --watch-tests       Run the tests and watch for changes
    --serve             Serve the UI (defaults to staging, use --minishift or --local to change this) and watch for changes
    --local             When serving, run against a local Syndesis server
    --minishift         Involve minishift as needed
-p  --project           Use the specified openshift project
    --nuke-everything   Use with --minishift, start or reset the minishift VM
    --verbose           Enable verbose logging
EOT
}

ui::run() {
  cd_to_ui_dir
  do_things
  local exit_code=$?
  cd_back
  exit ${exit_code}
}

cd_to_ui_dir() {
  pushd $(appdir)/ui-react > /dev/null 2>&1
  echo "note: changing directory to $(pwd)"
}

cd_back() {
  popd
  echo "note: changing directory back to $(pwd)" 2>&1
}

# SIGINT handler
killall() {
    trap '' INT TERM
    echo -e "\nShutting down..."
    kill -TERM 0
    wait
    if [ $(hasflag --minishift) ]; then
      if [ $(hasflag --serve) ]; then
        yarn watch:app:minishift:restore
      fi
    fi
    cd_back
    exit 0
}

do_things() {
  if [ $(hasflag --verbose) ]; then
    UI_VERBOSE_YARN="--verbose"
    UI_VERBOSE_LEARNA="--loglevel verbose"
  fi

  # in here we assume we're in the 'ui-react' directory
  local project=$(readopt --project -p)
  local result=''

  if [ -z "${project}" ]; then
   project='syndesis'
  fi

  # Install npm package dependencies
  if [ $(hasflag --install) ]; then
    echo "Installing UI package dependencies"
    # Use --force to ensure any native libs are rebuilt
    yarn install --force $UI_VERBOSE_YARN
    if [ $? -ne 0 ]; then
      check_error "Error installing UI package dependencies"
      return 1
    fi
  fi

  # Run linter
  if [ $(hasflag --lint) ]; then
    yarn lint $UI_VERBOSE_LEARNA
    if [ $? -ne 0 ]; then
      check_error "Error running lint task"
      return 1
    fi
  fi

  if [ $(hasflag --build) ]; then
    yarn build $UI_VERBOSE_LEARNA
  fi

  if [ $(hasflag --run-tests) ]; then
    yarn test $UI_VERBOSE_LEARNA
    if [ $? -ne 0 ]; then
      check_error "Error running tests"
      return 1
    fi
  fi

  if [ $(hasflag --watch-tests) ]; then
    trap 'killall' INT
    yarn test --watch $UI_VERBOSE_LEARNA &
    cat
    return 0
  fi

  # Set up minishift instance or environment as needed
  if [ $(hasflag --minishift) ]; then
    if [ $(hasflag --nuke-everything) ]; then
      ../../tools/bin/syndesis minishift --full-reset
      if [ $? -ne 0 ]; then
        check_error "Error creating minishift instance"
        return 1
      fi
      eval $(minishift oc-env)
      result=$(oc login -u developer -p pass > /dev/null)
      check_error "$result"
      ../../tools/bin/syndesis minishift --install --dev -p $project
      if [ $? -ne 0 ]; then
        check_error "Error installing Syndesis into minishift"
        return 1
      fi
      result=$(oc project $project)
      check_error "$result"
    else
      echo "Logging into openshift"
      eval $(minishift oc-env)
      result=$(oc login -u developer -p pass > /dev/null)
      check_error "$result"
      if [ -n "${project}" ]; then
        result=$(oc project $project)
        check_error "$result"
      fi
    fi
  fi

  # Start up a UI development server as appropriate
  if [ $(hasflag --serve) ]; then
    trap 'killall' INT
    if [ $(hasflag --local) ]; then
      echo -e "\nRunning against a local syndesis server"
      yarn start:local $UI_VERBOSE &
    elif [ $(hasflag --minishift) ]; then
      echo -e "\nRunning against minishift"
      yarn watch:app:minishift $UI_VERBOSE_LEARNA &
    else
      echo -e "\nRunning against the staging environment"
      if [ -z ${BACKEND+x} ]; then
        export BACKEND=https://syndesis-staging.b6ff.rh-idev.openshiftapps.com
      fi
      yarn watch:app:proxy $UI_VERBOSE_LEARNA &
    fi
    # block
    cat
  fi

  echo "Done!"
  return 0;
}
