require 'rails_helper'

RSpec.describe Dunlop::ApplicationHelper, type: :helper do
  subject { helper }

  describe '#hours_for_select' do
    it 'returns a proper array' do
      subject.hours_for_select.first.should be_a DayTimeMinutes
      subject.hours_for_select.map(&:to_s).should eq %w[
        00:00
        01:00
        02:00
        03:00
        04:00
        05:00
        06:00
        07:00
        08:00
        09:00
        10:00
        11:00
        12:00
        13:00
        14:00
        15:00
        16:00
        17:00
        18:00
        19:00
        20:00
        21:00
        22:00
        23:00
      ]
    end
  end


  describe '#plan_date', disabled: true do
    let(:window_prefix) { '&nbsp;<span class="fa fa-clock-o"></span>&nbsp;&nbsp;' }
    it 'is blank when none is set' do
      expect( subject.plan_date ).to be_blank
    end

    it "is shows when window_from is blank" do
      expect( subject.plan_date window_to: 6 ).to eq "#{window_prefix}-&nbsp;6:00"
    end

    it "shows when window_to is blank" do
      expect( subject.plan_date window_from: 4).to eq "#{window_prefix}4:00&nbsp;-"
    end

    it 'returns a proper display when window is set' do
      expect( subject.plan_date window_from: 4, window_to: 11 ).to eq "#{window_prefix}4:00&nbsp;-&nbsp;11:00"
      expect( subject.plan_date window_from: 4, window_to: 11 ).to be_html_safe
    end

    it "Prepends the date if given" do
      expect( subject.plan_date date: Date.current, window_from: 4, window_to: 11 ).to eq "#{Date.current.strftime('%d-%m-%Y')} #{window_prefix}4:00&nbsp;-&nbsp;11:00"
    end
  end

  describe '#plan_date_for_record', disabled: true do
    it "Calls the plan_date helper with correct paramenters" do
      expect( subject ).to receive(:plan_date).with(date: Date.current, window_from: 8, window_to: 10)
      subject.plan_date_for_record build(:owner_initialization_scenario1, plan_date: Date.current, window_from: 8, window_to: 10)
    end
  end

  describe '#boolean_show' do
    it 'returns a span including yes as a class if value is present' do
      expect( subject.boolean_show "a" ).to eq '<i class="boolean-show yes checkmark box icon"></i>'
    end
    it 'returns a span including no as a class if value is not present' do
      expect( subject.boolean_show "" ).to eq '<i class="boolean-show no square outline icon"></i>'
    end
  end

  describe '#search_result_info' do
    it "displays the found results" do
      5.times { create :workflow_instance }
      expect( subject.search_result_info WorkflowInstance.all.page(2).per(2) ).to eq "3 - 4 / #{WorkflowInstance.count}"
    end
  end

  describe '#page_title' do
    it 'works with a custom string' do
      expect( subject.page_title "Custom title" ).to eq '<h3 class="page-title ui header">Custom title</h3>'
    end
  end

  describe '#plan_date_for_record' do
    it 'display all info' do
      record = double plan_date: '2015-12-4'.to_date, window_from: 2, window_to: 5
      expect( subject.plan_date_for_record record ).to eq  '04-12-2015 &nbsp;<span class="fa fa-clock-o"></span>&nbsp;&nbsp;2:00&nbsp;-&nbsp;5:00'
    end
    it "displays without window_to" do
      record = double plan_date: '2015-12-4'.to_date, window_from: 2, window_to: nil
      expect( subject.plan_date_for_record record ).to eq  '04-12-2015 &nbsp;<span class="fa fa-clock-o"></span>&nbsp;&nbsp;2:00&nbsp;-'
    end

    it "does not crash on blank record" do
      expect( subject.plan_date_for_record nil ).to eq  ''
    end
  end

  describe '#show_address' do
    it 'gives a proper address' do
      record = build :workflow_instance_scenario1, zipcode: '2554', housenumber: 22, housenumber_ext: 'E'
      expect( subject.show_address record ).to eq "2554 22E"
    end
  end

  describe "#human_duration" do
    it "uses translations" do
      subject.human_duration(1.day + 1.day).should eq "2 dagen"
      subject.human_duration(3.days + 2.months).should eq "2 maanden en 3 dagen"
      subject.human_duration(1.day + 1.month + 3.years).should eq "3 jaar, 1 maand en 1 dag"
      subject.human_duration(-2.months).should eq "-2 maanden"
    end
  end

  describe "#tarray" do
    it "works with normal arrays" do
      subject.tarray(%w[ a b c]).should eq [['a', 'a'], ['b', 'b'], ['c', 'c']]
    end

    it "works with models" do
      expect(subject).to receive(:can?).with(:read, WorkflowInstance::Scenario1).and_return true
      expect(subject).to receive(:can?).with(:read, OwnerInitialization::Scenario1).and_return false

      subject.tarray([WorkflowInstance::Scenario1, OwnerInitialization::Scenario1]).should eq [
        ["SC1", WorkflowInstance::Scenario1]
      ]
    end
  end

  describe "#link_to_model" do
    it "handles nil parameter" do
      subject.link_to_model(nil).should_not be_present
    end

    it "handles models without a defined url and presentation name" do
      model = Dunlop::LogEntry.create
      expect { subject.polymorphic_path [dunlop, model] }.to raise_error NoMethodError
      expect { subject.polymorphic_path [main_app, model] }.to raise_error NoMethodError
      expect(subject).to receive(:can?).with(:read, model).and_return true
      subject.link_to_model(model).should_not be_present
    end

    it "handles models without a defined url but with presentation name" do
      model = create :user, email: "user-link-to-model234@example.com"
      expect { subject.polymorphic_path [main_app, model] }.to raise_error NoMethodError
      expect(subject).to receive(:can?).with(:read, model).and_return true
      subject.link_to_model(model).should eq "user-link-to-model234@example.com"
    end

    it "returns a proper link for main path" do
      expect(subject).to receive(:can?).with(:read, WorkflowInstance::Scenario1).and_return true
      model = create(:workflow_instance_scenario1, my_name: 'Hellooooo')
      subject.link_to_model(model).should eq %|<a href="/workflow_instance/scenario1s/#{model.id}">Hellooooo</a>|
    end

    it "returns a proper link for a dunlop handled model" do
      model = create :execution_batch_my_batch
      expect(subject).to receive(:can?).with(:read, model).and_return true
      subject.link_to_model(model).should eq %|<a href="/dunlop/execution_batches/#{model.id}">Uitvoerings batch</a>|
    end

    it "does not return a link for a model without permission" do
      model = create :execution_batch_my_batch
      expect(subject).to receive(:can?).with(:read, model).and_return false
      subject.link_to_model(model).should_not be_present
    end
  end

  describe "#collapsible_content" do
    it "Works with simple title argument" do
      result = subject.collapsible_content "My Title" do
        "I am collapsed"
      end
      result.should eq '<div class="collapsible-container collapsed"><div class="collapsible-title"><span></span>My Title</div><div class="collapsible-content">I am collapsed</div></div>'
    end

    it "works with extra classes and id" do
      result = subject.collapsible_content "My Title", class: 'abc', id: 'def' do
        "I am collapsed"
      end
      result.should eq '<div class="abc collapsible-container collapsed" id="def"><div class="collapsible-title"><span></span>My Title</div><div class="collapsible-content">I am collapsed</div></div>'
    end

    it "works without block with content argument" do
      result = subject.collapsible_content "My Title", content: "I am collapsed"
      result.should eq '<div class="collapsible-container collapsed"><div class="collapsible-title"><span></span>My Title</div><div class="collapsible-content">I am collapsed</div></div>'
    end

    it "does not add the collapsed class when argument is given as false" do
      result = subject.collapsible_content "My Title", content: "I am collapsed", collapsed: false
      result.should eq '<div class="collapsible-container "><div class="collapsible-title"><span></span>My Title</div><div class="collapsible-content">I am collapsed</div></div>'
    end

    it "works with class as array argument" do
      result = subject.collapsible_content "My Title", class: ['abc', 'def'] do
        "I am collapsed"
      end
      result.should eq '<div class="abc def collapsible-container collapsed"><div class="collapsible-title"><span></span>My Title</div><div class="collapsible-content">I am collapsed</div></div>'
    end
  end
end
