class Users::SubjectService
  include CustomSortable

  sort_columns :updated_at, default_by: :updated_at, default_direction: :desc

  attr_reader :user, :params

  def initialize(user, params)
    @user   = user
    @params = params
  end

  def call
    subjects = category_scope_subjects
    subjects = user_policy_filter(subjects)

    custom_sort(subjects, :updated_at, params[:sort_direction])
  end

  private

  def category_scope_subjects
    case params[:category]
    when 'study' then
      Subject.joins(stage_shixuns: { shixun: :myshixuns }).where(myshixuns: { user_id: user.id })
    when 'manage' then
      Subject.joins(:subject_members).where(subject_members: { user_id: user.id })
    else
      study_subject_ids = StageShixun.where(shixun_id: user.myshixuns.pluck(:shixun_id)).pluck(:subject_id)
      manage_subject_ids = user.subject_members.pluck(:subject_id)
      Subject.where(id: study_subject_ids + manage_subject_ids)
    end
  end

  def user_policy_filter(relations)
    # 只有自己或者管理员才有过滤筛选及查看全部状态下实训功能
    if self_or_admin?
      status_filter(relations)
    else
      relations.where(status: 2, hidden: false)
    end
  end

  def status_filter(relations)
    return relations unless self_or_admin?

    case params[:category]
    when 'study' then
      study_subject_status_filter(relations)
    when 'manage' then
      manage_subject_status_filter(relations)
    else
      relations
    end
  end

  def study_subject_status_filter(relations)
    subjects = Subject.joins(shixuns: :myshixuns)
                      .where(myshixuns: { user_id: user.id })
                      .select('subjects.id, (COUNT(IF(myshixuns.status=1, 1, 0)) = subjects.stages_count) finished')
                      .group('subjects.id')
    subject_ids =
      case params[:status]
      when 'unfinished' then subjects.having('finished = 0').map(&:id)
      when 'finished' then subjects.having('finished = 1').map(&:id)
      end

    relations.where(id: subject_ids) if subject_ids.present?
    relations
  end

  def manage_subject_status_filter(relations)
    status = case params[:status]
             when 'editing'   then 0
             when 'applying'  then 1
             when 'published' then 2
             end
    relations.where(status: status) if status
    relations
  end

  def self_or_admin?
    User.current.id == user.id || User.current.admin?
  end
end