class PollVotesController < ApplicationController
  #在开始回答和提交问卷的时候,已经做了判断用户的身份权限
  before_action :require_login, :check_auth
  before_action :get_poll_question
  before_action :check_answer_in_question


  def create #每一次答案的点击,请求一次
    question_votes = @poll_question.poll_votes
    question_type = @poll_question.question_type
    question_answer_id = params[:poll_answer_id] ? params[:poll_answer_id] : -1 #该答案的id
    question_answer_text = params[:vote_text].present? ? params[:vote_text] : nil #其他选项的内容
    user_votes = question_votes.find_current_vote("user_id", current_user.id) #当前用户的答案,可能有多个
    # 当前用户的当前答案,如果已存在,当再次点击的时候,取消答案,即删除该答案
    current_vote_text = nil

    # if user_votes.find_vote_text.present?
    #   current_vote_text = user_votes.find_vote_text.first
    # end

    vote_answer_params = {
      :user_id => current_user.id,
      :poll_question_id => @poll_question.id,
      :poll_answer_id => question_answer_id,
      :vote_text => question_answer_text
    }
    #begin
    if question_type == 1
      if user_votes.present? #用户曾经回答过的,答案选择不一样,否则新建
        current_user_answer = user_votes.first
        if current_user_answer&.poll_answer_id != question_answer_id #如果说更换了答案,则以前的答案删除,并新建记录
          current_user_answer.destroy
          PollVote.create!(vote_answer_params)
        else

          if question_answer_text.present?
            current_user_answer.update!(vote_text: question_answer_text)
          end
        end
      else
        PollVote.create!(vote_answer_params)
      end
    elsif question_type == 2 #多选题的话,答案应该是1个以上
      question_answer_ids = params[:poll_answer_id] ? params[:poll_answer_id] : [] #该答案的id
      if question_answer_ids.present?
        if question_answer_text.present? #有文字输入,但是不存在其他选项的
          ques_vote_id = question_answer_ids.map(&:to_i).max
          ques_vote = user_votes.find_by(poll_answer_id: ques_vote_id)
          if ques_vote.present?
            ques_vote.update!(vote_text: question_answer_text)
          else
            answer_option = {
              :user_id => current_user.id,
              :poll_question_id => @poll_question.id,
              :poll_answer_id => ques_vote_id,
              :vote_text => question_answer_text
            }
            PollVote.create!(answer_option)
            # 重新取一次poll_votes
            user_votes = @poll_question.poll_votes.find_current_vote("user_id", current_user.id)
          end
          # if current_vote_text.present?   #已有其他输入文字的选项
          #   current_vote_text.update_attribute("vote_text", question_answer_text)
          # else
          #
          # end
        end

        ea_ids = user_votes.pluck(:poll_answer_id)
        common_answer_ids = question_answer_ids & ea_ids #已经存在的试卷选项id
        new_ids = question_answer_ids - common_answer_ids # 新增的id
        old_ids = ea_ids - common_answer_ids #没有选择的,则删掉
        if new_ids.size > 0
          new_ids.each do |e|
            answer_option = {
              :user_id => current_user.id,
              :poll_question_id => @poll_question.id,
              :poll_answer_id => e,
              :vote_text => nil
            }
            ex_a = PollVote.new(answer_option)
            ex_a.save!
          end
        end
        if old_ids.size > 0
          ea_answer = user_votes.find_current_vote("poll_answer_id", old_ids)
          ea_answer.destroy_all
        end
      else
        user_votes.destroy_all
      end
    else #主观题的输入
      # current_vote_text = user_votes.find_vote_text
      if user_votes.present?
        user_votes.first.update!(vote_text: question_answer_text)
        # if question_answer_text.present?
        #   user_votes.first.update_attribute("vote_text", question_answer_text)
        # else
        #   user_votes.destroy_all
        # end
      else
        PollVote.create!(vote_answer_params)
      end
    end
    @current_question_number = @poll_question.question_number
    @current_question_necessary = @poll_question.is_necessary
    #问答记录存在,且有值,才会有返回值。
    @current_question_status = 0
    new_user_votes = question_votes.where(user_id: current_user.id)
    if new_user_votes.present?
      vote_answer_id = new_user_votes.pluck(:poll_answer_id).reject(&:blank?).size
      vote_text_count = new_user_votes.pluck(:vote_text).reject(&:blank?).size
      if vote_text_count > 0 || vote_answer_id > 0
        @current_question_status = 1
      end
    end
  end


  private

  def get_poll_question
    @poll_question = PollQuestion.find_by_id(params[:poll_question_id])
    if @poll_question.blank?
      normal_status(-1, "问卷试题不存在!")
    else
      @poll = @poll_question.poll
      @course = @poll.course
      if @poll.blank?
        normal_status(-1, "问卷不存在!")
      elsif @course.blank?
        normal_status(-1, "课堂不存在!")
      end
    end
  end

  def check_answer_in_question
    poll_user_status = @poll.get_poll_status(current_user)
    poll_user = @poll.poll_users.find_by(user_id: current_user.id) #当前用户

    question_type = @poll_question&.question_type
    if (question_type == 1) && params[:poll_answer_id].blank?
      normal_status(-1, "答案ID错误!")
    elsif question_type == 2
      user_vote_count = params[:poll_answer_id].size
      if @poll_question.max_choices.present?
        question_max_choices = @poll_question.max_choices
      else
        question_max_choices = 0
      end
      if question_max_choices > 0 && (user_vote_count > question_max_choices)
        normal_status(-1, "多选题答案超过最大限制!")
      end
    elsif (poll_user.present? && poll_user.commit_status == 1) || poll_user_status == 3
      normal_status(-1, "已提交/已结束的问卷不允许修改!")
    end
  end

end