class Commit
  include ActiveModel::Conversion
  include Gitlabhq::Encode
  extend ActiveModel::Naming

  attr_accessor :commit
  attr_accessor :head
  attr_accessor :refs

  delegate :message,
    :authored_date,
    :committed_date,
    :parents,
    :sha,
    :date,
    :committer,
    :author,
    :message,
    :diffs,
    :tree,
    :id,
    :to => :commit


  class << self 
    def find_or_first(repo, commit_id = nil)
      commit = if commit_id
                 repo.commit(commit_id)
               else
                 repo.commits.first
               end
      Commit.new(commit) if commit
    end

    def fresh_commits(repo, n = 10)
      commits = repo.heads.map do |h|
        repo.commits(h.name, n).map { |c| Commit.new(c, h) }
      end.flatten.uniq { |c| c.id }

      commits.sort! do |x, y|
        y.committed_date <=> x.committed_date
      end

      commits[0...n]
    end

    def commits_with_refs(repo, n = 20)
      commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }

      commits.sort! do |x, y|
        y.committed_date <=> x.committed_date
      end

      commits[0..n]
    end

    def commits_since(repo, date)
      commits = repo.heads.map do |h|
        repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) }
      end.flatten.uniq { |c| c.id }

      commits.sort! do |x, y|
        y.committed_date <=> x.committed_date
      end

      commits
    end

    def commits(repo, ref, path = nil, limit = nil, offset = nil)
      if path
        repo.log(ref, path, :max_count => limit, :skip => offset)
      elsif limit && offset
        repo.commits(ref, limit, offset)
      else
        repo.commits(ref)
      end.map{ |c| Commit.new(c) } 
    end

    def commits_between(repo, from, to)
      repo.commits_between(from, to).map { |c| Commit.new(c) }
    end
  end

  def persisted?
    false
  end

  def initialize(raw_commit, head = nil)
    @commit = raw_commit
    @head = head
  end

  def safe_message
    utf8 message
  end

  def created_at
    committed_date
  end

  def author_email
    author.email
  end

  def author_name
    utf8 author.name
  end

  # Returns the commits title.
  #
  # Usually, the commit title is the first line of the commit message.
  # In case this first line is longer than 80 characters, it is cut off
  # after 70 characters and ellipses (`&hellp;`) are appended.
  #
  # @todo This might be better placed in a view helper.
  def title
    title_end = safe_message.index(/\n/)
    if (!title_end && safe_message.length > 80) || (title_end && title_end > 80)
      safe_message[0..69] << "&hellip;".html_safe
    else
      safe_message.split(/\n/, 2).first
    end
  end

  # Returns the commits description
  #
  # cut off, ellipses (`&hellp;`) are prepended to the commit message.
  #
  # @todo This might be better placed in a view helper.
  def description
    title_end = safe_message.index(/\n/)
    if (!title_end && safe_message.length > 80) || (title_end && title_end > 80)
      "&hellip;".html_safe << safe_message[70..-1]
    else
      safe_message.split(/\n/, 2)[1].try(:chomp)
    end
  end

  # Was this commit committed by a different person than the original author?
  def different_committer?
    author_name != committer_name || author_email != committer_email
  end

  def committer_name
    utf8 committer.name
  end

  def committer_email
    committer.email
  end

  def prev_commit
    parents.first
  end

  def prev_commit_id
    prev_commit.id
  end
end