module Panel::PersonalBook::Desktop::PersonalBooksHelper
  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 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 "book_title"
      des = content_tag(:div, publication.book_title,
        :class => "list_t_des")
    when "keywords"
      des = content_tag(:div, publication.keywords,
        :class => "list_t_des")
    when "publisher"
      des = content_tag(:div, publication.publisher,
        :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_book_desktop_personal_book_path(publication),
       "callback-method" => "editbook",
       "ajax-remote" => "get") + \
      content_tag(:a, t(:delete_),
        "ajax-remote" => "delete",
        "confirm-message" => t("sure?"),
        "callback-method" => "bookDelete",
        :class => "journal_paper_delete admbg2 admtxt",
        :href => panel_personal_book_desktop_personal_book_path(publication))
    end
  end

  def link_publication_file publication
    publication.writing_book_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?
      file_type = MIME::Types.type_for(file).first.to_s.split("/")[1]
      file_type = "/assets/ft-icons/#{file_type}/#{file_type}-48_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 => ConferenceCoAuthor.find(m).co_author, :email => ConferenceCoAuthor.find(m).email}
        {:id => m, :text => ConferenceCoAuthor.find(m).co_author}
      end
    }
    author_name.to_json
  end
end
