class Users::SubjectService
  include CustomSortable

  sort_columns :created_at, :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.distinct, params[:sort_by], 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 })
    when 'collect' then
      Subject.where(id: user.subject_collections.pluck(:container_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)
      collect_subject_ids = user.subject_collections.pluck(:container_id)
      Subject.where(id: study_subject_ids + manage_subject_ids + collect_subject_ids)
    end
  end

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

  def status_filter(relations)
    return relations unless self_or_admin?

    case params[:category]
    when 'study', 'collect' 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 = relations.where(id: subject_ids) if subject_ids.present?
    relations
  end

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

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