# 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/cloudbuild/property/trigger_build'
require 'google/cloudbuild/property/trigger_build_steps'
require 'google/cloudbuild/property/trigger_trigger_template'

# A provider to manage Cloud Build resources.
class CloudBuildTrigger < GcpResourceBase
  name 'google_cloudbuild_trigger'
  desc 'Trigger'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :id
  attr_reader :description
  attr_reader :disabled
  attr_reader :create_time
  attr_reader :substitutions
  attr_reader :filename
  attr_reader :ignored_files
  attr_reader :included_files
  attr_reader :trigger_template
  attr_reader :build

  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
    @id = @fetched['id']
    @description = @fetched['description']
    @disabled = @fetched['disabled']
    @create_time = parse_time_string(@fetched['createTime'])
    @substitutions = @fetched['substitutions']
    @filename = @fetched['filename']
    @ignored_files = @fetched['ignoredFiles']
    @included_files = @fetched['includedFiles']
    @trigger_template = GoogleInSpec::CloudBuild::Property::TriggerTriggerTemplate.new(@fetched['triggerTemplate'], to_s)
    @build = GoogleInSpec::CloudBuild::Property::TriggerBuild.new(@fetched['build'], to_s)
  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
    "Trigger #{@params[:id]}"
  end

  def un_parse
    {
      'id' => ->(x, _) { x.nil? ? [] : ["its('id') { should cmp #{x.inspect} }"] },
      'description' => ->(x, _) { x.nil? ? [] : ["its('description') { should cmp #{x.inspect} }"] },
      'disabled' => ->(x, _) { x.nil? ? [] : ["its('disabled') { should cmp #{x.inspect} }"] },
      'create_time' => ->(x, _) { x.nil? ? [] : ["its('create_time.to_s') { should cmp '#{x.inspect}' }"] },
      'substitutions' => ->(x, _) { x.nil? ? [] : x.map { |k, v| "its('substitutions') { should include(#{k.inspect} => #{v.inspect}) }" } },
      'filename' => ->(x, _) { x.nil? ? [] : ["its('filename') { should cmp #{x.inspect} }"] },
      'ignored_files' => ->(x, _) { x.nil? ? [] : x.map { |single| "its('ignored_files') { should include #{single.inspect} }" } },
      'included_files' => ->(x, _) { x.nil? ? [] : x.map { |single| "its('included_files') { should include #{single.inspect} }" } },
      'trigger_template' => ->(x, _) { x.nil? ? [] : GoogleInSpec::CloudBuild::Property::TriggerTriggerTemplate.un_parse(x, 'trigger_template') },
      'build' => ->(x, _) { x.nil? ? [] : GoogleInSpec::CloudBuild::Property::TriggerBuild.un_parse(x, 'build') },
    }
  end

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

    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_cloudbuild_trigger',
      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://cloudbuild.googleapis.com/v1/'
  end

  def resource_base_url
    'projects/{{project}}/triggers/{{id}}'
  end
end
