class BlogCommentsController < ApplicationController
  include ApplicationHelper
  before_filter :find_user
  def index

  end
  def create
    if User.current.logged?
      @article = BlogComment.new
      @article.author = User.current
      @article.blog_id = params[:blog_id]
      @article.safe_attributes = params[:blog_comment]
      if request.post?
        @article.save_attachments(params[:attachments])
        if @article.save
          # 更新kindeditor上传的图片资源所有者
          # if params[:asset_id]
          #   ids = params[:asset_id].split(',')
          #   update_kindeditor_assets_owner ids,@article.id,OwnerTypeHelper::BLOGCOMMENT
          # end
          render_attachment_warning_if_needed(@article)
        else
        end
        redirect_to user_blogs_path(:user_id=>params[:user_id])
      else
        respond_to do |format|
          format.html {
            render :layout => 'new_base_user'
          }
        end
      end
    else
      redirect_to signin_path
    end
  end
  def new
    respond_to do |format|
      format.html {render :layout=>'new_base_user'}
    end
  end
  def show
    @article = BlogComment.find(params[:id])
    respond_to do |format|
      format.html {render :layout=>'new_base_user'}
    end
  end
  def update
    @article = BlogComment.find(params[:id])
    @article.safe_attributes = params[:blog_comment]
    @article.save_attachments(params[:attachments])
    if @article.save
      render_attachment_warning_if_needed(@article)
    else
    end
    if params[:in_act]
      redirect_to user_path(params[:user_id])
    else
      if @article.id.eql?(User.find(params[:user_id]).blog.homepage_id)
        redirect_to user_path(params[:user_id])
      else
        redirect_to user_blog_blog_comment_path(:user_id=>params[:user_id],:blog_id=>params[:blog_id],:id=>params[:id])
      end
    end
  end
  def destroy
    @article = BlogComment.find(params[:id])
    if @article.parent_id.nil? #如果是文章被删，那么跳转到用户博客界面，如果带了course_id过来，那么就要跳转到课程首页
      if params[:course_id] #如果带了课程id过来，说明这是课程大纲，不要删除，只需取消课程大纲就ok了
        @course = Course.find(params[:course_id])
        @course.outline = 0
        @course.save
        redirect_to course_path(:id=>params[:course_id])
      else
        @article.children.destroy
        @article.destroy
        redirect_to user_blogs_path(:user_id=>User.current)
      end

    else#如果是回复被删，
      if params[:course_id] #如果带了course_id过来了，那么这是要跳到课程大纲去的
        @article.destroy
        redirect_to syllabus_course_path(:id=>params[:course_id])
      else
        root = @article.root
        @article.destroy
        redirect_to user_blog_blog_comment_path(:user_id=>root.author_id,:blog_id=>root.blog_id,:id=>root.id)
      end

    end
  end

  def edit
    @article = BlogComment.find(params[:id])
    if User.current.admin? || User.current.id == @article.author_id
      respond_to do |format|
        format.html { render :layout => 'new_base_user' }
      end
    else
      render_403
    end
  end

  def quote
    @blogComment = BlogComment.find(params[:id])
    @subject = @blogComment.title
    @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')

    @content = "> #{ll(Setting.default_language, :text_user_wrote, @blogComment.author.realname)}\n> "
    @temp = BlogComment.new
    @course_id = params[:course_id]
    @temp.content = "<blockquote>#{ll(Setting.default_language, :text_user_wrote, @blogComment.author.realname)} <br/>#{@blogComment.content.html_safe}</blockquote>".html_safe
    respond_to do | format|
      format.js
    end
  end

  #回复
  def reply
    if params[:in_user_center]
      @in_user_center = true
    end
    @article = BlogComment.find(params[:id]).root
    @quote = params[:quote][:quote]
    @blogComment = BlogComment.new
    @blogComment.author = User.current
    @blogComment.blog = Blog.find(params[:blog_id])
    params[:blog_comment][:sticky] = params[:blog_comment][:sticky] || 0
    params[:blog_comment][:locked] =  params[:blog_comment][:locked] || 0
    @blogComment.safe_attributes = params[:blog_comment]
    @blogComment.content = @quote + @blogComment.content
    @blogComment.title = "RE: #{@article.title}" unless params[:blog_comment][:title]
    @article.children << @blogComment
    @article.save
    # @article.update_attribute(:updated_on, @blogComment.updated_on)
    @user_activity_id = params[:user_activity_id]
    user_activity = UserActivity.where("act_type='BlogComment' and act_id =#{@article.id}").first
    if user_activity
      user_activity.updated_at = Time.now
      user_activity.save
    end
    attachments = Attachment.attach_files(@blogComment, params[:attachments])
    render_attachment_warning_if_needed(@blogComment)
    #@article.save
   # redirect_to user_blogs_path(:user_id=>params[:user_id])
    respond_to do |format|
      format.html {
        if params[:course_id] #如果呆了course_id过来了，那么这是要跳到课程大纲去的
          redirect_to syllabus_course_path(:id=>params[:course_id])
        else
          redirect_to user_blog_blog_comment_path(:user_id=>@article.author_id,:blog_id=>@article.blog_id,:id=>@article)
        end

      }
      format.js
    end
  rescue Exception => e #如果上面的代码执行发生异常就捕获
    flash[:notice] = e.message
  end

  private
  def find_user
    @user = User.find(params[:user_id])
  end
end
