class TemplateContext < CmsContext

  def create_template(name)
    @name = name
    click_a_tag('Add Template')
    within('#new-entity') do
      fill_in_by_data_field('name', name)
      click_and_wait('Add Template')
    end
  end

  def delete_field
    @old_order_and_ids = get_order_and_id_hash(Field.where(template_id: @template_id))
    field = Field.find_by(name: @field_value, template_id: @template_id)
    @deleted_order = field.order
    delete_and_save("#field-#{@deleted_order}")
  end

  def click_template_square(name)
    find('.template-square', text: name).click
  end

  def open_type_dropdown
    path = all("[data-field='fieldType']", visible: false).first.path
    within(:xpath, get_ancestor_path(path)) do
      find('div.nice-select').click
    end
  end

  def expect_on_edit_page(name)
    id = Template.find_by(name: name).id
    expect(current_path).to eq("/admin/templates/#{id}")
  end

  def expect_in_database
    template = Template.last
    expect(template.name).to eq(@name)
  end

  def expect_correct_field_values
    @template_fields.each do |row_number, field_values|
      within("#field-#{row_number}") do
        field_values.each do |field, value|
          expect(find_by_data_field(make_lazy_camel_case(field)).value).to eq(value)
        end
      end
    end
  end

  def expect_not_in_database(name)
    expect(Template.find_by(name: name)).to be_falsey
  end

  def expect_field_to_exist(name)
    field = Field.find_by(name: name, template_id: @template_id)
    expect_same_field_values(field, @field_values)
  end

  def expect_one_field(name)
    fields = Field.where(template_id: @template_id, name: name)
    expect(fields.length).to eq(1)
  end

  def expect_field_not_in_database(name)
    field = Field.find_by(name: name, template_id: @template_id)
    expect(field).to be_falsey
  end

  def expect_names_alphabetized
    templates = all('.template-square').map do |element|
      element.text
    end
    expect_sorted_text(templates)
  end

  def expect_order_rearranged
    expect(order_rearranged?(Field.where(template_id: @template_id))).to be_truthy
  end

  def expect_not_in_type_dropdown(options)
    options.each do |option|
      within('.list') { expect_content_not_on_page(option.first) }
    end
  end

  private

  def get_step_templates
    {
      'All Fields' => {
        0 => {
          'name' => 'Text',
          'mandatory' => 'yes',
          'char_limit' => '150',
          'field_type' => 'text'
        },
        1 => {
          'name' => 'Image',
          'field_type' => 'image'
        },
        2 => {
          'name' => 'Object',
          'field_type' => 'object',
          'object_type' => 'School'
        },
        3 => {
          'name' => 'Color',
          'field_type' => 'color'
        },
        4 => {
          'name' => 'Number',
          'field_type' => 'number'
        }
      },
      'School' => {
        0 => {
          'name' => 'Creator',
          'mandatory' => 'yes',
          'char_limit' => '150',
          'field_type' => 'text'
        }
      }
    }
  end

  def get_step_fields
    {
      'Name' => {
        'mandatory' => 'no',
        'char_limit' => '150',
        'field_type' => 'text'
      },
      'Creator' => {
        'mandatory' => 'yes',
        'char_limit' => '25',
        'field_type' => 'text'
      }
    }
  end

  def set_original_field_values(field)
    template_field = Field.find_by(field => @field_value, template_id: @template_id)
    @field_values = Field.column_names.map {|name|[name, convert_to_frontend_format(template_field[name])]}.to_h
  end

  def expect_field_values_in_database(db_fields)
    @template_fields.each do |row_number, field_values|
      db_field = db_fields.select {|field| field.order == row_number}.first
      expect_same_field_values(db_field, field_values)
      expect(db_field['char_limit']).to eq(0) unless field_values['char_limit']
    end
  end
end
