#coding=utf-8

class ResourcesService
  #发送资源到课程
  def send_resource_to_course user,params
    send_id  = params[:send_id]
    @ori = Attachment.find_by_id(send_id)
    course_ids = params[:course_ids]
    @flag = false
    unless course_ids.nil?
      course_ids.each do |id|
        next if @ori.blank?
        @exist = false
        Course.find(id).attachments.each do |att|  #如果课程中包含该资源
          if att.id == @ori.id || (!att.copy_from.nil? && !@ori.copy_from.nil? && att.copy_from == @ori.copy_from) || att.copy_from == @ori.id || att.id == @ori.copy_from
            att.created_on = Time.now
            att.save
            @exist = true
            @flag = true
            break
          end
        end
        next if @exist
        attach_copied_obj = @ori.copy
        attach_copied_obj.tag_list.add(@ori.tag_list) # tag关联
        attach_copied_obj.container = Course.find(id)
        attach_copied_obj.created_on = Time.now
        attach_copied_obj.author_id = user.id
        attach_copied_obj.is_public = 0
        attach_copied_obj.copy_from = @ori.copy_from.nil? ? @ori.id : @ori.copy_from #发送要添加copy_from
        if attach_copied_obj.attachtype == nil
          attach_copied_obj.attachtype = 4
        end
        if attach_copied_obj.save
          # 更新引用次数
          quotes = @ori.quotes.to_i + 1
          @ori.update_attribute(:quotes, quotes) unless @ori.nil?
          @ori.forwards << Forward.new(:to_type => attach_copied_obj.class.name, :to_id => attach_copied_obj.id,:created_at => Time.now)
          @flag = true
        else
          @flag = false
          @save_message = attach_copied_obj.errors.full_messages
          break
        end

      end
    end

    [@ori, @flag, @save_message]
  end

  def send_homework_to_course user,params
    homework = HomeworkCommon.find params[:send_id].to_i
    @ori = homework
    course_ids = params[:course_ids]
    @flag = false
    unless course_ids.nil?
      course_ids.each do |course_id|
        course = Course.find course_id.to_i
        new_homework = HomeworkCommon.new
        new_homework.name = homework.name
        new_homework.user_id = user.id
        new_homework.description = homework.description
        new_homework.homework_type = homework.homework_type
        new_homework.late_penalty = homework.late_penalty
        new_homework.course_id = course.id
        new_homework.teacher_priority = homework.teacher_priority
        new_homework.anonymous_comment = homework.anonymous_comment
        new_homework.quotes = 0
        new_homework.is_open = homework.is_open
        homework.attachments.each do |attachment|
          att = attachment.copy
          att.container_id = nil
          att.container_type = nil
          att.copy_from = attachment.id
          att.save
          new_homework.attachments << att
        end
        homework_detail_manual = homework.homework_detail_manual
        homework_detail_programing = homework.homework_detail_programing
        homework_detail_group = homework.homework_detail_group
        if homework_detail_manual
          new_homework.homework_detail_manual = HomeworkDetailManual.new
          new_homework_detail_manual =  new_homework.homework_detail_manual
          new_homework_detail_manual.ta_proportion = homework_detail_manual.ta_proportion
          new_homework_detail_manual.comment_status = 0
          new_homework_detail_manual.evaluation_num = homework_detail_manual.evaluation_num
          new_homework_detail_manual.absence_penalty = homework_detail_manual.absence_penalty
        end
        if homework_detail_programing
          new_homework.homework_detail_programing = HomeworkDetailPrograming.new
          new_homework.homework_detail_programing.ta_proportion = homework_detail_programing.ta_proportion
          new_homework.homework_detail_programing.language = homework_detail_programing.language
          homework.homework_tests.each_with_index do |homework_test|
            new_homework.homework_tests << HomeworkTest.new(
                input: homework_test.input,
                output: homework_test.output
            )
          end
        end

        if homework_detail_group
          new_homework.homework_detail_group = HomeworkDetailGroup.new
          new_homework.homework_detail_group.min_num = homework_detail_group.min_num
          new_homework.homework_detail_group.max_num = homework_detail_group.max_num
          new_homework.homework_detail_group.base_on_project = homework_detail_group.base_on_project
        end
        if new_homework.save
          new_homework_detail_manual.save if new_homework_detail_manual
          new_homework.homework_detail_programing.save if new_homework.homework_detail_programing
          new_homework.homework_detail_group.save if new_homework.homework_detail_group
          @flag = true
        else
          @flag = false
          @save_message = new_homework.errors.full_messages
          break
        end
        homework.update_column(:quotes, homework.quotes+1)
      end
    end
    [@ori, @flag, @save_message]
  end

  def send_exercise_to_course user,params
    # send_id  = params[:send_id]
    # @ori = Attachment.find_by_id(send_id)
    # course_ids = params[:course_ids]
    # @flag = false
    # unless course_ids.nil?
    #   course_ids.each do |id|
    #     next if @ori.blank?
    #     @exist = false
    #     Course.find(id).attachments.each do |att|  #如果课程中包含该资源
    #       if att.id == @ori.id || (!att.copy_from.nil? && !@ori.copy_from.nil? && att.copy_from == @ori.copy_from) || att.copy_from == @ori.id || att.id == @ori.copy_from
    #         att.created_on = Time.now
    #         att.save
    #         @exist = true
    #         @flag = true
    #         break
    #       end
    #     end
    #     next if @exist
    #     attach_copied_obj = @ori.copy
    #     attach_copied_obj.tag_list.add(@ori.tag_list) # tag关联
    #     attach_copied_obj.container = Course.find(id)
    #     attach_copied_obj.created_on = Time.now
    #     attach_copied_obj.author_id = user.id
    #     attach_copied_obj.is_public = 0
    #     attach_copied_obj.copy_from = @ori.copy_from.nil? ? @ori.id : @ori.copy_from #发送要添加copy_from
    #     if attach_copied_obj.attachtype == nil
    #       attach_copied_obj.attachtype = 4
    #     end
    #     if attach_copied_obj.save
    #       # 更新引用次数
    #       quotes = @ori.quotes.to_i + 1
    #       @ori.update_attribute(:quotes, quotes) unless @ori.nil?
    #       @ori.forwards << Forward.new(:to_type => attach_copied_obj.class.name, :to_id => attach_copied_obj.id,:created_at => Time.now)
    #       @flag = true
    #     else
    #       @flag = false
    #       @save_message = attach_copied_obj.errors.full_messages
    #       break
    #     end
    #
    #   end
    # end

    [@ori, @flag, @save_message]
  end

  # 我的资源-课件 已发布的
  def all_course_attachments user

    courses = user.courses.not_deleted

    courses_ids = courses.empty? ? '(-1)' :"(" + courses.map(&:id).join(",") + ")"

    attchments = Attachment.where("(author_id = #{user.id} and is_publish = 1 and container_id in #{courses_ids} and container_type = 'Course') or (container_type = 'Course' and is_publish = 1 and container_id in #{courses_ids})" ).order("created_on desc")

    # attchments.each do |v|
    #   course =  Course.where("id=?",v.container_id).first
    #   v[:coursename] = course.nil? ? "未知" : course.name
    #   v[:attafile_size] = (number_to_human_size(v[:filesize])).gsub("ytes", "").to_s
    # end

    attchments
  end

  # 我的资源-作业  已发布的
  def all_homework_commons user

    courses = user.courses.not_deleted

    courses_ids = courses.empty? ? '(-1)' :"(" + courses.map(&:id).join(",") + ")"

    homeworks = HomeworkCommon.where("course_id in #{courses_ids} and publish_time <= ?",Time.now.strftime("%Y-%m-%d")).order("created_at desc")

    # homeworks.each do |v|
    #   course =  Course.where("id=?",v.course_id).first
    #   v[:coursename] = course.nil? ? "未知" : course.name
    # end

    homeworks
  end

  # 我的资源-测验 已发布的
  def all_exercises user

    courses = user.courses.not_deleted

    courses_ids = courses.empty? ? '(-1)' :"(" + courses.map(&:id).join(",") + ")"

    exercises = Exercise.where("exercise_status <> 1 and course_id in #{courses_ids}").order("created_at desc")

    # exercises.each do |v|
    #   course =  Course.where("id=?",v.course_id).first
    #   v[:coursename] = course.nil? ? "未知" : course.name
    # end

    exercises
  end

end