class GraduationWork < ApplicationRecord
  # work_status: 0 未提交, 1 按时提交, 2 延时提交
  belongs_to :user
  belongs_to :course
  belongs_to :project, optional: true
  belongs_to :graduation_task, optional: true

  belongs_to :commit_user, class_name: 'User', foreign_key: :commit_user_id, optional: true
  belongs_to :update_user, class_name: 'User', foreign_key: :update_user_id, optional: true
  has_many :attachments, as: :container, dependent: :destroy
  has_many :tidings, as: :container, dependent: :destroy
  has_many :graduation_work_scores, dependent: :destroy

  has_many :graduation_work_comment_assignations, dependent: :destroy
  # has_many :formal_graduation_work_comment_assignations, -> { formal }, class_name: "GraduationWorkCommentAssignation"
  # has_many :temporary_graduation_work_comment_assignations, -> { temporary }, class_name: "GraduationWorkCommentAssignation"

  has_many :graduation_task_group_assignations, dependent: :destroy
  # has_many :formal_graduation_task_group_assignations, -> { formal }, class_name: "GraduationTaskGroupAssignation"
  # has_many :temporary_graduation_task_group_assignations, -> { temporary }, class_name: "GraduationTaskGroupAssignation"

  validates :description, length: { maximum: 5000, too_long: "不能超过5000个字符" }

  scope :has_committed, lambda { where("work_status != 0") }

  # 未提交
  scope :unfinished, -> {where(work_status: 0)}
  # 按时提交
  scope :finished, -> {where(work_status: 1)}
  # 延迟提交
  scope :delay_finished, -> {where(work_status:  2)}

  #根据graduation_task_id查找
  scope :find_by_task, lambda{|ids| where(graduation_task_id: ids)}
  scope :find_by_task_user, lambda{|ids| where(user_id: ids)}

  before_save :set_work_score

  def set_work_score
    unless self.ultimate_score
      if self.teacher_score.present? && self.cross_score.nil?
        self.final_score = self.teacher_score
      elsif self.cross_score.present? && self.teacher_score.nil?
        self.final_score = self.cross_score
      elsif self.teacher_score.present? && self.cross_score.present?
        self.final_score = ((self.cross_score + self.teacher_score).to_f / 2.0).try(:round, 2)
      end
      if self.final_score
        score = self.final_score - self.late_penalty
        self.work_score = (score < 0 ? 0 : score).to_f.try(:round, 2) if score
      else
        self.work_score = nil
      end
    end
  end

  def delete_atta atta
    last_score = graduation_work_scores.last
    (atta.author_id == User.current.id) && (last_score.blank? || last_score.try(:created_at) < atta.created_on)
  end

  # 分班名
  def class_grouping_name
    CourseMember.find_by(course_id: self.course_id, user_id: self.user_id, role: 4).try(:course_group).try(:name) || '未分班'
  end

  # 分组名
  def grouping_name
    self.group_id == 0 ?  "--" : "分组#{self.group_id}"
  end

  #用户是否有查看分数的权限
  def check_score_power? current_user, course_identity
    self.work_score.present? && (course_identity < Course::STUDENT || self.user_id = current_user.id || graduation_task.open_score)
  end

  # 作品是否能够分配指导老师
  def assign_power?(course_identity)
    course_identity < Course::STUDENT && graduation_task.cross_comment && graduation_task.comment_status == 2
  end

  # 老师评阅分
  def teacher_comment_score current_user, course_identity
    # 为提交
    if self.work_status == 0
      "--"
    else
      # 未打分
      if self.teacher_score.nil?
        "未批阅"
      else
        # 是否有权限看
        if self.check_score_power?(current_user, course_identity)
          format("%.1f", self.teacher_score.round(1))
        else
          "**"
        end
      end
    end
  end

  # 交叉评阅分
  def cross_comment_score current_user, course_identity
    if self.work_status == 0
      "--"
    else
      if self.cross_score.nil?
        "未批阅"
      else
        if self.check_score_power?(current_user, course_identity)
          "#{format("%.1f", self.cross_score.round(1))}"
        else
          "**"
        end
      end
    end
  end

  # 作品被交叉评阅的次数
  def cross_comment_num
    graduation_work_scores.where(reviewer_role: 2).group_by(&:user_id).count
  end

  # 作品是否被评阅过
  def scored?
    graduation_work_scores.where.not(score: nil).exists?
  end

  def work_cross_teacher_ids
    graduation_work_comment_assignations.temporary_formal.pluck(:user_id)
  end

  def work_cross_teachers
    User.where(id: work_cross_teacher_ids).map(&:real_name).join("、")
  end

  def work_cross_group_ids
    graduation_task_group_assignations.temporary_formal.pluck(:graduation_group_id)
  end

  def work_cross_groups
    course.graduation_groups.where(id: work_cross_group_ids).pluck(:name).join("、")
  end
end
