# coding: utf-8
require "rails_helper"

RSpec.describe ApplicationHelper, type: :helper do

  describe "#bonus_card_offset_col" do
    it "returns nil when index is not 0 or 2 and number of cards is not 1, 2, or 4" do
      expect(helper.bonus_card_offset_col(7, 7)).to eq nil
    end

    it "returns 'col-md-offset-2' when index is 0 or 2 and number of cards is 2 or 4" do
      expect(helper.bonus_card_offset_col(0, 2)).to eq "col-md-offset-2"
    end

    it "returns 'col-sm-offset-3 col-md-offset-4' when index is 0 or 2 and number of cards is 1" do
      expect(helper.bonus_card_offset_col(0, 1)).to eq "col-sm-offset-3 col-md-offset-4"
    end
  end

  describe "#webinar_cta_url" do
    context "@webinar is present" do
      it "returns the custom_cta_url if one is set" do
        @webinar = create(:webinar, custom_cta_url: "https:/google.com")
        expect(helper.webinar_cta_url("http://#{SecureRandom.hex}.test.com")).to eq @webinar.custom_cta_url
      end
      it "returns the url if there is no @webinar.custom_cta_url is set" do
        @webinar = create(:webinar)
        random_url = "http://#{SecureRandom.hex}.test.com"
        expect(helper.webinar_cta_url(random_url)).to eq random_url
      end
    end
    context "@webinar is not present" do
      it "returns the url provided to it" do
        random_url = "http://#{SecureRandom.hex}.test.com"
        expect(helper.webinar_cta_url(random_url)).to eq random_url
      end
    end
  end

  describe "#webinar_cta_text" do
    context "@webinar is present" do
      it "returns the custom_cta_text if one is set" do
        @webinar = create(:webinar, custom_cta_text: "https:/google.com")
        expect(helper.webinar_cta_text("http://#{SecureRandom.hex}.test.com")).to eq @webinar.custom_cta_text
      end
      it "returns the url if there is no @webinar.custom_cta_text is set" do
        @webinar = create(:webinar)
        random_text = "http://#{SecureRandom.hex}.test.com"
        expect(helper.webinar_cta_text(random_text)).to eq random_text
      end
    end
    context "@webinar is not present" do
      it "returns the url provided to it" do
        random_text = "http://#{SecureRandom.hex}.test.com"
        expect(helper.webinar_cta_text(random_text)).to eq random_text
      end
    end
  end

  describe "#date_iso_format" do
    it "returns a time object in a UTC iso8601 format string" do
      time = Time.now
      expect(helper.date_iso_format(time)).to eq time.utc.iso8601.gsub(/-|:|\.\d+/, "")
    end
  end

  describe "#date_iso_format_day" do
    it "returns a time object in an iso8601 format date string" do
      time = Time.now
      expect(helper.date_iso_format_day(time)).to eq time.to_date.iso8601.gsub(/-|:|\.\d+/, "")
    end
  end

  describe "#empty_or_other" do
    it "will return the second choice, if the first one is empty" do
      a = []
      b = [4]
      c = []
      expect(helper.empty_or_other(a, b)).to eq b
      expect(helper.empty_or_other(b, a)).to eq b
      expect(helper.empty_or_other(c, a)).to eq []
    end
  end

  describe "#filestack_url" do
    it "returns the filestack url for a given image record" do
      image = create(:upload)
      image_id = image.url.split("/")[-1]
      expected_url = URI.encode(
        "https://process.filestackapi.com/#{ENV['FILESTACK_API_KEY']}/auto_image/compress/quality=value:90/#{image_id}"
      )
      expect(helper.filestack_url(image)).to eq expected_url
    end
  end

  describe "#format_time_for_registration_modal" do
    it "returns a human readable date time to be used for a registration modal" do
      time = Time.new(1995, 1, 1)
      expect(helper.format_time_for_registration_modal(time)).to eq "Sunday, January 1st @12AM ET | 9PM PT"
    end
  end

  describe "#get_nested_objekt_fields" do
    it "will return the field values hash for a given objekt by id" do
      object = create(:objekt, template: create(:template))
      expect(helper.get_nested_objekt_fields(object.id)).to eq object.field_values_hash
    end
  end

  describe "#get_schedulable_title" do
    it "return banner name when schedulable is Banner" do
      schedulable = create(:banner)
      expect(helper.get_schedulable_title(schedulable)).to eq schedulable.name
    end
    it "return blog post title when schedulable is BlogPost" do
      schedulable = create(:blog_post)
      expect(helper.get_schedulable_title(schedulable)).to eq schedulable.title
    end
    it "return page name when schedulable is Page" do
      schedulable = create(:page)
      expect(helper.get_schedulable_title(schedulable)).to eq schedulable.name
    end
    it "return redirect description when schedulable is Redirect" do
      schedulable = create(:redirect)
      expect(helper.get_schedulable_title(schedulable)).to eq "#{schedulable.url} → #{schedulable.redirect}"
    end
  end

  describe "#header_class" do
    header_classes = {
      "Large Black" => "default-header",
      "Small Green" => "default-green-header",
      "Strong Black" => "strong-header",
    }
    header_classes.each do |header_type, css_class|
      it "return #{css_class} when given header_type of '#{header_type}'" do
        expect(helper.header_class(header_type)).to eq css_class
      end
    end
  end

  describe "#text_color_from_background" do
    text_color_map = {
      "#FF6D2C" => "#ffffff",
      "#FF7F45" => "#ffffff",
      "#FFE5DA" => "#5f5f5f",
      "#0F8D82" => "#ffffff",
      "#1EA69A" => "#ffffff",
      "#D1EDEB" => "#5f5f5f",
      "#E8F6F5" => "#5f5f5f",
      "#F4FAFA" => "#5f5f5f",
      "#FFFFFF" => "#5f5f5f",
    }
    text_color_map.each do |background_hex_color, text_hex_color|
      it "returns #{text_hex_color} as the appropriate CSS Hex color code for text to compliment the background color #{background_hex_color}" do
        expect(helper.text_color_from_background(background_hex_color)).to eq text_hex_color
      end
    end
  end

  describe "#header_color_from_background" do
    header_color_map = {
      "#FF6D2C" => "#ffffff",
      "#FF7F45" => "#ffffff",
      "#FFE5DA" => "#2c2f33",
      "#0F8D82" => "#ffffff",
      "#1EA69A" => "#ffffff",
      "#D1EDEB" => "#2c2f33",
      "#E8F6F5" => "#2c2f33",
      "#F4FAFA" => "#2c2f33",
      "#FFFFFF" => "#2c2f33",
    }
    header_color_map.each do |background_hex_color, header_hex_color|
      it "returns #{header_hex_color} as the appropriate CSS Hex color code for a header to compliment the background color #{background_hex_color}" do
        expect(helper.header_color_from_background(background_hex_color)).to eq header_hex_color
      end
    end
  end

  describe "#hide_if_empty" do
    it "returns 'hidden' if the input responds to .empty?" do
      expect(helper.hide_if_empty("")).to eq "hidden"
    end
  end

  describe "#markdown" do
    it "renders markdown into html" do
      sample_md = "#hi"
      expect(helper.markdown(sample_md)).to eq "<h1>hi</h1>\n"
    end
    it "renders markdown into html and accepts html if passed as an option" do
      sample_md = "#hi\n<h2>this is great</h2>"
      expect(helper.markdown(sample_md, skip_html_escape: true)).to eq "<h1>hi</h1>\n\n<h2>this is great</h2>\n"
    end
  end

  describe "#number_of" do
    it "returns the number of instances of a field type in a collection of fields" do
      field_values = {}
      field_name = SecureRandom.hex
      rand(99).times { |i|  field_values["#{field_name} #{i}"] = i }
      expect(helper.number_of(field_name, field_values)).to eq field_values.keys.count
    end
  end

  describe "#output_background_image" do
    it "returns 'background-image: none' if there is no image with the given id" do
      expect(helper.output_background_image(2)).to eq "background-image: none;"
    end
    it "returns 'background-image: url()' if there is an image with the given id" do
      image = create(:upload)
      id = image.url.split("/")[-1]
      url = URI.encode("https://process.filestackapi.com/#{ENV['FILESTACK_API_KEY']}/auto_image/compress/quality=value:90/#{id}")
      expect(helper.output_background_image(image.id)).to eq "background-image: url(#{url});"
    end
  end

  describe "#output_image_src" do
    it "returns src=<url for image provided>" do
      image = create(:upload)
      id = image.url.split("/")[-1]
      url = URI.encode("https://process.filestackapi.com/#{ENV['FILESTACK_API_KEY']}/auto_image/compress/quality=value:90/#{id}")
      expect(helper.output_image_src(image)).to eq "src=\"#{url}\""
    end
  end

  context "padding methods" do
    let(:obj) do
      {
        computer: 0,
        mobile: 0,
        tablet: 0,
      }
    end
    describe "#output_margin_bottoms" do
      it "returns a css style for a given class and an object of setting for margin bottoms" do
        expected_string = "#test-class {\n      margin-bottom: 0px; \n    }\n  @media (max-width: 991px) {\n    #test-class {\n      margin-bottom: 0px; \n    }\n  }\n  @media (max-width: 767px) {\n    #test-class {\n      margin-bottom: 0px; \n    }\n  }"
        expect(helper.output_margin_bottoms("#test-class", obj)).to eq expected_string
      end
    end

    describe "#output_padding_tops" do
      it "returns a css style for a given class and an object of setting for padding tops" do
        expected_string = "#test-class {\n      padding-top: 0px; \n    }\n  @media (max-width: 991px) {\n    #test-class {\n      padding-top: 0px; \n    }\n  }\n  @media (max-width: 767px) {\n    #test-class {\n      padding-top: 0px; \n    }\n  }"
        expect(helper.output_padding_tops("#test-class", obj)).to eq expected_string
      end
    end

    describe "#output_padding_bottoms" do
      it "returns a css style for a given class and an object of setting for padding bottoms" do
        expected_string = "#test-class {\n      padding-bottom: 0px; \n    }\n  @media (max-width: 991px) {\n    #test-class {\n      padding-bottom: 0px; \n    }\n  }\n  @media (max-width: 767px) {\n    #test-class {\n      padding-bottom: 0px; \n    }\n  }"
        expect(helper.output_padding_bottoms("#test-class", obj)).to eq expected_string
      end
    end
  end

  describe "#p" do
    it "it prints to the rails logger in debug mode" do
      message = SecureRandom.hex
      expect(Rails.logger).to receive(:debug).with(message)
      helper.p(message)
    end
  end

  describe "#plan_params" do
    it "add the appropriate params if present into a single query string" do
      plan = create(:plan)
      src = SecureRandom.hex
      utm_sales_page_cta = SecureRandom.hex
      field_values = {
        "Upgrade Src" => src,
        "utm_sales_page_cta" => utm_sales_page_cta,
        "Monthly #{plan.name} Coupon Code" => "code",
      }
      length = "Monthly"
      expect(helper.plan_params(plan, field_values, length)).to eq "?plan_id=1&upgrade_src=#{src}&utm_sales_page_cta=#{utm_sales_page_cta}&coupon_code=code"
    end
  end

  describe "#s3_url" do
    it "returns the url of the image on s3" do
      image = create(:upload)
      url = URI.encode("https://s3.amazonaws.com/cms-uploaded-images/#{image.s3_url}")
      expect(helper.s3_url(image)).to eq url
    end
  end

  describe "#standard_margin_bottoms" do
    it "returns a set of css styles for margin bottom across device type for a given partial id" do
      field_values = {
        "Computer Margin Bottom" => 0,
        "Tablet Margin Bottom" => 0,
        "Mobile Margin Bottom" => 0,
      }
      expect(helper.standard_margin_bottoms("test", field_values)).to eq "._test {\n      margin-bottom: 0px; \n    }\n  @media (max-width: 991px) {\n    ._test {\n      margin-bottom: 0px; \n    }\n  }\n  @media (max-width: 767px) {\n    ._test {\n      margin-bottom: 0px; \n    }\n  }"
    end
  end

  describe "#webpack_bundle_tag" do
    context "bundle does not exist" do
      it "returns nil" do
        expect(helper.webpack_bundle_tag("kung-fu")).to be_nil
      end
    end

    context "bundle does exist" do

      before(:each) do
        manifest_path = Rails.root.join("public", "javascripts", "manifest.json")
        unless File.exist? manifest_path
          manifest_data = {
            "bundle.js" => "/javascripts/bundle-1234.js",
            "bundle.css" => "/stylesheets/bundle-1234.css",
          }
          File.open(manifest_path, "w") { |file| file.write(manifest_data) }
        end
      end

    end
  end

  describe "#zero_is" do
    it "returns output if the input is zero" do
      expect(helper.zero_is("candy", "0px")).to eq "candy"
      expect(helper.zero_is("candy", "120px")).to eq "120px"
      expect(helper.zero_is("barre", "5%")).to eq "5%"
    end
  end

  describe "#to_css_class" do
    it "converst an array of strings to appropriate css class name" do
      names = %w[Apple Bank Coffee]
      expect(helper.to_css_class(names)).to eq "apple-bank-coffee"
    end
  end

end
