module OA
  class ViewCustomerMaster < OA::BasePage
    include EditServerInfoModal

    page_url "/warehouse/view_customer_master.php"
    div(:confirmation_message, class: "alertConfirm")
    list_item(:audio_delivery_option) { b_element(text: "Audio Delivery Option:").parent }
    div(:spec_panelheader, id: "specs_header")
    link(:add_spec) { spec_panelheader_element.link_element(text: "Add") }
    div(:spec_panelcontent, id: "specs")
    table(:spec_info) { spec_panelcontent_element.table_element(class: "data") }
    links(:delete_spec) { spec_panelcontent_element.link_elements(text: "Delete") }
    links(:edit_spec) { spec_panelcontent_element.link_elements(text: "Edit") }
    select_list(:spec_product_type, id: "product_type_id")
    select_list(:spec_encoding_order_type, id: "order_type")
    select_list(:spec_profile_type, id: "profile_type")
    select_list(:spec_format, id: "encoding_profile_id")
    select_list(:required, id: "required")
    text_field(:spec_filename, id: "filename")
    text_field(:spec_delivery_location, id: "delivery_location")
    button(:submit_spec, id: "form_submit_button")
    button(:cancel_spec, value: "Cancel")

    def delete_all_specs
      return unless spec_info?
      number_of_specs = spec_info_element.rows - 1
      puts "deleting #{number_of_specs} spec(s)..."
      (1..number_of_specs).each do |current_spec_index|
        delete_spec(number_of_specs - current_spec_index)
      end
    end

    # Populate Spec Fields
    def populate_spec_fields(spec_info_data)
      add_spec
      populate_order_fields(spec_info_data)
      populate_product_fields(spec_info_data)
      self.required = spec_info_data[:required]
      submit_spec
    end

    # Verify Spec Fields
    def check_populated_spec_data(spec_info_data)
      check_order_fields(spec_info_data)
      check_product_fields(spec_info_data)
    end

    def verify_populated_spec_fields(spec_info_data)
      edit_spec_elements.first.click
      verify_order_fields(spec_info_data)
      verify_product_fields(spec_info_data)
      expect(required).to eql spec_info_data[:required]
      cancel_spec
    end

    # check server data fields
    def verify_server_info_on_table(server_info_data)
      wait_until { edit_server_elements[0].when_present && !server_user_name_element.exists? }
      check_type_domain(server_info_data)
      check_server_auth(server_info_data)
      check_conn_data(server_info_data)
    end

    private

    def delete_spec(index)
      delete_element = delete_spec_elements[index]
      browser.after_hooks.without do
        delete_element.click
        wait_until { browser.alert.exists? }
        browser.alert.ok
      end
      wait_until { !delete_element.element.present? }
    end

    def populate_order_fields(spec_info_data)
      self.spec_product_type = spec_info_data[:product_type]
      self.spec_encoding_order_type = spec_info_data[:encoding_order_type]
      self.spec_profile_type = spec_info_data[:profile_type]
    end

    def populate_product_fields(spec_info_data)
      self.spec_format = spec_info_data[:format]
      self.spec_filename = spec_info_data[:filename]
      self.spec_delivery_location = spec_info_data[:delivery_location]
    end

    # rubocop:disable Metrics/AbcSize
    def check_order_fields(spec_info_data)
      Retriable.retriable(on: Watir::Exception::LocatorException, tries: 2) do
        wait_until(10) { spec_info_element.present? && spec_info_element[1] }
      end
      expect(spec_info_element[1]["Order Type"].text).to eql spec_info_data[:encoding_order_type]
      expect(spec_info_element[1]["Product Type"].text).to eql spec_info_data[:product_type]
    end
    # rubocop:enable Metrics/AbcSize

    def check_product_fields(spec_info_data)
      expect(spec_info_element[1]["Encoding Type"].text).to eql spec_info_data[:profile_type].downcase
      expect(spec_info_element[1]["Description"].text).to eql spec_info_data[:format]
    end

    def verify_order_fields(spec_info_data)
      expect(spec_product_type).to eql spec_info_data[:product_type]
      expect(spec_encoding_order_type).to eql spec_info_data[:encoding_order_type]
      expect(spec_profile_type).to eql spec_info_data[:profile_type]
    end

    def verify_product_fields(spec_info_data)
      expect(spec_format).to eql spec_info_data[:format]
      expect(spec_filename).to eql spec_info_data[:filename]
      expect(spec_delivery_location).to eql spec_info_data[:delivery_location]
    end
  end
end
