#encoding: UTF-8
class ExerciseBanksController < ApplicationController
  before_action :require_login
  before_action :find_bank, :bank_visit_auth, except: [:choose_shixun]
  before_action :bank_admin, only: [:update]
  before_action :commit_shixun_present, only: [:commit_shixun]

  def show
    @exercise_questions = @bank.exercise_bank_questions&.includes(:exercise_bank_choices, :exercise_bank_shixun_challenges,
                                                                  :exercise_bank_standard_answers).order("question_number ASC")

    if @bank.container_type == "Exercise"
      get_exercise_question_count
    else
      get_poll_question_count
    end
  end

  def update
    tip_exception("标题不能为空!") if params[:exercise_name].blank?
    @bank.update_attributes!(name: params[:exercise_name], description: params[:exercise_description])
    normal_status(0,"更新成功")
  end

  def choose_shixun
    search = params[:search]
    type = params[:type]
    # 超级管理员用户显示所有未隐藏的实训、非管理员显示所有已发布的实训(对本单位公开且未隐藏未关闭)
    if current_user.admin?
      @shixuns = Shixun.unhidden
    else
      none_shixun_ids = ShixunSchool.where("school_id != #{current_user.school_id}").pluck(:shixun_id)

      @shixuns = Shixun.where.not(id: none_shixun_ids).unhidden
    end

    # 实训的所有标签
    @tags = TagRepertoire.select([:id, :name]).joins(:shixuns).where(shixuns: {id: @shixuns}).distinct

    if params[:search] && params[:search].strip != ""
      @shixuns = @shixuns.joins(:user).where("shixuns.name like ? or concat(users.lastname, users.firstname) like ?",
                                             "%#{search}%", "%#{search}%").distinct
    end

    unless type.blank? || type == "all"
      @shixuns = @shixuns.joins(:shixun_tag_repertoires).where(shixun_tag_repertoires: {tag_repertoire_id: type}).distinct
    end

    @shixuns = @shixuns.select([:id, :name, :status, :myshixuns_count, :identifier, :user_id, :trainee])
    @total_count = @shixuns.size

    ## 分页参数
    page  = params[:page] || 1
    @shixuns = @shixuns.reorder("shixuns.created_at desc").includes(:challenges, user: [user_extension: :school]).page(page).per(10)
  end

  #确认实训的选择
  def commit_shixun
    @shixun_challenges = @shixun.challenges
    @shixun_challenges_count = @shixun_challenges.size
  end

  private

  def find_bank
    @bank = ExerciseBank.find_by!(id: params[:id])
  end

  def bank_admin
    tip_exception(403, "无权限") unless @bank.user_id == current_user.id || current_user.admin?
  end

  #判断实训是否已选择
  def commit_shixun_present
    question_shixun_ids = @bank.exercise_bank_questions.pluck(:shixun_id).reject(&:blank?)
    shixun_id = params[:shixun_id]
    @shixun = Shixun.find_by(id: shixun_id)
    if shixun_id.present? && question_shixun_ids.include?(shixun_id)
      normal_status(-1,"该实训已选择!")
    elsif @shixun.blank?
      normal_status(-1,"该实训不存在!")
    end
  end

  def get_exercise_question_count
    exercise_questions = @bank.exercise_bank_questions
    @exercise_ques_count = exercise_questions.size # 全部的题目数
    @exercise_ques_scores = exercise_questions.pluck(:question_score).sum

    #单选题的数量及分数
    exercise_single_ques = exercise_questions.find_by_custom("question_type", Exercise::SINGLE)
    @exercise_single_ques_count = exercise_single_ques.size
    @exercise_single_ques_scores = exercise_single_ques.pluck(:question_score).sum

    #多选题的数量及分数
    exercise_double_ques = exercise_questions.find_by_custom("question_type", Exercise::MULTIPLE)
    @exercise_double_ques_count = exercise_double_ques.size
    @exercise_double_ques_scores = exercise_double_ques.pluck(:question_score).sum

    # 判断题数量及分数
    exercise_ques_judge = exercise_questions.find_by_custom("question_type", Exercise::JUDGMENT)
    @exercise_ques_judge_count = exercise_ques_judge.size
    @exercise_ques_judge_scores = exercise_ques_judge.pluck(:question_score).sum

    #填空题数量及分数
    exercise_ques_null = exercise_questions.find_by_custom("question_type", Exercise::COMPLETION)
    @exercise_ques_null_count = exercise_ques_null.size
    @exercise_ques_null_scores = exercise_ques_null.pluck(:question_score).sum

    #简答题数量及分数
    exercise_ques_main = exercise_questions.find_by_custom("question_type", Exercise::SUBJECTIVE)
    @exercise_ques_main_count = exercise_ques_main.size
    @exercise_ques_main_scores = exercise_ques_main.pluck(:question_score).sum

    #实训题数量及分数
    exercise_ques_shixun = exercise_questions.find_by_custom("question_type", Exercise::PRACTICAL)
    @exercise_ques_shixun_count = exercise_ques_shixun.size
    @exercise_ques_shixun_scores = exercise_ques_shixun.pluck(:question_score).sum
  end

  def get_poll_question_count
    exercise_questions = @bank.exercise_bank_questions
    @poll_questions_count = exercise_questions&.size # 全部的题目数
    @poll_question_singles = exercise_questions.find_by_custom("question_type", 1).size # 单选题
    @poll_question_doubles = exercise_questions.find_by_custom("question_type", 2).size # 多选题
    @poll_question_mains = exercise_questions.find_by_custom("question_type", 3).size #主观题
  end

end