require 'rails_helper'

feature "Show badge info" do
  describe "Not linked to service provider" do
    background do
      i_am_logged_in_as_admin
      @workflow_instance_batch = create :workflow_instance_batch_scenario1, name: 'Batchy'
      @dslam_1 = create :workflow_instance_scenario1, :active, my_name: 'DSLA/111'
      @dslam_2 = create :workflow_instance_scenario1, :active, plan_date: '2019-03-09', my_name: 'DSLA/222', workflow_instance_batch: @workflow_instance_batch
      @dslam_3 = create :workflow_instance_scenario1, my_name: 'DSLA/333', workflow_instance_batch: @workflow_instance_batch
      @dslam_4  = create :workflow_instance_scenario1, my_name: 'DSLA/444'
      @owner_initialization = @dslam_3.owner_initialization
      @owner_initialization.replace_notes 'PreparationNotes'
      @owner_initialization.is_overdue!
    end

    describe 'Statistics' do
      it "Shows the dslams for a workflow_instance state", js: true do
        visit root_path
        badge = find('.workflow_instance.scenario1.active.display-badge')
        expect( badge.text ).to eq 'Actief (2)'
        badge.click
        within '#display-badge-info-popup' do
          expect( page ).to have_content 'DSLA/111'
          expect( page ).to have_content 'DSLA/222'
          expect( page ).not_to have_content 'DSLA/333'
          expect( page ).not_to have_content 'DSLA/444'

          click_on "Toon lijst"
        end
        page.current_url.should include "/workflow_instance/scenario1s?q%5Bstate_eq%5D=active"
        expect( page ).to have_content 'DSLA/111'
        expect( page ).to have_content 'DSLA/222'
        expect( page ).not_to have_content 'DSLA/333'
        expect( page ).not_to have_content 'DSLA/444'
      end

      it "Show the dslams for a subprocess state", js: true do
        visit root_path
        badge = find('.owner_initialization.scenario1.overdue.display-badge')
        expect( badge.text ).to eq 'Overdue (1)'
        badge.click
        within '#display-badge-info-popup' do
          page.should_not have_content 'DSLA/111'
          page.should_not have_content 'DSLA/222'
          page.should     have_content 'DSLA/333'
          page.should_not have_content 'DSLA/444'
        end
      end
    end

    describe "Actual record" do

      it "Shows basic information for the workflow_instance record", js: true, disabled: true do # disabled because plan_date out of scope
        visit workflow_instance_scenario1s_path
        badge = find(%|[data-record='{"id":#{@dslam_2.id}}'] .workflow_instance.scenario1.display-badge|)
        badge.click
        within '#display-badge-info-popup' do
          expect( page ).to have_content '09-03-2019'
        end
      end

      it "Shows basic information for the subprocess record", js: true do
        visit workflow_instance_scenario1s_path
        badge = find(%|[data-record='{"id":#{@dslam_3.id}}'] .owner_initialization.scenario1.display-badge|)
        badge.click
        within '#display-badge-info-popup' do
          expect( page ).to have_content 'PreparationNotes'
        end
      end
    end

    describe "Batch scope" do
      it "Shows batch limited informatio", js: true do
        # Create bogus not batch records, if batch is not activated on
        # query, two records are found in stead of 1
        dslam_4 = create :workflow_instance_scenario1, :active, my_name: 'DSLA/444'
        dslam_4.owner_initialization.update state: 'processing'

        visit workflow_instance_batch_scenario1s_path
        badge = find(%|.contractor_initialization.scenario1.pending.display-badge|)
        expect( badge.text ).to eq 'Contr. init (2)'
        badge.click
        within '#display-badge-info-popup' do
          expect( page ).to have_content 'DSLA/222'
          expect( page ).not_to have_content 'DSLA/444'
        end
        #TODO: workflow step filtering
        #click_on "Toon lijst"
        #page.current_url.should include "/workflow_instance/scenario1s?q%5Bworkflow_instance_batch_id_eq%5D=#{@workflow_instance_batch.id}&q%5Bworkflow_step%5D%5Bowner_initialization%5D=pending"
      end
    end
  end

end
