# require ActionView::Helpers::FormTagHelper

module AttributeFieldsHelper
  include ActionView::Helpers::FormTagHelper
  include ActionView::Helpers::FormOptionsHelper
  include ActionView::Helpers::DateHelper
  include ActionView::Helpers::TagHelper

  def block_helper(user,index)
    @index = index
    @user = user
    @attribute_value = @user.get_value_from_field_id(id) 
    @new_attribute = @attribute_value.nil?  
    @attribute_value = @attribute_value || @user.attribute_values.build(attribute_field_id: id)
    @prefiled_value = @attribute_value.get_values
    return instance_eval("render_#{markup}") #rescue ""
  end

  def render_text_area
    result = ""
    result << label
    if self.locale?
      @prefiled_value.each do |key,value|
        result << controls_wrapper{text_area_tag("#{get_field_name_base}[#{key.to_s}]",  value, {})}
      end
    else
      value = @prefiled_value
      result << controls_wrapper{text_area_tag(get_field_name_base,  value, {})}
    end
    result << end_block
    result.html_safe 
  end

  def render_radio_button
    result = ""
    result << label
    markup_value.each do |key,value|
      result <<  controls_wrapper{label_tag(key,radio_button_tag(get_field_name_base+"[#{key}]", value[I18n.locale.to_s],  (@prefiled_value==value ? true : false), {})+value[I18n.locale.to_s],:class=>"control-label")}
    end
    result << end_block
    result.html_safe
  end

  def render_checkbox
    # label+ "" + end_block
    # check_box_tag(name, value = "1", checked = false, options = {})

    result = ""
    result << label
    markup_value.each do |key,value|
      result <<  controls_wrapper{label_tag(key,check_box_tag(get_field_name_base+"[#{key}]", value[I18n.locale.to_s],  (@prefiled_value==value ? true : false), {})+value[I18n.locale.to_s],:class=>"control-label")}
    end
    result << end_block
    result.html_safe

  end

  def render_date_durnation
    control_group_wrapper do 
      if @new_attribute
        @prefiled_value = {}
      end
      label+controls_wrapper{date_select(get_field_name_base+"[value][from]",@prefiled_value["from"]) +date_select(get_field_name_base+"[value][end]",@prefiled_value["end"])} + end_block
    end
  end

  def render_text_field_ext
    result = ""
    result << label
    if self.locale?
      @prefiled_value.each do |key,value|
        result << controls_wrapper{text_field_tag("#{get_field_name_base}[#{key.to_s}]",  value, {})}
      end
    else
      value = @prefiled_value
      result << controls_wrapper{text_field_tag(get_field_name_base,  value, {})}
    end
    result << end_block
    result.html_safe 
  end


  def render_date
    label+controls_wrapper{date_select(get_field_name_base+"[value]",@prefiled_value)} + end_block
  end

  def render_select
    label+controls_wrapper{select_tag( get_field_name_base+"[value]",options_for_select(markup_value.collect{|p| [p[1][I18n.locale.to_s],p[0]]},@prefiled_value))} + end_block
  end

  def render_text_field
    result = ""
    result << label
    if self.locale?
      @prefiled_value.each do |key,value|
        result << controls_wrapper{text_field_tag("#{get_field_name_base}[#{key.to_s}]",  value, {})}
      end
    else
      value = @prefiled_value
      result << controls_wrapper{text_field_tag(get_field_name_base,  value, {})}
    end
    result << end_block
    result.html_safe 
  end

  def render_email
    label+controls_wrapper{email_field_tag(get_field_name_base, value,  {})}+ end_block
  end

  def render_addr
    label+controls_wrapper{text_field_tag(get_field_name_base, value,  {})} + end_block
  end

protected

  def controls_wrapper(&block)
    result = "<div class='controls'>"
    result << yield
    result << "</div>"
    result.html_safe
  end

  def control_group_wrapper(&block)
    result = "<div class='control-group'>"
    result << yield
    result << "</div>"
    result.html_safe
  end

  def end_block
    if @new_attribute
      hidden_field_tag(get_field_name_base+"[attribute_field_id]",id)
    else
      hidden_field_tag(get_field_name_base+"[id]",@attribute_value.id)
    end
  end

  def get_field_name_base
    # "user[#{self.attribute._type.downcase.pluralize}][#{self.attribute._id.to_s}][attribute_values][#{attribute_value.id}]"
    if @new_attribute
      "user[new_attribute_values][#{@index}]"
    else
      "user[attribute_values][#{@index}]"
    end
  end

  def label
    label_tag(key,title,:class=>"control-label")
  end

end