# frozen_string_literal: true

# Requirements
# =======================================================================

# Stdlib
# -----------------------------------------------------------------------

# Deps
# -----------------------------------------------------------------------
require 'plist'
require 'fileutils'

# Project / Package
# -----------------------------------------------------------------------


# Refinements
# =======================================================================

require 'nrser/refinements/types'
using NRSER::Types


# Definitions
# =======================================================================


# An server {Locd::Agent} (HTTP only at the moment) that the proxy can
# route requests to.
# 
class Locd::Agent::Proxy < Locd::Agent
  
  # Mixins
  # ============================================================================
  
  include Locd::Agent::System
  
  
  # Constants
  # ==========================================================================
  
  # Attribute / method names that {#to_h} uses.
  # 
  # @return [Hamster::SortedSet<Symbol>]
  # 
  TO_H_NAMES = Locd::Agent::TO_H_NAMES.union [:port, :url]
  
  
  # Class Methods
  # ==========================================================================
  
  # @return [String]
  #   The non-namespace part of the proxy's label.
  # 
  def self.label_name
    "proxy"
  end
  
  
  # @todo Document cmd_template method.
  # 
  # @param [type] arg_name
  #   @todo Add name param description.
  # 
  # @return [return_type]
  #   @todo Document return value.
  # 
  def self.default_cmd_template
    "{bin} proxy run"
  end # .cmd_template
  
  
  def self.default_write_kwds cmd_template: self.default_cmd_template,
                              **kwds
    super(
      keep_alive: true,
      run_at_load: true,
      port: Locd.config[:proxy, :port],
      cmd_template: cmd_template,
      **kwds,
      label: self.label
    )
  end
  
  
  # Instance Methods
  # ============================================================================
  
  # @!group Instance Methods: Attribute Readers
  # ----------------------------------------------------------------------------
  # 
  # Methods to read proxied and computed attributes.
  # 
  
  # @return [Fixnum]
  #   Port service runs on.
  def port
    config['port']
  end
  
  
  # @return [String]
  #   The URL the agent can be reached at through the proxy.
  def url
    "http://#{ label }:#{ Locd.get_port }"
  end
  
  # @!endgroup Instance Methods: Attribute Readers
  
end # class Locd::Agent::Proxy