# typed: false
require "rails_helper"
require_relative "page_spec_helper"

describe Domain::Fa::Parser::Page do
  include Domain::Fa::Parser::PageSpecHelper

  it "sees_not_logged_in" do
    parser = get_parser("index_not_logged_in.html", require_logged_in: false)
    refute parser.logged_in?
    assert_nil parser.logged_in_user
  end

  it "throws_if_not_logged_in_by_default" do
    assert_raises(Domain::Fa::Parser::NotLoggedInError) do
      get_parser("index_not_logged_in.html", require_logged_in: true)
    end
  end

  it "submission_not_found_does_not_throw" do
    parser = get_parser("submission_not_found_in_db.html")
    refute parser.logged_in?
    assert parser.submission_not_found?
  end

  it "sees_logged_in_correctly" do
    parser = get_parser("index_logged_in_zzreg.html")
    assert parser.logged_in?
    assert_equal parser.logged_in_user, "zzreg"
    refute parser.submission_not_found?
  end

  it "user_page_is_correct" do
    parser = get_parser("user_page_miles_df.html")
    assert parser.probably_user_page?

    expect(parser.server_local_time_string).to eq("Feb 23rd, 2016 01:57 AM")
    expect(parser.logged_in_user_tz.formatted_offset).to eq("-08:00")
    expect(parser.server_local_time).to eq(
      Time.parse("Feb 23rd, 2016 09:57 AM UTC"),
    )

    up = parser.user_page

    assert_equal "Miles-DF", up.name
    assert_equal "", up.full_name
    assert_equal "Traditional Artist", up.artist_type
    assert_equal "full", up.mood
    assert up.profile_html, /Attention!!: VERY bad english!/

    assert_equal 541, up.num_submissions
    assert_equal 759_919, up.num_pageviews
    assert_equal 19_937, up.num_comments_recieved
    assert_equal 6645, up.num_comments_given
    assert_equal 55, up.num_journals
    assert_equal 365_602, up.num_favorites
    assert_equal Time.parse("Jan 12th, 2006 07:52 -08:00"), up.registered_since
  end

  it "user page old old version is correct" do
    parser = get_parser("user_page_zaush_old_old.html")
    assert parser.logged_in?
    up = parser.user_page
    assert_equal "//a.facdn.net/1459994050/zaush.gif", up.profile_thumb_url
  end

  it "gallery_is_correct" do
    parser = get_parser("gallery_page_miles_df.html")
    assert parser.probably_listings_page?

    listings = parser.submissions_parsed
    assert_equal 48, listings.length
    assert_equal 19_183_016, listings.first.id
    assert_nil listings.first.artist # artist not listed in main gallery
    assert_nil listings.first.artist_user_page_path # artist not listed in main gallery
    assert_equal "Corona_King_2", listings.first.title
    assert_equal "/view/19183016/", listings.first.view_path
    assert_equal "//t.facdn.net/19183016@200-1456245114.jpg",
                 listings.first.thumb_path

    assert_equal 2, parser.submission_folders.length

    parser_empty = get_parser("gallery_page_empty_miles_df.html")
    assert parser_empty.probably_listings_page?
    assert_equal 0, parser_empty.submissions_parsed.length
  end

  it "new_gallery_is_correct" do
    parser = get_parser("gallery_page_no_folders_fa_update_1_bradydalton.html")
    assert parser.probably_listings_page?

    listings = parser.submissions_parsed

    assert_equal 26, listings.length
    first = listings.first

    assert_equal 19_067_130, first.id
    assert_equal "BradyDalton", first.artist
    assert_equal "/user/bradydalton/", first.artist_user_page_path
    assert_equal "Commission - Scarlett Vaylute Final", first.title
    assert_equal "/view/19067130/", first.view_path
    assert_equal "//t.facdn.net/19067130@300-1455225217.jpg", first.thumb_path
  end

  it "new_gallery_is_correct_with_folders" do
    parser = get_parser("gallery_page_with_folders_fa_update_1_feretta.html")
    assert parser.probably_listings_page?

    listings = parser.submissions_parsed

    assert_equal 48, listings.length
    first = listings.first

    assert_equal 21_837_914, first.id
    assert_equal "Feretta", first.artist
    assert_equal "/user/feretta/", first.artist_user_page_path
    assert_equal "[FERAL] Grunting, heaving, humping puppies", first.title
    assert_equal "/view/21837914/", first.view_path
    assert_equal "//t.facdn.net/21837914@400-1480285460.jpg", first.thumb_path

    folders = parser.submission_folders
    assert_equal({ href: "/scraps/feretta/", title: "Scraps" }, folders[0])
    assert_equal(
      {
        href: "/gallery/feretta/folder/60236/Tale-of-Tails",
        title: "Tale of Tails",
      },
      folders[1],
    )
    assert_equal(
      {
        href: "/gallery/feretta/folder/60234/Illustrations",
        title: "Illustrations",
      },
      folders[2],
    )
    assert_equal(
      { href: "/gallery/feretta/folder/229520/Marketing", title: "Marketing" },
      folders.last,
    )
  end

  it "browse_page_update_1" do
    parser = get_parser("browse_page_fa_update_1.html")
    assert_page_type parser, :probably_listings_page?

    listings = parser.submissions_parsed
    assert_equal 48, listings.length

    first = listings.first
    first.debug = true
    assert_equal 21_840_759, first.id
    assert_equal "Industrial", first.artist
    assert_equal "/user/industrial/", first.artist_user_page_path
    assert_equal "Which one?!", first.title
    assert_equal "/view/21840759/", first.view_path
    assert_equal "//t.facdn.net/21840759@200-1480303217.jpg", first.thumb_path
  end

  it "gallery_page_is_correct_fa_update_1" do
    parser = get_parser("gallerey_draconicon_fa_update_1.html")
    assert_page_type parser, :probably_listings_page?

    listings = parser.submissions_parsed
    assert_equal 72, listings.length

    first = listings.first
    assert_equal 21_849_303, first.id
    assert_equal "draconicon", first.artist
    assert_equal "/user/draconicon/", first.artist_user_page_path
    assert_equal "Mind Most Unsafe", first.title
    assert_equal "/view/21849303/", first.view_path
    assert_equal "//t.facdn.net/21849303@120-1480382449.jpg", first.thumb_path
  end

  it "submission_is_correct" do
    parser = get_parser("submission_19143193_miles_df.html")
    assert parser.probably_submission?
    assert_equal "-08:00", parser.logged_in_user_tz.formatted_offset
    assert_equal Domain::Fa::Parser::Page::VERSION_0, parser.page_version

    sub = parser.submission
    assert_equal 19_143_193, sub.id
    assert_equal "//t.facdn.net/19143193@400-1455889648.jpg", sub.small_img
    assert_equal "workout_session_8_(STREAM_part1)", sub.title
    assert_equal "Miles-DF", sub.artist
    assert_equal "//d.facdn.net/art/miles-df/1455889648/1455889648.miles-df_miles-df_stream.jpg",
                 sub.full_res_img
    expect(Time.parse("Feb 19th, 2016 08:47 AM -08:00")).to be_within(
      1.second,
    ).of(sub.posted_date)
    assert_equal "All", sub.category
    assert_equal "All", sub.theme
    assert_equal "Unspecified / Any", sub.species
    assert_equal "Any", sub.gender
    assert_equal 0, sub.num_favorites
    assert_equal 1, sub.num_comments
    assert_equal 599, sub.num_views
    assert_equal "1280x989", sub.resolution_str
    assert_equal [], sub.keywords_array
    assert sub.description_html =~ /Yep, me/

    parser = get_parser("submission_19190013_korichi.html")
    assert parser.probably_submission?
    assert_equal "Feb 24th, 2016 12:55 AM", parser.server_local_time_string
    assert_equal "-08:00", parser.logged_in_user_tz.formatted_offset

    sub = parser.submission
    assert_equal 19_190_013, sub.id
    assert_equal "Korichi", sub.artist
    assert_equal "General Furry Art", sub.theme
    assert_equal "Artwork (Digital)", sub.category
    assert_equal "1280x914", sub.resolution_str
    assert_equal 82, sub.num_views
    assert_equal "Male", sub.gender
    assert_equal "Dragon (Other)", sub.species
    expect(Time.parse("Feb 24, 2016 12:51 AM -08:00")).to be_within(
      1.second,
    ).of(sub.posted_date)
    assert_equal %w[kobold frog hunting male frog mighty hunter],
                 sub.keywords_array
  end

  it "updated_fa_1_submission" do
    parser = get_parser("submission_21773129_new_fa_feretta.html")
    assert parser.probably_submission?
    assert_equal Domain::Fa::Parser::Page::VERSION_1, parser.page_version

    sub = parser.submission
    assert_equal 21_773_129, sub.id
    assert_equal "//t.facdn.net/21773129@400-1479650817.jpg", sub.small_img
    assert_equal "Space Vixen - Wild Ride desktop", sub.title
    assert_equal "Feretta", sub.artist
    assert sub.description_html =~ /Forgot to put this up/
    assert_equal "//d.facdn.net/art/feretta/1479650817/1479650802.feretta_161004_space_vixen_hop_desktop.jpg",
                 sub.full_res_img
    expect(Time.parse("Nov 20, 2016 9:06AM -08:00")).to be_within(1.second).of(
      sub.posted_date,
    )
    assert_equal "Artwork (Digital)", sub.category
    assert_equal "Miscellaneous", sub.theme
    assert_equal "Canid - Vulpine", sub.species
    assert_equal "Female", sub.gender
    assert_equal 94, sub.num_favorites
    assert_equal 17, sub.num_comments
    assert_equal 925, sub.num_views
    assert_equal "1920x1080", sub.resolution_str
    assert_equal %w[space_vixen vix rocket space], sub.keywords_array
  end

  it "old_submission_is_parsed" do
    parser = get_parser("submission_53013_uaykan.html")
    assert_equal Domain::Fa::Parser::Page::VERSION_0, parser.page_version
    sub = parser.submission

    assert_equal 53_013, sub.id
    assert_equal "uaykan", sub.artist
  end

  it "folders_work" do
    parser = get_parser("gallery_page_salkitten.html")
    folders = parser.submission_folders
    assert_equal 11, folders.length
    assert_equal({ href: "/scraps/salkitten/", title: "Scraps" }, folders[0])
    assert_equal(
      { href: "/gallery/salkitten/folder/51051/7up-Pup", title: "7up Pup" },
      folders[2],
    )
    assert_equal(
      {
        href: "/gallery/salkitten/folder/93190/Animations",
        title: "Animations",
      },
      folders.last,
    )
  end

  it "with_no_folders" do
    parser = get_parser("gallery_page_spearfrost.html")
    assert_equal 0, parser.submission_folders.length
  end

  it "works_with_no_myform" do
    parser = get_parser("submission_19225668_no_comments.html")
    sub = parser.submission

    assert_equal 19_225_668, sub.id
  end

  it "text_only_submission" do
    parser = get_parser("submission_10327196_text_only.html")
    assert parser.probably_submission?

    sub = parser.submission
    assert_equal 10_327_196, sub.id
    assert_equal "icyfoxy", sub.artist
    assert_equal "Ancient Awakening- Wereyena herm TF (By Wyraachur)", sub.title
    assert_equal "//d.facdn.net/art/icyfoxy/stories/1365480658/1365480658.icyfoxy_sonya_the_wereyena.txt",
                 sub.full_res_img
  end

  it "poetry_submission" do
    parser = get_parser("submission_5971150_no_image.html")

    assert parser.probably_submission?
    sub = parser.submission

    assert_equal "Shy Child", sub.title
    assert_equal "WolfieD", sub.artist
    assert_equal "//d.facdn.net/art/wolfied/poetry/1308624615/1308624615.wolfied_shy_child.txt",
                 sub.full_res_img
  end

  it "have_file_url" do
    parser = get_parser("submission_7762536.html")

    sub = parser.submission
    assert_equal 7_762_536, sub.id
    assert_equal "//d.facdn.net/art/musicofluie/1334184376/1334184376.musicofluie_dwragon.png",
                 sub.full_res_img
  end

  it "flash_submission" do
    parser = get_parser("submission_21956745_flash.html")

    assert parser.probably_submission?
    sub = parser.submission

    assert_equal 21_956_745, sub.id
    assert_equal "Drake Test: Take One", sub.title
    assert_equal "Tasuric", sub.artist
    assert (/A closeup of the drake g/) =~ sub.description_html
    assert_equal "Flash", sub.category
  end

  it "id_is_correct_20340491" do
    parser = get_parser("submission_20340491_id_changing.html")
    assert parser.probably_submission?
    sub = parser.submission

    assert_equal 20_340_491, sub.id
    assert_equal "Skye", sub.title
  end

  it "is a legacy submission and can be parsed" do
    parser = get_parser("submission_22390847_legacy.html")
    assert_page_type parser, :probably_submission?
    submission = parser.submission
    assert_equal "puppymachine", submission.artist
    assert_equal "puppymachine", submission.artist_url_name
    assert_equal "//a.facdn.net/1482804755/puppymachine.gif",
                 submission.artist_avatar_url
  end

  it "is an old-old legacy submission and can be parsed" do
    parser = get_parser("submission_28075884_legacy_old_old.html")
    assert_page_type parser, :probably_submission?
    submission = parser.submission
    assert_equal "F-Amiss", submission.artist
    assert_equal "f-amiss", submission.artist_url_name
    assert_equal "//a.facdn.net/1424255659/f-amiss.gif",
                 submission.artist_avatar_url
  end

  def get_parser(file_name, require_logged_in: true)
    file_path = "domain/fa/parser/legacy/#{file_name}"
    contents =
      SpecUtil.read_fixture_file(file_path) ||
        raise("Couldn't open #{file_path}")
    Domain::Fa::Parser::Page.new(contents, require_logged_in:)
  end
end
