module Workstation
  class AccountPage < Workstation::BasePage
    page_url "/account/myprofile"

    select_list(:workstation_view, name: "workstationview")
    link(:save_view_settings, id: "view_settings_submit")
    span(:view_settings_save_message, id: "view_settings_save_message")
    form(:country_settings_form, id: "country_settings_form")
    select_list(:language, id: "language")
    select_list(:select_number_format, id: "number_format")

    span(:apply_language, id: "submit_country_settings")
    nav(:subnav, class: "main-subnav")
    link(:account_profile_link) { subnav_element.link_element(href: "/account/myprofile") }

    span(:apply_email, id: "submit_email_notifications")
    label(:spike_checkbox, for: "spike_detector")
    checkbox(:spike, id: "spike_detector")
    span(:apply_email_message, id: "submit_email_notifications_message")

    def select_language(lang)
      language.select(lang)
      apply_language.click
    end

    def language_verification(lang)
      language_title_hash = {
        "German" => "Konto",
        "French" => "Compte",
        "English" => "Profile",
        "Spanish" => "Perfil",
        "Italian" => "Conto",
        "Japanese" => "アカウント",
        "Portugese" => "Conta",
        "Russian" => "Аккаунт",
        "Turkish" => "Hesap"
      }
      expected = language_title_hash[lang]
      expect(account_profile_link_element.text).to eq(expected)
    end

    alias_method :old_workstation_view=, :workstation_view=

    def workstation_view=(view)
      return if workstation_view_element.selected_options.first.downcase == view.downcase
      view_type = workstation_view_element.options.map(&:text).find { |opt| opt.downcase == view.downcase }
      self.old_workstation_view = view_type
      save_view_settings
      view_settings_save_message_element.element.wait_until(&:present?)
    end

    def select_country(number_format)
      self.select_number_format = number_format
    end
  end
end
