# encoding=utf-8
# 外部数据分析专用
class SourcesService
  include ApplicationHelper
  include GamesHelper
  LIMIT = 10

  # shixuns start ############################################
  def shixun_index params
    #offset = params[:page].to_i * 15
    #Shixun.select([:id, :identifier]).offset(offset).limit(15)
    Shixun.select([:id, :name,:identifier, :created_at]).where("created_at < ?", params[:time])
  end

  def shixun_detail params
    shixun = Shixun.select([:id, :identifier, :name, :description, :user_id, :myshixuns_count, :evaluate_script,
                            :propaedeutics, :challenges_count, :gpid, :created_at, :updated_at]).find_by_identifier(params[:identifier])
    return {:status => 404} unless shixun.present?
    g = Gitlab.client
    git_url = "#{Redmine::Configuration['gitlab_address']}/#{g.project(shixun.try(:gpid)).try(:path_with_namespace)}.git"
    language = shixun.mirror_repositories.first.try(:type_name)
    homework_common_ids = HomeworkCommonsShixuns.where(:shixun_id => shixun.id).pluck(:homework_common_id)
    course_ids = HomeworkCommon.where(:id => homework_common_ids).pluck(:course_id)
    course_info =[]
    course_ids.each do |id|
      course= Course.select([:name]).find id
      course_info << {course_id: id, course_name: course.name}
    end

    list = shixun.try(:attributes).merge({:git_url => git_url, :language => language, :course_info => course_info})
    return list
  end

  def shixun_challenges params
    list = []
    shixun = Shixun.find_by_identifier(params[:identifier])
    return {:status => 404} unless shixun.present?
    shixun.challenges.each do |challenge|
      sets = []
      challenge.test_sets.each do |set|
        sets << {input: set.input, output: set.output}
      end
      list << {:id => challenge.id, :name => challenge.subject, :shixun_id => challenge.shixun_id,
               :task_pass => challenge.task_pass, :position => challenge.position, :answer => challenge.answer,
               :score => challenge.score, :path => challenge.path, :difficulty => challenge.difficulty,
               :user_id => shixun.user_id, :sets => sets}
    end
    return list
  end


  def get_tpi_info params
    shixun = Shixun.find_by_identifier params[:identifier]
    page = params[:page].to_i
    offset = page * 10
    challenge_paths = shixun.challenges.pluck(:path)
    myshixuns = shixun.myshixuns.select([:id, :user_id, :gpid, :shixun_id]).offset(offset).limit(10)
    g = Gitlab.client
    tpi_info = []
    myshixuns.each do |myshixun|
      code_info = []
      challenge_paths.each do |paths|
        x_paths = paths.split(";")
        x_paths.each do |path|
          Rails.logger.info("############################gpid: #{myshixun.gpid}")
          Rails.logger.info("############################path: #{path}")
          code = g.files(myshixun.gpid, path, "master").try(:content)
          code = tran_base64_decode64(code)
          # 取begin和end之间的代码
          if code.present?
            user_code = code.match(/\/\*+ Begin \*+\/(\S*)\/\*+ End \*+\//)
            coder = user_code[1] if code.length == 2
          end
          code_info << {path: path, code: code}
        end
      end
      tpi_info << {user_id: myshixun.user_id, code: code_info}
    end
    {tpi_info: tpi_info}
  end

  # paths start ###########################
  def get_paths_info params
    path = Subject.find params[:id]
    stages = path.stages.order("position asc")
    path_info = []
    stages.each do |stage|
      chapter_name = stage.name
      chapter_shixuns = []
      stage.stage_shixuns.each do |stage_shixun|
        challenges =  stage_shixun.shixun.challenges
        challenge_info = []
        challenges.each do |challenge|
          challenge_info << {challenge_id: challenge.id, challenge_name: challenge.subject}
        end
        chapter_shixuns << {name: stage_shixun.shixun.name, identifier: stage_shixun.shixun.identifier, challenge_info: challenge_info}
      end
      path_info << {chapter_name: chapter_name, chapter_shixuns: chapter_shixuns}
    end
    { path_info: path_info}
  end

  # games start ############################################
  def myshixuns_index params
    time = Time.parse(params[:time].to_s)
    offset = ((params[:page] || 1).to_i - 1) * 50
    Myshixun.select([:id, :user_id, :shixun_id]).where("created_at < ?", time).offset(offset).limit(50)
  end

  def search_myshixun params
    myshixun = Myshixun.where(:shixun_id => params[:shixun_id], :user_id => params[:user_id]).first
    return {:status => 404} unless myshixun.present?
    g = Gitlab.client
    git_url = "#{Redmine::Configuration['gitlab_address']}/#{g.project(myshixun.try(:gpid)).try(:path_with_namespace)}.git"
    {:status => myshixun.status, :git_url => git_url, :shixun_id => myshixun.shixun_id, :user_id => myshixun.user_id}
  end

  def games params
    time = Time.parse(params[:time].to_s)
    offset = ((params[:page] || 1).to_i - 1) * 50
    Game.select([:id, :identifier, :user_id, :myshixun_id]).where("created_at < ?", time).offset(offset).limit(50)
  end

  # 关卡详情
  def game_detail params
    game = Game.min.find_by_identifier(params[:identifier])
    myshixun = game.myshixun
    return {:status => 404} unless game.present?
    challenge = Challenge.min.find(game.challenge_id)
    outputs_commit_count = Output.find_by_sql("select count(*) as commit_count from outputs where game_id=#{game.id} and test_set_position=1").first.try(:commit_count)
    right = game.status == 2 ? true : false
    type = challenge.st
    g = Gitlab.client
    last_code = game.game_codes.pluck(:new_code)
    commit_status = []
    game.outputs.group(:query_index).each_with_index do |output, index|
      result = output.code == 0 ? true : false
      commit_status << {commit_time: output.created_at, commit_result: result}
    end
    git_url = "#{Redmine::Configuration['gitlab_address']}/#{g.project(myshixun.try(:gpid)).try(:path_with_namespace)}.git"
    {:id => game.id, :identifier => game.identifier, :commit_count => outputs_commit_count, :right => right, :type => type,
     :myshixun_id => game.myshixun_id, :final_Score => game.final_score, :git_url => git_url,
     shixun_id: myshixun.shixun_id, challenge_id: challenge.id, :last_commit_code => last_code, commit_status: commit_status}
  end

  # 搜索学员开启的关卡
  def search_game params
    shixun_id = Challenge.where(:id => params[:challenge_id]).pluck(:shixun_id).first
    myshixun_id = Myshixun.where(:shixun_id => shixun_id, :user_id => params[:user_id]).pluck(:id).first
    game = Game.where(:myshixun_id => myshixun_id, :challenge_id => params[:challenge_id]).first
    if game.present?
      return {:id => game.id, :identifier => game.identifier}
    else
      return {:status => 404}
    end
  end

  # users start ############################################
  def users params
    time = Time.parse("#{params[:time]}")
    users = User.select([:id, :login, :created_on, :firstname, :lastname, :nickname]).includes(user_extensions: :school).where("created_on < ?", time)
    users = users.map{|user| {id: user.id, login: user.login, username: user.show_real_name, school: user.school_name, work: user.identity, sex: user.sex, created_on: user.created_on}}
  end

  def login params
    user, last_login_on = User.try_to_login(params[:username], params[:password])
    return {status: -1, message: '账号或密码错误!'} if user.blank?
    {status: 1, user_id: user.id, message: "登陆成功!"}
  end

  def get_user_info params
    user = User.find params[:id]
    {username: user.show_real_name, user_id: user.id, email: user.mail, phone: user.phone, sex: user.sex, school_name: user.school_name}
  end

  # courses start ############################################
  def course_detail params
    course = Course.select([:id, :name, :tea_id, :time, :term, :class_period, :school_id, :description,
                            :end_time, :end_term, :visits, :credit, :end_date, :members_count, :homework_commons_count]).find(params[:id])
    if course.present?
      return course.try(:attributes)
    else
      return {:status => 404}
    end
  end

  def courses params
    time = Time.parse("#{params[:time]}")
    Course.select([:id, :name, :created_at]).where("created_at < ?", time)
  end

  def exercises params
    time = Time.parse("#{params[:time]}")
    Exercise.select([:id, :exercise_name, :created_at]).where("created_at < ?", time)
  end

  def exercise_detail params
    exercise = Exercise.find params[:id]
    if exercise.present?
      return exercise.try(:attributes)
    else
      return {:status => 404}
    end
  end

  def get_exercise_questions params
    exercise = Exercise.find params[:id]
    exercise_list = []
    exercise.exercise_questions.each do |question|
      title =
          if question.question_type == 1 || question.question_type == 2
            question.exercise_choices.pluck(:choice_text)
          else
            []
          end
      right_answer =
          if question.question_type == 1 || question.question_type == 2
            choice_id = question.exercise_standard_answers.first.try(:exercise_choice_id)
            choice_ids = choice_id.to_s.split("") if choice_id
            question.exercise_choices.where(:choice_position => choice_ids).pluck(:choice_text)
          elsif question.question_type == 5
            []
          else
            question.exercise_standard_answers.pluck(:answer_text)
          end
      exercise_list << {choice_id: choice_id, id: question.id, name: question.question_title, type: question.question_type_name, title: title, right_answer: right_answer, score: question.question_score}
    end
    {exercise_id: exercise.id, exercise_name: exercise.exercise_name, course_id: exercise.course_id, course_name: exercise.course.name, exercise_list: exercise_list}
  end

  # 竞赛相关 ###################################################
  def get_competition_members params
    comp = Competition.find_by_identifier params[:identifier]
    team_ids = comp.competition_teams.pluck(:id)
    user_team = TeamMember.where(:competition_team_id => team_ids, :user_id => params[:user_id]).first
    user_ids =TeamMember.where(:competition_team_id => user_team.try(:competition_team_id)).pluck(:user_id)
    {team_user_ids: user_ids}
  end

  def get_team_ids params
    comp = Competition.find_by_identifier params[:identifier]
    team_ids = comp.competition_teams.pluck(:id)
    {team_ids: team_ids}
  end

  def get_team_user_ids params
    comp = CompetitionTeam.find params[:team_id]
    user_ids = comp.team_members.pluck(:user_id)
    {user_ids: user_ids}
  end

  def get_user_competition_id params
    competition_ids = Competition.where(:identifier => params[:identifier]).pluck(:id)
    team_id = TeamMember.where(:user_id => params[:educoder_userid], :competition_id => competition_ids).pluck(:competition_team_id)
    {team_id: team_id}
  end

  def get_team_info params
    comp = Competition.find_by_identifier(params[:identifier])
    if comp
      team_info = []
      teams = comp.competition_teams
      teams.each do |t|
        team_member = []
        t.team_members.each do |m|
          user = User.find m.user_id
          team_member << {user_id: m.user_id, user_name: user.show_real_name}
        end
        team_info << {team_id: t.id, team_name: t.name, team_member: team_member}
      end
      {team_info: team_info}
    end

  end

  def shixun_homework_commons params
    HomeworkCommon.where(:homework_type => 4, :course_id => params[:id])
  end
end