module ButtonsHelper
  def button_to_release_report(video, content: "", disabled: false)
    if disabled
      t(".generating")
    else
      ReleaseReportButton.build_for_client(self, video, content).to_html
    end
  end

  def button_to_video_release_confirmation(video, releasable, classes: "btn-no-style border cursor-copy p-2", additional_video_release_params: {}, &block)
    if releasable.uses_edl?
      route = [:new, video, releasable, :video_release_confirmation]
      method = :get
    else
      route = [video, releasable, :video_release_confirmations]
      method = :post
    end

    button_to \
      route,
      method: method,
      remote: true,
      class: classes,
      data: { toggle: "tooltip", disable: true },
      title: "Add to Report",
      form: { data: { ujs_target: "video-release-confirmation-form" } },
      params: { video_release_confirmation: { time_elapsed: "" }.merge(additional_video_release_params) },
      &block
  end

  def link_to_edl_file_download(video)
    video_presenter = VideoPresenter.new(video)
    if video_presenter.edl_download_disabled?
      content_tag("a", fa_icon("file-excel-o", text: t(".video.edl_download")), class: "dropdown-item" )
    else
      link_to(fa_icon("file-excel-o", text: t(".video.edl_download")), video_presenter.edl_file_path, class: "dropdown-item", target: "_blank")
    end
  end

  def link_to_graphics_only_edl_file_download(video)
    video_presenter = VideoPresenter.new(video)
    if video_presenter.graphics_edl_download_disabled?
      content_tag("a", fa_icon("file-excel-o", text: t(".video.graphics_only_edl_download")), class: "dropdown-item" )
    else
      link_to(fa_icon("file-excel-o", text: t(".video.graphics_only_edl_download")), video_presenter.graphics_only_edl_file_path, class: "dropdown-item", target: "_blank")
    end
  end

  def link_to_audio_only_edl_file_download(video)
    video_presenter = VideoPresenter.new(video)
    if video_presenter.audio_edl_download_disabled?
      content_tag("a", fa_icon("file-excel-o", text: t(".video.audio_only_edl_download")), class: "dropdown-item" )
    else
      link_to(fa_icon("file-excel-o", text:  t(".video.audio_only_edl_download")), video_presenter.audio_only_edl_file_path, class: "dropdown-item", target: "_blank")
    end
  end

  class ReleaseReportButton
    def self.build_for_client(view, video, content)
      client_name = video.project.client_name.parameterize.underscore
      klass = begin
                "buttons_helper/#{client_name}_report_button".classify.constantize
              rescue NameError
                DiscoveryReportButton
              end
      klass.new(view, video, content)
    end

    def initialize(view, video, content)
      @view = view
      @video = video
      @content = content
    end

    def to_html
      content_tag(:div, class: "btn-group") do
        concat main_button
        concat dropdown_menu
      end
    end

    private

    attr_reader :view, :video, :content

    delegate :button_tag, :concat, :content_tag, :fa_icon, :link_to, :link_to_if, :policy, :t, to: :view

    def main_button
      button_tag content, class: "btn btn-sm btn-success dropdown-toggle", data: { toggle: "dropdown", boundary: "window" }, aria: { haspopup: true, expanded: false }
    end

    def dropdown_header(content, css_class: nil)
      css = css_class.to_s + " dropdown-header"
      content_tag(:h6, content, class: css)
    end

    def dropdown_menu
    end

    def dropdown_divider
      content_tag(:div, nil, class: "dropdown-divider")
    end

    def link_to_big_music_cue_sheet
      link_to build_report_path(:audio_reports, "xlsx", type: "big"), class: "dropdown-item", target: "_blank" do
        fa_icon "file-excel-o", text: "BiG Music Cue Sheet"
      end
    end

    def link_to_issues_and_concerns
      link_to build_report_path(:issues_and_concerns_reports, "xlsx"), class: "dropdown-item", target: "_blank" do
        fa_icon "file-excel-o", text: "Issues and Concerns Report"
      end
    end

    def build_report_path(type, format, options = {})
      # TODO: Use Current for locale
      [video, type, { format: format, locale: I18n.locale }.merge(options)]
    end
  end

  class DiscoveryReportButton < ReleaseReportButton
    def dropdown_menu
      content_tag(:div, class: "dropdown-menu dropdown-menu-right") do
        concat dropdown_header("Download Discovery Documents", css_class: "text-uppercase")
        concat dropdown_divider
        concat dropdown_header("Video")
        concat link_to_production_elements_log
        concat dropdown_header("Graphics")
        concat link_to_gfx_cue_list
        concat dropdown_header("Audio")
        concat link_to_music_cue_sheet
        concat link_to_big_music_cue_sheet
        concat dropdown_header("Other")
        concat link_to_issues_and_concerns
      end
    end

    def link_to_production_elements_log
      link_to build_report_path(:video_reports, "xlsx", type: "discovery"), class: "dropdown-item", target: "_blank" do
        fa_icon "file-excel-o", text: "Production Elements Log"
      end
    end

    def link_to_gfx_cue_list
      link_to build_report_path(:graphic_reports, "xlsx", type: "discovery"), class: "dropdown-item", target: "_blank" do
        fa_icon "file-excel-o", text: "GFX Cue List"
      end
    end

    def link_to_music_cue_sheet
      link_to build_report_path(:audio_reports, "xlsx", type: "discovery"), class: "dropdown-item", target: "_blank" do
        fa_icon "file-excel-o", text: "Music Cue Sheet"
      end
    end
  end

  class NatGeoReportButton < ReleaseReportButton
    def dropdown_menu
      content_tag(:div, class: "dropdown-menu dropdown-menu-right") do
        concat dropdown_header("Download Nat Geo Documents", css_class: "text-uppercase")
        concat dropdown_divider
        concat dropdown_header("Video")
        concat link_to_legal_binder_log
        concat dropdown_header("Graphics")
        concat link_to_text_graphics_log
        concat dropdown_header("Audio")
        concat link_to_music_cue_sheet
        concat link_to_original_music_cue_sheet
        concat link_to_big_music_cue_sheet
        concat dropdown_header("Other")
        concat link_to_issues_and_concerns
      end
    end

    def link_to_legal_binder_log
      link_to build_report_path(:video_reports, "xlsx", type: "nat_geo"), class: "dropdown-item", target: "_blank" do
        fa_icon "file-excel-o", text: "Legal Binder Log"
      end
    end

    def link_to_text_graphics_log
      link_to build_report_path(:graphic_reports, "xlsx", type: "nat_geo"), class: "dropdown-item", target: "_blank" do
        fa_icon "file-excel-o", text: "Text Graphics Log"
      end
    end

    def link_to_music_cue_sheet
      link_to build_report_path(:audio_reports, "xlsx", type: "nat_geo"), class: "dropdown-item", target: "_blank" do
        fa_icon "file-excel-o", text: "Music Cue Sheet"
      end
    end

    def link_to_original_music_cue_sheet
      link_to build_report_path(:audio_reports, "xlsx", type: "nat_geo-original"), class: "dropdown-item", target: "_blank" do
        fa_icon "file-excel-o", text: "Original Music Log"
      end
    end
  end
end
