class FilesController < ApplicationController
  include MessagesHelper

  before_action :require_login, except: %i[index]
  before_action :find_course, except: %i[public_with_course_and_project mine_with_course_and_project]
  before_action :find_ids, only: %i[bulk_delete bulk_send bulk_move bulk_public]
  before_action :file_validate_sort_type, only: :index
  before_action :validate_send_message_to_course_params, only: :bulk_send
  before_action :set_pagination, only: %i[index public_with_course_and_project mine_with_course_and_project]
  before_action :validate_upload_params, only: %i[upload import]
  before_action :find_file, only: %i[show setting update]

  SORT_TYPE = %w[created_on downloads quotes]

  def index
    sort = params[:sort] || 0 # 0: 降序;1: 升序
    sort_type = params[:sort_type] || 'created_on' # created_on:时间排序, downloads:下载次数排序; quotes: 引用次数排序
    course_second_category_id = params[:course_second_category_id] || 0 # 0: 为主目录, 其他为次目录id
    @user = current_user || nil
    @attachments = @course.attachments.by_keywords(params[:search])
                       .includes(attachment_group_settings: :course_group, author: [:user_extension, :course_members])
                       .by_course_second_category_id(course_second_category_id)
                       .ordered(sort: sort.to_i, sort_type: sort_type.strip)
    get_category(@course, course_second_category_id)

    case @user.course_identity(@course)
    when 5
      # 课程学生
      @attachments = @attachments.published
    when 6 || 7
      # 非课堂成员
      @attachments = @attachments.publiced.published
    end

    @total_count = @attachments.size
    @public_count = @attachments.publiced.size
    @private_count = @total_count - @public_count

    @attachments = @attachments.page(@page).per(@page_size)
  end

  def bulk_delete
    ActiveRecord::Base.transaction do
      begin
        @course.attachments.by_ids(@attachment_ids).destroy_all
      rescue Exception => e
        tip_exception(e.message)
        raise ActiveRecord::Rollback
      end
    end
  end

  def bulk_send
    return  normal_status(403, "您没有权限进行该操作") unless current_user.teacher_of_course?(@course)
    course_ids = params[:to_course_ids]

    begin
      @attachment_ids.each do |id|
        @attachment = @course.attachments.find_by_id id
        course_ids.each do |course_id|
          course = Course.find_by_id course_id
          unless @attachment.nil? || course.nil?
            course.attachments << course.attachments.build(@attachment.attributes.except("id").merge(
                quotes: 0,
                downloads: 0,
                author_id: current_user.id,
                created_on: Time.now,
                course_second_category_id: 0 # TODO 暂时只支持发送到其他课堂的主目录
            ))
          end
        end
      end
    rescue Exception => e
      uid_logger_error(e.message)
      tip_exception(e.message)
      raise ActiveRecord::Rollback
    end
  end

  def bulk_move
    return  normal_status(403, "您没有权限进行该操作") unless current_user.teacher_of_course?(@course)

    to_category_id = params[:to_category_id] || 0 # 默认移动到主目录
    unless to_category_id == 0
      course_second_category = @course.course_second_categories.find_by_id to_category_id
      return normal_status(2, "参数to_category_id有误,该目录不存在") if course_second_category.nil?
    end

    begin
      @course.attachments.by_ids(@attachment_ids).update_all(course_second_category_id: to_category_id)
    rescue Exception => e
      uid_logger_error(e.message)
      tip_exception(e.message)
      raise ActiveRecord::Rollback
    end
  end

  def bulk_public
    @user = current_user
    return  normal_status(403, "您没有权限进行该操作") unless @user.teacher_of_course?(@course)

    @course.attachments.by_ids(@attachment_ids).update_all(is_public: 1)
  end

  def public_with_course_and_project
    @attachments = Attachment.publiced.simple_columns
                       .contains_course_and_project
                       .includes(:author => :user_extension)
                       .by_filename_or_user_name(params[:search])
                       .ordered(sort: 0, sort_type: 'created_on')


    @total_count = @attachments.size
    @attachments = @attachments.page(@page).per(@page_size)
  end

  def mine_with_course_and_project
    @current_user = current_user
    @attachments = Attachment.mine(current_user)
                       .simple_columns
                       .contains_course_and_project
                       .by_keywords(params[:search])
                       .ordered(sort: 0, sort_type: 'created_on')

    @total_count = @attachments.size
    @attachments = @attachments.page(@page).per(@page_size)
  end

  # 上传资源
  def upload
    attachment_ids = params[:attachment_ids]
    course_second_category_id = params[:course_second_category_id] || 0 # 0: 为主目录, 其他为次目录id
    is_unified_setting = params.has_key?(:is_unified_setting) ? params[:is_unified_setting] : true
    publish_time = params[:publish_time]
    course_group_publish_times = params[:course_group_publish_times] || []

    begin
      attachment_ids.each do |attchment_id|
        attachment = Attachment.find_by_id attchment_id
        unless attachment.nil?
          attachment.container = @course
          attachment.course_second_category_id = course_second_category_id
          attachment.description = params[:description]
          attachment.is_public = params[:is_public] ? 1 : 0
          attachment.set_publish_time(publish_time) if is_unified_setting
          attachment.set_course_group_publish_time(@course, course_group_publish_times) if @course.course_groups.size > 0 && !is_unified_setting && publish_time.blank?
          attachment.save!
        end
      end
    rescue Exception => e
      uid_logger_error(e.message)
      tip_exception(e.message)
      raise ActiveRecord::Rollback
    end
  end

  # 选用资源 & 导入资源
  def import
    return  normal_status(403, "您没有权限进行该操作") unless current_user.teacher_of_course?(@course) || current_user.student_of_course?(@course)

    attachment_ids = params[:attachment_ids]
    course_second_category_id = params[:course_second_category_id] || 0 # 0: 为主目录, 其他为次目录id

    begin
      attachment_ids.each do |attachment_id|
        ori = Attachment.find_by_id(attachment_id)
        @course.attachments.each do |att|
          @exist = false
          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
            break
          end
        end

        next if @exist
        attach_copied_obj = ori.copy
        attach_copied_obj.container = @course
        attach_copied_obj.created_on = Time.now
        attach_copied_obj.author = current_user
        attach_copied_obj.is_public = 0
        attach_copied_obj.course_second_category_id = course_second_category_id
        attach_copied_obj.copy_from = ori.copy_from.nil? ? ori.id : ori.copy_from
        if attach_copied_obj.attachtype == nil
          attach_copied_obj.attachtype = 4
        end
        attach_copied_obj.save
        ori.update_columns(quotes: ori.quotes.to_i + 1)
      end
    rescue Exception => e
      uid_logger_error(e.message)
      tip_exception(e.message)
      raise ActiveRecord::Rollback
    end
  end

  # 资源设置
  def update
    return  normal_status(403, "您没有权限进行该操作") unless current_user.teacher_or_admin?(@course) || @file.author == current_user

    is_unified_setting = params[:is_unified_setting]
    publish_time = params[:publish_time]
    is_public = params[:is_public]
    course_group_publish_times = params[:course_group_publish_times] || []

    @old_attachment = @file
    @new_attachment = Attachment.find_by_id params[:new_attachment_id]

    begin
      unless @new_attachment.nil?
        @new_attachment_history = @old_attachment.become_history
        @new_attachment_history.save!

        @old_attachment.copy_attributes_from_new_attachment(@new_attachment)
        @old_attachment.is_public = is_public == true ? 1 : 0 if is_public
        @old_attachment.save!
        @new_attachment.delete
      end
      if params[:description] && !params[:description].strip.blank? && params[:description] != @old_attachment.description
        @old_attachment.description = params[:description]
      end

      @old_attachment.set_public(is_public)

      if is_unified_setting
        @old_attachment.set_publish_time(publish_time)
        @old_attachment.attachment_group_settings.destroy_all
      end

      if publish_time.blank? && @course.course_groups.size > 0 && !is_unified_setting
        @old_attachment.set_course_group_publish_time(@course, course_group_publish_times)
      end

      @old_attachment.save!
    rescue Exception => e
      uid_logger_error(e.message)
      tip_exception(e.message)
      raise ActiveRecord::Rollback
    end
  end

  def show
    return normal_status(403, "您没有权限进行该操作") if !current_user.teacher_of_course?(@course) && current_user != @file.author
    @attachment_histories = @file.attachment_histories
  end

  def histories
    @user = current_user
    @file = @course.attachments.find_by_id params[:id]

    return normal_status(-2, "该课程下没有id为 #{params[:id]}的资源") if @file.nil?
    return normal_status(403, "您没有权限进行该操作") if @user != @file.author && !@user.teacher_of_course?(@course) && !@file.public?

    @attachment_histories = @file.attachment_histories
  end

  def get_category(course, category_id)
    if category_id == 0
      category = course.attachment_course_modules.first
      @category_id = category.try(:id)
      @category_name = category.try(:module_name)
    else
      category = CourseSecondCategory.find category_id
      @category_id = category.try(:id)
      @category_name = category.try(:name)
    end
  end

  private
  def find_file
    @file = Attachment.find params[:id]
  end

  def find_attachment_ids(attachment_ids = params[:attachment_ids])
    return normal_status(-2, "参数attachment_ids不能为空!") if attachment_ids.blank?
    return normal_status(-2, "参数attachment_ids格式错误!") if !attachment_ids.is_a? Array
  end

  def find_course_second_category_id
    course_second_category_id = params[:course_second_category_id] || 0 # 0: 为主目录, 其他为次目录id
    if course_second_category_id != 0
      course_second_category = CourseSecondCategory.find_by(id: course_second_category_id, category_type: "attachment")
      return normal_status(-2, "未来找到course_second_category为 #{course_second_category_id} 的目录") if course_second_category.nil?
    end
  end

  def find_ids
    @attachment_ids = params[:ids] || []
    find_attachment_ids(@attachment_ids)
  end

  def file_validate_sort_type
    normal_status(-2, "参数sort_tyope暂时只支持 'created_on', 'quotes', 'downloads'") if params.has_key?(:sort_type) && !SORT_TYPE.include?(params[:sort_type].strip)
  end

  def validate_upload_params
    find_attachment_ids
    find_course_second_category_id
  end
end