class Ecs::CreateCourseAchievementMethodsService < ApplicationService
  include AcceptsNestedAttributesHelper

  attr_reader :course_target, :params

  def initialize(course_target, params)
    @course_target = course_target
    @params        = params
  end

  def call
    Ecs::CreateCourseAchievementMethodsForm.new(
      ec_course: course_target.ec_course,
      course_achievement_methods: params[:course_achievement_methods]
    ).validate!

    accepts_attributes = build_accepts_nested_attributes(
      course_target, course_target.ec_course_achievement_methods,
      params[:course_achievement_methods],
      &method(:deal_course_achievement_method_attribute!)
    )

    course_target.assign_attributes(ec_course_achievement_methods_attributes: accepts_attributes)
    course_target.save!
    course_target
  end

  private

  def deal_course_achievement_method_attribute!(item)
    # 创建新的评价方法时，全部为新建
    if item[:id].blank? || course_target.ec_course_achievement_methods.find_by(id: item[:id]).blank?
      item[:ec_achievement_evaluation_relates_attributes] =
        item[:course_evaluation_relates].map do |relate|
          { ec_course_evaluation_subitem_id: relate[:subitem_id].to_i, position: relate[:position] }
        end
      return
    end

    achievement_method = course_target.ec_course_achievement_methods.find_by(id: item[:id])
    relates = achievement_method.ec_achievement_evaluation_relates

    # 获取传入的 subitem id数组和已存在的 subitem id数组
    old_data = relates.map { |e| [e.ec_course_evaluation_subitem_id, e.position] }
    new_data = item[:course_evaluation_relates].map { |e| [e[:subitem_id, e[:position]]] }

    # 分别得到需要移除的 subitem ID数组和需要创建的 subitem ID数组
    destroy_data = old_data - new_data
    create_data = new_data - old_data

    # 生成需要创建关系的 subitem id 数据
    create_attributes = create_data.map { |arr| { ec_course_target_id: course_target.id, ec_course_evaluation_subitem_id: arr[0], position: arr[1] } }

    # 处理需要更新或者删除的记录
    exists_attributes = relates.map do |relate|
      if destroy_data.include?([relate.ec_course_evaluation_subitem_id, relate.position])
        { id: relate.id, _destroy: true }
      else
        relate.as_json(only: %i[id ec_course_target_id ec_course_evaluation_subitem_id ec_course_achievement_method_id position])
      end
    end

    # 相加得到accepts_nested_attributes
    item[:ec_achievement_evaluation_relates_attributes] = create_attributes + exists_attributes
  end
end