module Panel::PersonalJournal::Desktop::JournalPagesHelper
  def publication_record publication, view
   
    content_tag :li,
     "item" => "true",
       "data-id" => publication.id.to_s,
      :class => "list_t_item #{view.blank? ? '' : "#{view}_view" } #{publication.is_hidden? ? "private" : "public" }" do

      content_tag :div,
        :class => "inner" do
          marker(publication.id) + \
          content(publication, view) + \
          edit_or_delete(publication)
        end
      end
  end

  def get_height view
    case view
    when "journal_title"
     height = "50px"
    when "keywords"
     height = "100px"
    when "abstract"
     height = "200px"
    when "file"
      height = "200px"
    else
      height = "50px"
    end
    height
  end

  def marker id
    content_tag :div,
     :class => "list_item_action" do
      content_tag(:a, "",:href=>"",
                  :class => "icon-check-empty",
                  "toggle-onclick"=>"icon-check-empty icon-check",
                     "data-id" => id.to_s,
                  "ajax-remote"=>"false") + \
      content_tag(:a, "",:href=>"",
                  :class => "icon-star-empty",
                  "toggle-onclick"=>"icon-star-empty icon-star",
                     "data-id" => id.to_s,
                  "ajax-remote"=>"false")
    end
  end

  def content publication, view
    case view
    when "journal_title"
      des = content_tag(:div, publication.journal_title,
        :class => "list_t_des")
    when "keywords"
      des = content_tag(:div, publication.keywords,
        :class => "list_t_des")
    when "abstract"
      des = content_tag(:div, publication.abstract,
        :class => "list_t_des")
    when "file"
      des = content_tag(:div, link_publication_file(publication),
        :class => "list_t_des")
    else
    end

    content_tag(:div, publication.paper_title,
      :class => "list_t_title")  + des

  end

  def edit_or_delete publication
    content_tag :div,
      :class => "list_item_function" do
      content_tag(:a, t("edit"),
       :class => "journal_paper_edit admbg2 admtxt",
       :href => edit_panel_personal_journal_desktop_journal_page_path(publication),
       "callback-method" => "editpaper",
       "ajax-remote" => "get") + \
      content_tag(:a, t(:delete_),
        "ajax-remote" => "delete",
        "confirm-message" => t("sure?"),
        "callback-method" => "paperDelete",
        "delete-item" => "true",
        :class => "journal_paper_delete admbg2 admtxt",
        :href => panel_personal_journal_desktop_journal_page_path(publication))
    end
  end

  def link_publication_file publication
    publication.writing_journal_files.map{|file|
      
      link_to(image_tag(check_file_type(file.file.url)) + \
        content_tag(:span, (file.title_translations[I18n.locale.to_s] rescue nil), :class => "filetitle"),
        file.file.url,
        :class => "file",
        "target" => "_blank",
        "title" => (file.title_translations[I18n.locale.to_s] rescue nil))
    }.inject(:+)
  end

  def check_file_type file
    if not file.nil?
      x = file.split(/[\s\.]/)
      file_type = x[x.length - 1]
      file_type = file_type.downcase
      file_type = "/assets/ft-icons/#{file_type}/#{file_type}-sm-32_32.png"
    else
      file_type = ""
    end
  end

  def generate_authors_name ids
    author_name = ids.map{|m|
      if m == "0"
        #{:id => 0, :text => current_user.name, :email => current_user.email }
        {:id => 0, :text => current_user.name}
      else
        #{:id => m, :text => JournalCoAuthor.find(m).co_author, :email => JournalCoAuthor.find(m).email}
        {:id => m, :text => JournalCoAuthor.find(m).co_author}
      end
    }

    author_name.to_json
  end
end
