module Workstation
  class EditFilmReleasePage < Workstation::BasePage
    include Workstation::ReleaseStatusBar
    include Workstation::FilmCastAndCrew

    # TODO: paramaterize the following url in a seperate PR
    # page_url = 'http://workstation.qaorch.com/releasebuilder/editfilmrelease?release_id=1234'

    select_list(:release_meta_language, name: "meta_language")
    text_field(:release_name, name: "releaseName")
    select_list(:film_brand, id: "artistName")
    text_field(:digital_upc, name: "filmUPC")
    select_list(:country_of_origin, id: "country_of_origin")
    text_field(:label, name: "label")
    text_field(:release_date, name: "release_date")
    text_field(:sale_start_date, name: "sale_start_date")
    select(:distribution, name: "distribution")
    select(:primary_genre, id: "primaryGenre")
    select(:subgenre, id: "secondaryGenres")
    select(:country_of_origin, id: "country_of_origin")
    select(:film_type, id: "productSubtype")
    select(:studio, id: "imprintName")
    select(:audio_language, id: "audio_language")
    text_area(:short_synopsis, id: "shortSynopsis")
    text_area(:long_synopsis, id: "description")
    label(:closed_caption, for: "createreleasemodal_closedcaption-YES")
    label(:vod_rights, for: "vodStartDateBtn-Y")
    text_field(:vod_rights_date, id: "vodStartDate")
    label(:download_rights, for: "saleStartDateBtn-Y")
    text_field(:download_rights_date, id: "saleStartDate")
    label(:theatrical_release, for: "theatricalReleaseDateBtn-Y")
    text_field(:theatrical_release_date, id: "theatricalReleaseDate")
    label(:physical_release, for: "physicalReleaseDateBtn-Y")
    text_field(:physical_release_date, id: "physicalReleaseDate")
    span(:validation_errors, id: "validation_errors")
    text_field(:copyright, id: "c_line")
    button(:submit, id: "saveRelease")

    select(:us_rating, id: "rating_0")
    text_field(:us_reason, id: "rating_reason_0")
    div(:ratings_loading, id: "ratingloading")
    button(:add_new_studio_btn, id: "newImprintButton")
    button(:add_studio, id: "addNewImprint")

    text_field(:add_new_studio_text_field, id: "newImprint")

    # rubocop:disable Metrics/AbcSize
    # rubocop:disable Metrics/MethodLength
    def populate_required_fields(data)
      # TODO: Refactor this to use #populate_page_with to reduce method length
      self.release_meta_language = data["meta_language"]
      self.primary_genre = data["genre"]
      self.copyright = data["c_line"]
      self.subgenre = data["subgenre"]
      self.release_name = data["release_name"]
      self.short_synopsis = data["short_synopsis"]
      self.long_synopsis = data["long_synopsis"]
      self.country_of_origin = data["country_of_origin"]
      create_new_studio(data["studio"])
      self.film_type = data["film_type"]
      self.audio_language = data["audio_language"]
      physical_release_element.element.click
      theatrical_release_element.element.click
      download_rights_element.element.click
      vod_rights_element.element.click
      self.download_rights_date = data["download_rights_date"]
      self.theatrical_release_date = data["theatrical_release_date"]
      self.physical_release_date = data["physical_release_date"]
      self.vod_rights_date = data["vod_rights_date"]
      us_rating_element.when_present.send_keys data["us_rating"]
      self.us_reason = data["us_reason"]
      closed_caption_element.element.click
      add_talent(data["talent"])
      add_production_crew(data["production_crew"])
      submit_element.element.click
    end
    # rubocop:enable Metrics/MethodLength
    # rubocop:enable Metrics/AbcSize

    def create_new_studio(name)
      add_new_studio_btn
      add_new_studio_text_field_element.when_present.element.set name
      add_studio_element.click
      wait_until { studio_options.include? name }
    end
  end
end
