#
# Copyright (c) 2012-2013,2016,2019 LAAS/CNRS
# All rights reserved.
#
# Permission to use, copy, modify, and distribute this software for any purpose
# with or without   fee is hereby granted, provided   that the above  copyright
# notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS  SOFTWARE INCLUDING ALL  IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR  BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR  ANY DAMAGES WHATSOEVER RESULTING  FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION,   ARISING OUT OF OR IN    CONNECTION WITH THE USE   OR
# PERFORMANCE OF THIS SOFTWARE.
#
#                                            Anthony Mallet on Fri Apr 13 2012
#

namespace eval request {
  variable requests	[dict create]

  # --- new ----------------------------------------------------------------
  #
  proc new { module activity params } {
    variable requests

    if {[dict exists $requests "${module}::$activity"]} {
      error "activity ${module}::$activity exists"
    }

    set s [session::get $params]
    dict set requests "${module}::$activity" [dict create session $s]
    return
  }


  # --- status -------------------------------------------------------------
  #
  proc status { module activity } {
    variable requests
    set id "${module}::$activity"

    if {[catch {dict get $requests $id data} json]} {
      if {![dict exists $requests $id]} {
        error "activity $id does not exist"
      }
      set e [list]
      lappend e [subst {"id":"$id"}]
      lappend e [subst {"status":"none"}]
      return "{[join $e ,]}"
    }
    return $json
  }


  # --- abort --------------------------------------------------------------
  #
  proc abort { module activity } {
    variable requests
    set id "${module}::$activity"

    if {[catch {dict get $requests $id} info]} {
      error "activity $id does not exist"
    }
    return [genomix::client::$module abort $activity]
  }


  # --- clean --------------------------------------------------------------
  #
  proc clean { module activity } {
    variable requests
    set id "${module}::$activity"

    if {[catch {dict get $requests $id} info]} {
      error "activity $id does not exist"
    }
    dict unset requests $id
    return [genomix::client::$module clean $activity]
  }


  # --- clean-all ----------------------------------------------------------
  #
  proc clean-all { module params } {
    variable requests

    foreach id [dict keys $requests ${module}::*] {
      infox "cleaning $id" 1
      scan $id {%*[^:]::%d} activity
      catch {genomix::client::$module clean $activity}
      dict unset requests $id
    }

    return
  }


  # --- event --------------------------------------------------------------
  #
  proc event { module activity status jsonerr output} {
    variable requests
    set id "${module}::$activity"

    if {[catch {dict get $requests $id} info]} {
      infox "discarding $status reply for $id"
      if {$status != "sent"} {
        genomix::client::$module clean $activity
      }
      return
    }

    set e [list]
    lappend e [subst {"id":"$id"}]
    lappend e [subst {"status":"$status"}]
    if {$jsonerr != ""} {
      lappend e [subst {"errmsg":$jsonerr}]
    }
    if {$output != ""} {
      lappend e [subst {"output":$output}]
    }
    set json "{[join $e ,]}"

    dict set requests $id data $json
    if {[catch {
      session::event [dict get $info session] request $json
    } m]} {
      warnx "not pushing event for $id: $m"
    }
  }
}
