module HomeworkCommonsHelper

  # 未发布时非老师角色不能访问,发布后非课堂成员不能访问未公开的作业,学生需要考虑分班设置的作业是否已发布
  def homework_publish
    if (@user_course_identity >= Course::STUDENT && (@homework.publish_time.nil? || @homework.publish_time > Time.now)) ||
        (@user_course_identity > Course::STUDENT && (!@homework.unified_setting || @course.is_public == 0 || !@homework.is_public))
      tip_exception(403,"没有操作权限")
    elsif @user_course_identity == Course::STUDENT && !@homework.unified_setting
      member = @course.course_member(current_user.id)
      group_setting = @homework.homework_group_settings.find_by(course_group_id: member.try(:course_group_id))
      if group_setting.try(:publish_time).nil? || group_setting.try(:publish_time) > Time.now
        tip_exception(403,"没有操作权限")
      end
    end
  end

  def homework_curr_status homework_common, identity, course, member, teacher_course_groups
    result = {}
    status = []
    time = ""
    time_status = nil
    if course.try(:is_end)
      status << "已结束"
      time = course.end_date.strftime("%Y-%m-%d")
      time_status = 6
    else
      if homework_common.end_time && homework_common.end_time < Time.now && homework_common.allow_late &&
          (homework_common.late_time.nil? || homework_common.late_time > Time.now)
        status << "补交中"
      end

      ho_detail_manual = homework_common.homework_detail_manual
      if ho_detail_manual
        # 作业状态大于“提交”状态时,不用考虑分班权限
        if ho_detail_manual.comment_status > 1
          case ho_detail_manual.comment_status
            when 3
              if ho_detail_manual.evaluation_end && ho_detail_manual.evaluation_end > Time.now
                status << "匿评中"
                time = how_much_time(ho_detail_manual.evaluation_end)
                time_status = 3
              end
            when 4
              if ho_detail_manual.appeal_time && ho_detail_manual.appeal_time > Time.now
                status << "申诉中"
                time = how_much_time(ho_detail_manual.appeal_time)
                time_status = 4
              end
            when 2, 5, 6
              status << "评阅中"
              time = course.end_date.present? ? how_much_time(course.end_date.end_of_day) : ""
              time_status = 5
          end

          # 如果还在补交阶段则显示补交结束时间
          if homework_common.end_time && homework_common.end_time < Time.now && homework_common.allow_late &&
              homework_common.late_time && homework_common.late_time > Time.now
            time = how_much_time(homework_common.late_time)
            time_status = 2
          end
        else
          # member = course.course_members.find_by(user_id: user.id, is_active: 1)
          # teacher_course_groups = member.try(:teacher_course_groups)
          # identity = user.course_identity(course)

          # 作业统一设置、游客身份、超级管理员、分班权限不限的老师身份
          if homework_common.unified_setting || identity > Course::STUDENT || identity == Course::ADMIN ||
              (identity < Course::STUDENT && teacher_course_groups.blank?)
            case ho_detail_manual.comment_status
              when 0
                status << "未发布"
                time = homework_common.publish_time.present? ? "将于 #{format_time(homework_common.publish_time)} 发布" : "创建于#{time_from_now(homework_common.created_at)}"
                time_status = 0
              when 1
                if homework_common.end_time && homework_common.end_time >= Time.now
                  status << "提交中"
                  time = how_much_time(homework_common.end_time)
                  time_status = 1
                elsif homework_common.end_time && homework_common.end_time < Time.now
                  time_status = 5
                  if homework_common.allow_late && (homework_common.late_time.nil? || homework_common.late_time >= Time.now)
                    time = how_much_time(homework_common.late_time)
                    time_status = 2
                  end
                  status << "评阅中"
                end
            end
          else
            # 未分班的学生始终显示“未发布”(按理不会来到这个判断)
            if member.role == "STUDENT" && member.course_group_id == 0
              status << "未发布"
              time = ""
              time_status = 0
            else
              if member.role == "STUDENT"
                setting = homework_common.homework_group_settings.find_by(course_group_id: member.course_group_id)
                min_publish_time = setting.try(:publish_time)
                max_end_time = setting.try(:end_time)
              else
                # 多个分班权限的取最小publish_time,最大end_time
                min_publish_time = homework_common.homework_group_settings.where.not(publish_time: nil).
                    where(course_group_id: teacher_course_groups.pluck(:course_group_id)).pluck(:publish_time).min
                max_end_time = homework_common.homework_group_settings.where.not(end_time: nil).
                    where(course_group_id: teacher_course_groups.pluck(:course_group_id)).pluck(:end_time).max
              end

              if min_publish_time.nil?
                status << "未发布"
                time = "创建于#{time_from_now(homework_common.created_at)}"
                time_status = 0
              elsif min_publish_time > Time.now
                status << "未发布"
                time = "将于 #{format_time(min_publish_time)} 发布"
                time_status = 0
              elsif max_end_time.present? && max_end_time > Time.now   #6.14 -hs  添加present?
                status << "提交中"
                time = how_much_time(max_end_time)
                time_status = 1
              elsif homework_common.allow_late && (homework_common.late_time.nil? || homework_common.late_time >= Time.now)
                status << "评阅中"
                time = how_much_time(homework_common.late_time)
                time_status = 2
              else
                status << "评阅中"
                time = ""
                time_status = 5
              end
            end
          end
          status << "未开启补交" if !homework_common.allow_late && time_status != 0
        end
      end
    end
    # 如果作业状态都没有的话,在课堂结束前,都显示评阅中
    if status.blank?
      status << "评阅中"
    end
    result[:status] = status
    result[:time] = time
    result[:time_status] = time_status
    result
  end


  # 阶段剩余时间
  def left_time homework, user_id
    setting = homework.homework_group_setting(user_id)
    if setting.publish_time && setting.publish_time < Time.now
      if setting.end_time > Time.now
        status = "剩余提交时间"
        time = "#{how_much_time(setting.end_time)}"
      else
        ho_detail_manual = homework.homework_detail_manual
        case ho_detail_manual.comment_status
        when 3
          if ho_detail_manual.evaluation_end && ho_detail_manual.evaluation_end > Time.now
            status = "剩余匿评时间"
            time = "#{how_much_time(ho_detail_manual.evaluation_end)}"
          end
        when 4
          if ho_detail_manual.appeal_time && ho_detail_manual.appeal_time > Time.now
            status = "剩余申诉时间"
            time = "#{how_much_time(ho_detail_manual.appeal_time)}"
          end
        else
          if homework.allow_late && homework.late_time && homework.late_time >= Time.now
            status = "剩余补交时间"
            time = "#{how_much_time(homework.late_time)}"
          end
        end
      end
    end
    {status: status, time: time}
  end

  # 作品数统计:type: 1 已提交  0 未提交
  def studentwork_count homework_common, type, member
    student_works = homework_common.teacher_works(member)
    type == 1 ? student_works.where("work_status != 0").size : student_works.where(work_status: 0).size
  end

  # 上次查重的时间
  def last_review_time homework_common, course_group
    review = homework_common.homework_group_reviews.where(:course_group_id => course_group.id).last
    review ? (format_time review.created_at) : "--"
  end

  # 分班的有效作品数
  def homework_works_count homework_common, course_group
    myshixun_ids = homework_common.student_works.where(user_id: course_group.course_members.pluck(:user_id)).pluck(:myshixun_id)
    myshixuns = Myshixun.joins(:games).where(id: myshixun_ids).where("games.status = ?", 2).count
  end

  # 未分班的有效作品数
  def homework_ungroup_works_count homework_common, user_ids
    myshixun_ids = homework_common.student_works.where(user_id: user_ids).pluck(:myshixun_id)
    myshixuns = Myshixun.joins(:games).where(id: myshixun_ids).where("games.status = ?", 2).count
  end

  def ungroup_last_review_time homework_common
    review = homework_common.homework_group_reviews.where(:course_group_id => 0).last
    review ? (format_time review.created_at) : "--"
  end

  def work_finished_time challenge_id, user_id
    Challenge.find(challenge_id).games.find_by(user_id: user_id)
  end

  def challenge_setting homework, challenge_id
    homework.homework_challenge_settings.find_by(challenge_id: challenge_id)
  end

  def group_homework_setting homework, group_id
    homework.homework_group_settings.find_by(course_group_id: group_id)
  end

  def homework_st_proportion homework
    (1.0 - homework.homework_detail_manual.te_proportion - homework.homework_detail_manual.ta_proportion).round(2)
  end

  def teacher_comment homework, user_id
    [{ id: 0 ,name: "未评", count: homework.uncomment_count(user_id)}, {id: 1, name: "已评", count: homework.comment_count(user_id)}]
  end

  # 作品状态
  def homework_status homework, member
    [{id: 0, name: "未提交",   count: homework.unfinished_count(member)},
     {id: 1, name: "按时提交", count: homework.finished_count(member)},
     {id: 2, name: "延时提交", count: homework.delay_finished_count(member)}]
  end

  # 作品分数的显示
  def work_score_format score, current_user, score_open
    score.nil? ? "--" : (current_user || score_open) ? number_with_precision(score, precision: 1) : "**"
  end

  def anon_comments user, work_id
    StudentWorksScore.where(student_work_id: work_id, reviewer_role: 3, user_id: user.id)
  end
end