module ShixunsHelper

	def level_to_s(level)
		%W(初级 中级 中高级 高级)[level-1]
	end

  #难度
	def diff_to_s(trainee)
		%W(初级学员 中级学员 中高级学员 高级学员)[trainee-1]
	end

  #1. 未发布
  def status_to_s(status)
		%W(未发布 已发布 已关闭)[status-1]
	end

	def shixun_authentication_status shixun
		case shixun.try(:status)
		when 0,nil
			"编辑中"
		when 1
			"待审核"
		when 2
			"已发布"
		when 3
			"已关闭"
		when -1
			"已删除"
		end
	end

	def shixun_public_status shixun
		case shixun.try(:public)
		when 0,nil
			"未公开"
		when 1
			"待审核"
		when 2
			"已公开"
		end
	end

	# 已完成实训所获得的经验值
	def myshixun_exp myshixun
		score = 0
		myshixun.games.each do |game|
			score += game.final_score.to_i < 0 ? 0 : game.challenge.score.to_i
		end
		score
	end

	# 已完成实训所消耗的时间
	def myshixun_spend_time myshixun
		time = myshixun.games.map(&:cost_time).sum
		time
	end

	# 已完成实训的准确率
	def myshixun_accuracy myshixun
		accuracy = 0
		count = 0
		myshixun.games.each do |game|
			count += 1
			accuracy += game.accuracy ? game.accuracy : 0
		end
		accuracy.to_f / count
	end

	# 实训的最终通关时间
	def myshixun_done_time myshixun
		time = ""
		if myshixun.status == 1 || myshixun.games.where(:status => 2).count == myshixun.shixun.challenges.count
			time = myshixun.games.where(:status => 2).map(&:end_time).max
		end
		time
	end

	def had_passed_count

	end

	#显示项目配置菜单
	def show_project_memu user
		if user.allowed_to?(:edit_project, @shixun)
			result = "edit_project"
		elsif user.allowed_to?(:manage_members, @shixun)
			result = "training_task"
		elsif user.allowed_to?(:manage_versions, @shixun)
			result = "manage_versions"
		elsif user.allowed_to?(:manage_repository, @shixun)
			result = "manage_repository"
		end
		result
	end

	def show_shixun_mirror shixun
		mirror_name = ""
		shixun.mirror_repositories.try(:each) do |mirror|
			if mirror_name.blank?
				mirror_name = mirror.type_name
			else
				mirror_name = "#{mirror_name};#{mirror.type_name}"
			end
		end
		return mirror_name
	end

	def generate_script shixun, script
		if script.present?
			source_class_name = []
			challenge_program_name = []
			shixun.challenges.map(&:exec_path).each do |exec_path|
				challenge_program_name << "\"#{exec_path}\""
				if shixun.mirror_name.try(:first) == "Java"
					if exec_path.nil? || exec_path.split("src/")[1].nil?
						source = ""
					else
						source = "\"#{exec_path.split("src/")[1].split(".java")[0]}\""
					end
					source_class_name << source.gsub("/", ".") if source.present?
				end
			end
			script = if script.include?("sourceClassName") && script.include?("challengeProgramName")
								 script.gsub("CHALLENGEPROGRAMNAMES","#{challenge_program_name.reject(&:blank?).join(" ")}").gsub("SOURCECLASSNAMES", "#{source_class_name.reject(&:blank?).join(" ")}")
							 else
								 script.gsub("CHALLENGEPROGRAMNAMES","#{challenge_program_name.reject(&:blank?).join(" ")}").gsub("SOURCECLASSNAMES", "#{challenge_program_name.reject(&:blank?).join(" ")}")
							 end
		end
		return script
	end

	def had_passed_challenge challenge
		Game.where(:challenge_id => challenge.id, :status => 2).limit(3).reorder("final_score desc")
	end

  def finished_num challenge
		Game.finished_num(challenge.id).count
	end

	def doing_num challenge
		Game.doing_num(challenge.id).count
	end

  def challenge_status(challenge_id, user_id)
		Game.where(challenge_id: challenge_id, user_id: user_id).limit(1).try(:status) || 0
	end

  # 若实训关卡位置，关卡数等信息发生变化则需要修改脚本内容
  def modify_shixun_script shixun, script
		if script.present?
			source_class_name = []
			challenge_program_name = []
			shixun.challenges.map(&:exec_path).each do |exec_path|
				challenge_program_name << "\"#{exec_path}\""
				if shixun.main_mirror_name == "Java" || shixun.main_mirror_name == "Openjdk10/VNC" || shixun.main_mirror_name == "JavaWeb"
					if exec_path.nil? || exec_path.split("src/")[1].nil?
						source = "\"\""
					else
						source = "\"#{exec_path.split("src/")[1].split(".java")[0]}\""
					end
					logger.info("----source: #{source}")
					source_class_name << source.gsub("/", ".") if source.present?
				elsif shixun.main_mirror_name.try(:first) == "C#"
					if exec_path.nil? || exec_path.split(".")[1].nil?
						source = "\"\""
					else
						source = "\"#{exec_path.split(".")[0]}.exe\""
					end
					source_class_name << source if source.present?
				end
			end
			logger.info("####source_class_name: #{source_class_name}")
			script = if script.include?("sourceClassName") && script.include?("challengeProgramName")
								 script.gsub(/challengeProgramNames=\(.*\)/,"challengeProgramNames=\(#{challenge_program_name.reject(&:blank?).join(" ")}\)").gsub(/sourceClassNames=\(.*\)/, "sourceClassNames=\(#{source_class_name.reject(&:blank?).join(" ")}\)")
							 else
								 script.gsub(/challengeProgramNames=\(.*\)/,"challengeProgramNames=\(#{challenge_program_name.reject(&:blank?).join(" ")}\)").gsub(/sourceClassNames=\(.*\)/, "sourceClassNames=\(#{challenge_program_name.reject(&:blank?).join(" ")}\)")
							 end
		end
		return script
	end

end