module EditServerInfoModal
  include PageObject

  # Server Panel
  div(:server_info, id: "remote_server_info")
  links(:edit_server) { server_info_element.link_elements(text: "Edit") }
  table(:remote_server_info) { server_info_element.table_element(class: %w[data full]) }
  select_list(:server_type, id: "order_type")
  text_field(:server_domain_name, id: "domain_name")
  text_field(:server_user_name, id: "user_name")
  text_field(:server_password, id: "password")
  text_field(:server_port, id: "port")
  select_list(:server_connection_type, id: "connection_type")
  select_list(:server_auth_type, id: "authenticate_type")
  select_list(:server_batch_delivery, id: "batch_delivery")
  text_field(:server_batch_folder, id: "batch_foldername")
  text_field(:server_manifest_file, id: "manifest_filename")
  text_field(:server_delivery_file, id: "delivery_complete_file")
  select_list(:server_ftp_mode, id: "ftp_pasv_mode")
  text_field(:server_batch_size, id: "num_per_batch")
  text_field(:server_connection_limit, id: "connection_limit")
  text_field(:server_sftp_known_host, id: "known_host")
  button(:submit_server, value: "Save & Close")
  button(:cancel_server, value: "Cancel")

  # Populate Server Fields
  def populate_server_info_fields(server_info_data)
    edit_server_elements.first.click
    populate_type_domain_port(server_info_data)
    populate_server_auth(server_info_data)
    populate_batch_data(server_info_data)
    populate_conn_data(server_info_data)
    populate_manifest_delivery_size(server_info_data)
    self.server_ftp_mode = server_info_data[:ftp_pasv_mode]
  end

  # Verify Server Fields
  def verify_server_info_on_form(server_info_data)
    edit_server_elements.first.click
    verify_type_domain_port(server_info_data)
    verify_server_auth(server_info_data)
    verify_batch_data(server_info_data)
    verify_conn_data(server_info_data)
    verify_manifest_delivery_size(server_info_data)
    cancel_server
  end

  private

  # Server Info Methods
  def populate_type_domain_port(server_info_data)
    self.server_type = server_info_data[:server_type]
    self.server_domain_name = server_info_data[:domain_name]
    self.server_port = server_info_data[:port]
  end

  def populate_server_auth(server_info_data)
    self.server_user_name = server_info_data[:user_name]
    self.server_password = server_info_data[:password]
    self.server_auth_type = server_info_data[:authenticate_type]
  end

  def populate_batch_data(server_info_data)
    self.server_batch_delivery = server_info_data[:batch_delivery]
    self.server_batch_folder = server_info_data[:batch_foldername]
  end

  def populate_conn_data(server_info_data)
    self.server_connection_type = server_info_data[:connection_type]
    self.server_connection_limit = server_info_data[:connection_limit]
    self.server_sftp_known_host = server_info_data[:known_host]
  end

  def populate_manifest_delivery_size(server_info_data)
    self.server_manifest_file = server_info_data[:manifest_filename]
    self.server_delivery_file = server_info_data[:delivery_complete_file]
    self.server_batch_size = server_info_data[:num_per_batch]
  end

  def verify_type_domain_port(server_info_data)
    expect(server_type).to eql server_info_data[:server_type]
    expect(server_domain_name).to eql server_info_data[:domain_name]
    expect(server_port).to eql server_info_data[:port]
  end

  def verify_server_auth(server_info_data)
    expect(server_user_name).to eql server_info_data[:user_name]
    expect(server_password).to eql server_info_data[:password]
    expect(server_auth_type).to eql server_info_data[:authenticate_type]
  end

  def verify_batch_data(server_info_data)
    expect(server_batch_delivery).to eql server_info_data[:batch_delivery]
    expect(server_batch_folder).to eql server_info_data[:batch_foldername]
  end

  def verify_conn_data(server_info_data)
    expect(server_connection_type).to eql server_info_data[:connection_type]
    expect(server_connection_limit).to eql server_info_data[:connection_limit]
    expect(server_sftp_known_host).to eql server_info_data[:known_host]
  end

  def verify_manifest_delivery_size(server_info_data)
    expect(server_manifest_file).to eql server_info_data[:manifest_filename]
    expect(server_delivery_file).to eql server_info_data[:delivery_complete_file]
  end

  def check_type_domain(server_info_data)
    wait_until { edit_server_elements[0].when_present }
    expect(remote_server_info_element[1]["Domain Name"].text).to eql server_info_data[:domain_name]
  end

  def check_server_auth(server_info_data)
    expect(remote_server_info_element[1]["Username"].text).to eql server_info_data[:user_name]
    expect(remote_server_info_element[1]["Password"].text).to eql server_info_data[:password]
  end

  def check_conn_data(server_info_data)
    expect(remote_server_info_element[1]["Batch Size"].text).to eql server_info_data[:num_per_batch]
    expect(remote_server_info_element[1]["Conn. Limit"].text).to eql server_info_data[:connection_limit]
  end
end
