class Subjects::CopySubjectService < ApplicationService
  attr_reader :subject, :to_subject, :user, :laboratory

  def initialize(subject, user, laboratory=nil)
    @subject = subject
    @user = user
    @laboratory = laboratory
    subject_params = subject.attributes.dup.except('id', 'copy_subject_id', 'user_id', 'homepage_show',
                                                   'stages_count', 'shixuns_count', 'stage_shixuns_count')
    @to_subject = Subject.new(subject_params)
  end

  def call
    return if subject.blank?
    ActiveRecord::Base.transaction do
      copy_subject!
    end
  end


  private
  # 复制实践课程表
  def copy_subject!
    to_subject.copy_subject_id = subject.id
    to_subject.user_id = user.id
    to_subject.save!

    copy_stages_data!(subject, to_subject)
    copy_subject_members_data(to_subject)
    laboratory.laboratory_subjects.create(subject: to_subject) if laboratory
  end

  # 复制章节需要的章节
  def copy_stages_data!(subject, to_subject)
    subject.stages.each do |stage|
      to_stage = to_subject.stages.new
      to_stage.attributes = stage.attributes.dup.except('id', 'subject_id', 'user_id')
      to_stage.user_id = user.id
      to_stage.save!

      copy_stage_shixuns_data!(stage, to_stage)
    end
  end

  # 创建实践课程关联实训表
  def copy_stage_shixuns_data!(stage, to_stage)
    stage.stage_shixuns.each do |stage_shixun|
      to_shixun = copy_shixun_data!(stage_shixun)
      to_stage_shixun = to_stage.stage_shixuns.new
      to_stage_shixun.attributes = stage_shixun.attributes.dup.except('id', 'subject_id', 'stage_id', 'shixun_id')
      to_stage_shixun.subject_id = to_stage.subject_id
      to_stage_shixun.shixun_id = to_shixun.id
      to_stage_shixun.save!

    end
  end

  # 复制实训数据
  def copy_shixun_data!(stage_shixun)
    shixun = stage_shixun.shixun
    to_shixun = Shixun.new
    to_shixun.attributes = shixun.attributes.dup.except('id', 'user_id', 'identifier', 'homepage_show',
                                                        'use_scope', 'averge_star', 'myshixuns_count', 'challenges_count')
    to_shixun.identifier = Util::UUID.generate_identifier(Shixun, 8)
    to_shixun.user_id = user.id
    if laboratory
      to_shixun.laboratory_id = laboratory.id
    end
    # 复制版本库
    fork_repository_name = "#{user.login}/#{to_shixun.identifier}"
    GitService.fork_repository(repo_path: "#{shixun.repo_name}.git",
                               fork_repository_path: (fork_repository_name + ".git"))
    to_shixun.repo_name = fork_repository_name
    to_shixun.save!

    copy_shixun_info_data!(shixun, to_shixun)
    copy_shixun_mirror_repositories_data!(shixun, to_shixun)
    copy_shixun_tag_repertoires_data!(shixun, to_shixun)
    copy_shixun_service_configs_data!(shixun, to_shixun)
    copy_challenges_data!(shixun, to_shixun)
    copy_shixun_members_data!(to_shixun)
    copy_jupyter_data_sets(shixun, to_shixun) if shixun.is_jupyter?
    # 云上实验室
   if laboratory
     laboratory.laboratory_shixuns.create(shixun: to_shixun)
   end
    to_shixun
  end

  # 复制jupyter的数据集
  def copy_jupyter_data_sets(shixun, to_shixun)
    return unless shixun.is_jupyter?
    folder = EduSetting.get('shixun_folder')
    raise "存储目录未定义" unless folder.present?
    path = "#{folder}/#{to_shixun.identifier}"
    FileUtils.mkdir_p(path, :mode => 0777) unless File.directory?(path)
    # 复制数据集
    save_path = File.join(folder, shixun.identifier)
    shixun.data_sets.each do |set|
      new_date_set = Attachment.new
      new_date_set.attributes = set.attributes.dup.except("id", "container_id", "disk_directory")
      new_date_set.container_id = to_shixun.id
      new_date_set.disk_directory = to_shixun.identifier
      new_date_set.save!
      FileUtils.cp("#{save_path}/#{set.relative_path_filename}", path)
    end
  end

  # 创建实训长字段内容
  def copy_shixun_info_data!(shixun, to_shixun)
    to_shixun_info = ShixunInfo.new
    to_shixun_info.attributes = shixun.shixun_info.attributes.except('id', 'shixun_id')
    to_shixun_info.shixun_id = to_shixun.id
    to_shixun_info.save!
  end

  # 创建实训镜像标签
  def copy_shixun_mirror_repositories_data!(shixun, to_shixun)
    shixun.shixun_mirror_repositories.each do |shixun_mirror_repository|
      to_shixun_mirror_repository = to_shixun.shixun_mirror_repositories.new
      to_shixun_mirror_repository.attributes = shixun_mirror_repository.attributes.dup.except('id', 'shixun_id')
      to_shixun_mirror_repository.shixun_id = to_shixun.id
      to_shixun_mirror_repository.save!
    end
  end

  # 创建实训tag标签
  def copy_shixun_tag_repertoires_data!(shixun, to_shixun)
    shixun.shixun_tag_repertoires.each do |shixun_tag_repertoire|
      to_shixun_tag_repertoire = to_shixun.shixun_tag_repertoires.new
      to_shixun_tag_repertoire.attributes = shixun_tag_repertoire.attributes.dup.except('id', 'shixun_id')
      to_shixun_tag_repertoire.shixun_id = to_shixun.id
      to_shixun_tag_repertoire.save!
    end
  end

  # 复制实训服务配置
  def copy_shixun_service_configs_data!(shixun, to_shixun)
    shixun.shixun_service_configs.each do |shixun_service_config|
      to_shixun_service_config = to_shixun.shixun_service_configs.new
      to_shixun_service_config.attributes = shixun_service_config.attributes.dup.except('id', 'shixun_id')
      to_shixun_service_config.shixun_id = to_shixun.id
      to_shixun_service_config.save!
    end
  end

  # 复制关卡信息
  def copy_challenges_data!(shixun, to_shixun)
    shixun.challenges.each do |challenge|
      to_challenge = to_shixun.challenges.new
      to_challenge.attributes = challenge.attributes.dup.except('id', 'shixun_id', 'praises_count', 'user_id', 'visits')
      to_challenge.user_id = user.id
      to_challenge.shixun_id = to_shixun.id
      to_challenge.save!

      copy_challenge_answers_data!(challenge, to_challenge)
      copy_challenge_tags_data!(challenge, to_challenge)
      copy_test_sets_data!(challenge, to_challenge)
      copy_challenge_chooses_data!(challenge, to_challenge)
    end
  end

  # 复制答案数据
  def copy_challenge_answers_data!(challenge, to_challenge)
    challenge.challenge_answers.each do |challenge_answer|
      to_challenge_answer = to_challenge.challenge_answers.new
      to_challenge_answer.attributes = challenge_answer.attributes.dup.except('id', 'challenge_id')
      to_challenge_answer.challenge_id = to_challenge.id
      to_challenge_answer.save!
    end
  end

  # 复制关卡标签数据
  def copy_challenge_tags_data!(challenge, to_challenge)
    challenge.challenge_tags.each do |challenge_tag|
      to_challenge_tag = to_challenge.challenge_tags.new
      to_challenge_tag.attributes = challenge_tag.attributes.dup.except('id', 'challenge_id')
      to_challenge_tag.challenge_id = to_challenge.id
      to_challenge_tag.save!
    end
  end

  # 复制测试集
  def copy_test_sets_data!(challenge, to_challenge)
    challenge.test_sets.each do |test_set|
      to_test_set = to_challenge.test_sets.new
      to_test_set.attributes = test_set.attributes.dup.except('id', 'challenge_id')
      to_test_set.challenge_id = to_challenge.id
      to_test_set.save!
    end
  end

  # 复制选择题关卡
  def copy_challenge_chooses_data!(challenge, to_challenge)
    challenge.challenge_chooses.each do |challenge_choose|
      to_challenge_choose = to_challenge.challenge_chooses.new
      to_challenge_choose.attributes = challenge_choose.attributes.dup.except('id', 'challenge_id')
      to_challenge_choose.challenge_id = to_challenge.id
      to_challenge_choose.save!

      copy_challenge_questions_data!(challenge_choose, to_challenge_choose)
      copy_challenge_choose_tags_data!(challenge_choose, to_challenge_choose)

    end
  end

  # 复制选择题问题
  def copy_challenge_questions_data!(challenge_choose, to_challenge_choose)
    challenge_choose.challenge_questions.each do |challenge_question|
      to_challenge_question = to_challenge_choose.challenge_questions.new
      to_challenge_question.attributes = challenge_question.attributes.dup.except('id', 'challenge_choose_id')
      to_challenge_question.challenge_choose_id = to_challenge_choose.id
      to_challenge_question.save!
    end
  end

  # 复制选择题标签
  def copy_challenge_choose_tags_data!(challenge_choose, to_challenge_choose)
    challenge_choose.challenge_tags.each do |challenge_tag|
      to_challenge_tag = to_challenge_choose.challenge_tags.new
      to_challenge_tag.attributes = challenge_tag.attributes.dup.except('id', 'challenge_choose_id')
      to_challenge_tag.challenge_choose_id = to_challenge_choose.id
      to_challenge_tag.save!
    end
  end

  # 创建实训成员
  def copy_shixun_members_data!(to_shixun)
    to_shixun.shixun_members.create!(user_id: user.id, role: 1)
  end

  # 创建课程成员
  def copy_subject_members_data(to_subject)
    to_subject.subject_members.create!(user_id: user.id, role: 1)
  end

end
