=begin
 ******************************************************************************
 * Copyright 1990-2024 MBARI
 * MBARI Proprietary Information. All rights reserved.
 ******************************************************************************
 * Summary  : Process the latest SES image
 * Filename : behavior.rb
 * Author   : Henthorn
 * Project  : SES
 * Version  : 1
 * Created  : Apr 2024
 ******************************************************************************
=end

##
# Refer to Rover-code home directory as base
#
require "#{ENV['ROVER_HOME']}/utils/rover_environment"
#

require 'utils/misc'
require "control/behavior"
require 'device/rover'

##########
# ProcessImage class.
# Runs image processing code on the latest image, saving the results
# in a image data file.
class ProcessImage < Behavior
  include RoverTime

  attr_reader :name

  #####
  # Accept a behavior element and extract parameters
  #
  def initialize(p_behavior_element)
    super

    load_attrs(p_behavior_element)
  end
  #####

  ##
  # Iterate over attributes in the element, extract parameters.
  #
  def load_attrs(p_behavior_element)
    super

    attrs = p_behavior_element.attributes
    attrs.keys.each do |k|
      puts("Unrecognized attr: #{k}")

    end
  end
  #####

  ##
  # Initialize behavior and make ready for execution.
  # Should be called just before executing the behavior.
  #
  def init(p_seq_number=1)   # Where in the plan behavior sequence I am
    syslog("_ #{@name} init")

    # Throw an exception if @my_thread != nil and @my_thread.alive?
    # Perform initialization procedures

    @state = READY
    @b_status = "ready"
  end
  #####


  #####
  # Resume after hibernation
  #
  def resume_init()
    syslog( "_ resuming from hibernating")
    @b_status = "ready"
  end
  #####


  #####
  # Behaviors report on their status.
  #
  def status()
    syslog(@b_status)
    return @b_status
  end
  #####


  #####
  # Clean-up method. Use this to restore state of vehicle
  # after behavior execution, whether successful or not.
  #
  def clean_up
  end
  #####

  #####
  # Run the execute method in a separate thread, return the thread object
  #
  def run
    self.execute
  end

  ##
  # Execute the behavior
  #
  def execute
    # Execute if ready.
    # This need not be enforced by behaviors, just an example.
    # Alternatively, you could run init method if not ready.
    #
    unless (@state == READY)
      syslog("_ #{@name} not ready to execute")
    end

    syslog("_ #{@name} execute")
    @b_status = "running"
    @state = EXECUTING

    begin
      # Perform the behavior
    rescue
      # Handle exceptions
    ensure
      self.clean_up
      @state = FINISHED
      @b_status = "finished"
    end
  end
  #####


  ##
  # Abort the behavior
  #
  def abort
    syslog("_ #{@name} abort")
    if (@state == EXECUTING)
      @state = ABORTING

      # Perform aborting procedure
      @my_thread.kill if @my_thread
    else
      syslog("_ #{@name} not executing, aborting...")
    end
    @state = FINISHED
    @b_status = "aborted"
  end
  #####


  #####
  # My data files go in the plan data dir by default.
  # Specialized behaviors are free to point to new or other directories.
  #
  def data_home=(p_seq_num)
    @data_home = DataLog.plan_data_home
  end
  #####

  #####
  def data_home
    return @data_home
  end
  #####

end


##
# Standalone unit test (recommended)
#
if __FILE__ == $0

  if "usage" == ARGV[0]
    puts(Behavior.usage)
    exit(0)
  end

  require 'rexml/document'
  require 'control/sample_behavior'

  include SyslogWriter

  # Test code here
  puts("Done")
end
