# frozen_string_literal: true

require "rails_helper"

class TestTab < Alchemy::Admin::LinkDialog::BaseTab
  def title
    "Test Tab"
  end

  def self.panel_name
    :test_tab
  end

  def fields
    [title_input, target_select]
  end
end

RSpec.describe "Link overlay", type: :system do
  let!(:language) { create(:alchemy_language) }

  before do
    authorize_user(:as_admin)
  end

  context "GUI" do
    it "has a tab for linking internal pages" do
      visit link_admin_pages_path
      within("#overlay_tabs") { expect(page).to have_content("Internal") }
    end

    it "has a tab for adding anchor links" do
      visit link_admin_pages_path
      within("#overlay_tabs") { expect(page).to have_content("Anchor") }
    end

    it "has a tab for linking external pages" do
      visit link_admin_pages_path
      within("#overlay_tabs") { expect(page).to have_content("External") }
    end

    it "has a tab for linking files" do
      visit link_admin_pages_path
      within("#overlay_tabs") { expect(page).to have_content("File") }
    end

    context "add new tab" do
      before do
        stub_alchemy_config(:link_dialog_tabs, ["TestTab"])
      end

      it "has a new tab" do
        visit link_admin_pages_path
        within("#overlay_tabs") { expect(page).to have_content("Test Tab") }
      end
    end
  end

  context "linking pages", js: true do
    let(:lang_root) do
      create(:alchemy_page, :language_root)
    end

    let!(:page1) do
      create(:alchemy_page, :public, parent_id: lang_root.id)
    end

    let!(:page2) do
      create(:alchemy_page, :public, parent_id: lang_root.id)
    end

    let!(:article) do
      create(
        :alchemy_element,
        name: "article",
        page_version: page1.draft_version,
        autogenerate_ingredients: true
      )
    end

    it "should be possible to link a page" do
      visit edit_admin_page_path(page1)

      within "#element_#{article.id} .ingredient-editor.text.linkable" do
        fill_in "Intro", with: "Link me"
        click_button_with_tooltip Alchemy.t(:place_link)
      end

      within "[name='overlay_tab_internal_link']" do
        expect(page).to have_selector("#s2id_internal_link")
        select2_search(page2.name, from: "Page")
        click_button "apply"
      end

      within "#element_#{article.id}" do
        click_button "Save"
      end

      within "#flash_notices" do
        expect(page).to have_content "Saved element."
      end

      expect(page).to have_css("iframe#alchemy_preview_window", wait: 5)
      within_frame "alchemy_preview_window" do
        expect(page).to have_link("Link me", href: "/#{page2.urlname}")
      end
    end

    it "should be possible to link an external url" do
      visit edit_admin_page_path(page1)

      within "#element_#{article.id} .ingredient-editor.text.linkable" do
        fill_in "Intro", with: "Link me"
        click_button_with_tooltip Alchemy.t(:place_link)
      end

      within "#overlay_tabs" do
        find("[panel='overlay_tab_external_link']").click
      end

      within "[name='overlay_tab_external_link']" do
        expect(page).to have_selector("#external_link")
        fill_in("URL", with: "https://example.com")
        click_button "apply"
      end

      within "#element_#{article.id}" do
        click_button "Save"
      end

      within "#flash_notices" do
        expect(page).to have_content "Saved element."
      end

      expect(page).to have_css("iframe#alchemy_preview_window", wait: 5)
      within_frame "alchemy_preview_window" do
        expect(page).to have_link("Link me", href: "https://example.com")
      end
    end

    it "should be possible to link a file" do
      file = create(:alchemy_attachment)
      visit edit_admin_page_path(page1)

      within "#element_#{article.id} .ingredient-editor.text.linkable" do
        fill_in "Intro", with: "Link me"
        click_button_with_tooltip Alchemy.t(:place_link)
      end

      within "#overlay_tabs" do
        find("[panel='overlay_tab_file_link']").click
      end

      within "[name='overlay_tab_file_link']" do
        expect(page).to have_selector("#file_link")
        select2_search(file.name, from: "File")
        click_button "apply"
      end

      within "#element_#{article.id}" do
        click_button "Save"
      end

      within "#flash_notices" do
        expect(page).to have_content "Saved element."
      end

      expect(page).to have_css("iframe#alchemy_preview_window", wait: 5)
      within_frame "alchemy_preview_window" do
        expect(page).to have_link("Link me", href: "/attachment/#{file.id}/download/#{file.file_name}")
      end
    end
  end
end
