class ImageFieldContext < CmsContext

  def initialize
    super
    @partials = [{'name' => 'Big Image', 'devs_only' => 'false'}]
  end

  def given_an_image(field_name)
    image = create_image('page_partial')
    partial_field = PartialField.find_by(name: field_name, field_type: 'image')
    image_field = PagePartialField.find_by(partial_field_id: partial_field.id)
    image_field.update!(value: image.id)
  end

  def create_reusable_image
    create_image('page_partial', true)
  end

  def upload_image
    expand_page_partial('.page-partial')
    within('.page-partials-index') do
      click_a_tag('Upload Image')
    end
    wait_until { !Upload.last.nil? }
    @src = Upload.last.url
  end

  def remove_image
    expand_page_partial('.page-partial')
    click_and_wait("Remove Image")
    @src = ''
  end

  def upload_reusable_image
    expand_page_partial('.page-partial')
    click_a_tag('Reusable Images')
    within('.reusable-images-container') do
      image = find('img')
      @src = image[:src]
      image.click
    end
  end

  def expect_reusable_in_database
    expect(Upload.find_by(url: @src).reusable).to eq(true)
  end

  def expect_correct_src
    if @src.present?
      within('.instance-fields') do
        expect(find('img')[:src]).to eq(@src)
      end
    else
      within('.instance-fields') do
        expect(find('div.empty'))
      end
    end
  end

  private

  def get_partial_fields
    {
      'Big Image' => {
        'image' => {
          'field_names' => ['Image'],
          'char_limit' => '0',
          'mandatory' => 'false'
        },
        'number' => {
          'field_names' => ['Computer Margin Bottom', 'Tablet Margin Bottom', 'Mobile Margin Bottom', 'Mobile/Tablet Height', 'Fixed Height Desktop (Optional)'],
          'char_limit' => '0',
          'mandatory' => 'false'
        }
      }
    }
  end

end
