# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
#     ***     AUTO GENERATED CODE    ***    AUTO GENERATED CODE     ***
#
# ----------------------------------------------------------------------------
#
#     This file is automatically generated by Magic Modules and manual
#     changes will be clobbered when the file is regenerated.
#
#     Please read more about how to change this file in README.md and
#     CONTRIBUTING.md located at the root of this package.
#
# ----------------------------------------------------------------------------
require 'gcp_backend'
require 'google/compute/property/regionbackendservice_backends'
require 'google/compute/property/regionbackendservice_connection_draining'

# A provider to manage Compute Engine resources.
class ComputeRegionBackendService < GcpResourceBase
  name 'google_compute_region_backend_service'
  desc 'RegionBackendService'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :name
  attr_reader :health_checks
  attr_reader :backends
  attr_reader :description
  attr_reader :fingerprint
  attr_reader :protocol
  attr_reader :session_affinity
  attr_reader :region
  attr_reader :timeout_sec
  attr_reader :connection_draining
  attr_reader :load_balancing_scheme

  def initialize(params)
    super(params.merge({ use_http_transport: true }))
    @params = params
    @fetched = @connection.fetch(product_url, resource_base_url, params, 'Get')
    parse unless @fetched.nil?
    @params = params
  end

  def parse
    @name = @fetched['name']
    @health_checks = @fetched['healthChecks']
    @backends = GoogleInSpec::Compute::Property::RegionBackendServiceBackendsArray.parse(@fetched['backends'], to_s)
    @description = @fetched['description']
    @fingerprint = @fetched['fingerprint']
    @protocol = @fetched['protocol']
    @session_affinity = @fetched['sessionAffinity']
    @region = @fetched['region']
    @timeout_sec = @fetched['timeoutSec']
    @connection_draining = GoogleInSpec::Compute::Property::RegionBackendServiceConnectionDraining.new(@fetched['connectionDraining'], to_s)
    @load_balancing_scheme = @fetched['loadBalancingScheme']
  end

  # Handles parsing RFC3339 time string
  def parse_time_string(time_string)
    time_string ? Time.parse(time_string) : nil
  end

  def exists?
    !@fetched.nil?
  end

  def to_s
    "RegionBackendService #{@params[:name]}"
  end

  def un_parse
    {
      'name' => ->(x, _) { x.nil? ? [] : ["its('name') { should cmp #{x.inspect} }"] },
      'health_checks' => ->(x, _) { x.nil? ? [] : x.map { |single| "its('health_checks') { should include #{single.inspect} }" } },
      'backends' => ->(x, _) { x.nil? ? [] : x.map { |single| "its('backends') { should include '#{single.to_json}' }" } },
      'description' => ->(x, _) { x.nil? ? [] : ["its('description') { should cmp #{x.inspect} }"] },
      'fingerprint' => ->(x, _) { x.nil? ? [] : ["its('fingerprint') { should cmp #{x.inspect} }"] },
      'protocol' => ->(x, _) { x.nil? ? [] : ["its('protocol') { should cmp #{x.inspect} }"] },
      'session_affinity' => ->(x, _) { x.nil? ? [] : ["its('session_affinity') { should cmp #{x.inspect} }"] },
      'region' => ->(x, _) { x.nil? ? [] : ["its('region') { should cmp #{x.inspect} }"] },
      'timeout_sec' => ->(x, _) { x.nil? ? [] : ["its('timeout_sec') { should cmp #{x.inspect} }"] },
      'connection_draining' => ->(x, _) { x.nil? ? [] : GoogleInSpec::Compute::Property::RegionBackendServiceConnectionDraining.un_parse(x, 'connection_draining') },
      'load_balancing_scheme' => ->(x, _) { x.nil? ? [] : ["its('load_balancing_scheme') { should cmp #{x.inspect} }"] },
    }
  end

  def dump(path, template_path, test_number, ignored_fields)
    name = 'RegionBackendService'

    arr = un_parse.map do |k, v|
      next if ignored_fields.include?(k)
      v.call(method(k.to_sym).call, k)
    end
    template_vars = {
      name: name,
      arr: arr,
      type: 'google_compute_region_backend_service',
      identifiers: @params,
      number: test_number,
    }
    File.open("#{path}/#{name}_#{test_number}.rb", 'w') do |file|
      file.write(ERB.new(File.read(template_path)).result_with_hash(template_vars))
    end
  end

  private

  def product_url
    'https://www.googleapis.com/compute/v1/'
  end

  def resource_base_url
    'projects/{{project}}/regions/{{region}}/backendServices/{{name}}'
  end
end
