class SubDocumentCommentsController < ApplicationController
  before_filter :find_subdomain_and_subfield, :only => [:new, :create, :show, :index, :destroy, :edit]
  before_filter :find_subfield_content, :only => [:show, :index]
  before_filter :authorize_allowed, :only => [:create, :add_reply]
  helper :attachments,:organizations
  layout 'base_sub_domain'

  def new
    @sub_document_comment = SubDocumentComment.new
  end


  def create
    @sub_document_comment = SubDocumentComment.new(:sub_domain_id => params[:sub_domain_id], :creator_id => User.current.id, :org_subfield_id => params[:org_subfield_id])
    @sub_document_comment.title = params[:sub_document_comment][:title]
    @sub_document_comment.content = params[:sub_document_comment][:content]
    # @sub_document_comment.save_attachments(params[:attachments])
    if params[:field_id]
      @sub_document_comment.org_subfield_id = params[:field_id].to_i
    end
    if @sub_document_comment.save
      flash.keep[:notice] = l(:notice_successful_create)
      redirect_to org_subfield_sub_domain_sub_document_comments_path(:org_subfield_id => @org_subfield.id, :sub_domain_id => @subdomain.id)
      # EditorOfDocument.create(:editor_id => User.current.id, :org_document_comment_id => @sub_document_comment.id, :created_at => @sub_document_comment.updated_at)
      # if params[:field_id]
      #   @org_subfield = OrgSubfield.find(params[:field_id])
      #   if @org_subfield.subfield_subdomain_dir.nil?
      #     redirect_to organization_path(@organization, :org_subfield_id => params[:field_id])
      #   else
      #     redirect_to show_org_subfield_organization_path(:id => @organization.id, :sub_dir_name => @org_subfield.subfield_subdomain_dir.name)
      #   end
      # else
      #   redirect_to organization_org_document_comments_path(@organization)
      # end
    else
      redirect_to new_org_document_comment_path(:organization_id => @organization.id)
    end
  end

  def show
    if @organization.is_public? || User.current.admin? || User.current.member_of_org?(@organization)
      @document = SubDocumentComment.find(params[:id])
      respond_to do |format|
        format.html {render :layout => @organization.switch_type ? 'base_sub_domain' : 'base_org'}
      end
    else
      render_403
    end
  end

  def index
    if @organization.is_public? || User.current.admin? || User.current.member_of_org?(@organization)
      @documents = @subdomain.sub_document_comments.where("parent_id is null").order("created_at desc")
      @is_remote = true
      @limit = 20
      @atta_count = @documents.count
      @atta_pages = Paginator.new @atta_count, @limit, params['page'] || 1
      @offset ||= @atta_pages.offset
      @documents = paginateHelper @documents,20
      respond_to do |format|
        format.html {render :layout => @organization.switch_type ? 'base_sub_domain' : 'base_org'}
      end
    else
      render_403
    end
  end

  def update
    @sub_document =  SubDocumentComment.find(params[:id])
    @sub_document.update_attributes(:title => params[:sub_document_comment][:title], :content => params[:sub_document_comment][:content])
    @organization = OrgSubfield.find(params[:org_subfield_id]).organization
    # Attachment.attach_files(@org_document, params[:attachments])
    # @org_document.save_attachments(params[:attachments])
    if @sub_document.parent.nil?
      act  = OrgActivity.where("org_act_type='SubDocumentComment' and org_act_id =?", @sub_document.id).first
      act.update_attributes(:updated_at => @sub_document.updated_at)
      # EditorOfDocument.create(:editor_id => User.current.id, :org_document_comment_id => @org_document.id, :created_at => Time.now)
    end
    respond_to do |format|
      format.html {
        if params[:flag].to_i == 0

          # render :action => 'index', :layout => @organization.switch_type ? 'base_sub_domain' : 'base_org'
          redirect_to org_subfield_sub_domain_sub_document_comments_path(:org_subfield_id => params[:org_subfield_id], :sub_domain_id => params[:sub_domain_id])
          # render :layout => @organization.switch_type ? 'base_sub_domain' : 'base_org'
          # redirect_to organization_path(@organization)
        else
          redirect_to org_subfield_sub_domain_sub_document_comment_path(:org_subfield_id => params[:org_subfield_id], :sub_domain_id => params[:sub_domain_id])
        end
      }
    end
  end

  def edit
    @sub_document = SubDocumentComment.find(params[:id])
    @flag = params[:flag]
    render :layout => @organization.switch_type ? 'base_sub_domain' : 'base_org'
  end

  def add_reply
    @document = SubDocumentComment.find(params[:id]).root
    @subdomain = @document.sub_domain
    @org_subfield = @subdomain.org_subfield
    @organization = @org_subfield.organization
    @act = OrgActivity.find(params[:id])
    @comment = SubDocumentComment.new(:sub_domain_id => @document.sub_domain, :creator_id => User.current.id, :reply_id => params[:id])
    @comment.content = params[:sub_content]
    @document.children << @comment
    @document.save
  end

  def add_reply_in_doc
    @document = SubDocumentComment.find(params[:id]).root
    @subdomain = @document.sub_domain
    @org_subfield = @subdomain.org_subfield
    @organization = @org_subfield.organization
    @comment = SubDocumentComment.new(:sub_domain_id => @document.sub_domain_id, :creator_id => User.current.id, :reply_id => params[:id])
    @comment.content = params[:flag] ? params[:sub_content] : params[:org_comment][:sub_content]
    @document.children << @comment
    @document.save
    respond_to do |format|
      format.html {redirect_to org_subfield_sub_domain_sub_document_comment_path(:id => @document.id, :org_subfield_id => @org_subfield.id, :sub_domain_id => @subdomain.id)}
    end
  end

  def destroy
    @sub_document_comment = SubDocumentComment.find(params[:id])
    @sub_document_comment.destroy
    @sub_domain = @sub_document_comment.sub_domain
    @org_subfield = @sub_domain.org_subfield
  rescue ActiveRecord::RecordNotFound
    respond_to do |format|
      # format.html{
      #   redirect_to org_subfield_sub_domain_sub_document_comments_path(:id => params[:sub_domain_id], :org_subfield_id => params[:org_subfield_id])
      # }
      format.js
    end
  end

  def delete_reply
    @sub_document_comment = OrgDocumentComment.find(params[:id])
    @document = @sub_document_comment.root
    org = @sub_document_comment.organization
    @sub_document_comment.destroy
    respond_to do |format|
      format.html {redirect_to org_document_comment_path(:id => @document.id, :organization_id => @document.organization_id)}
    end
  end
  def quote
    @org_comment = OrgDocumentComment.find(params[:id])
    @subject = @org_comment.content
    @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')

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

  def reply
    @document = OrgDocumentComment.find(params[:id]).root
    @quote = params[:quote][:quote]
    @org_document = OrgDocumentComment.new(:creator_id => User.current.id, :reply_id => params[:id])

    # params[:blog_comment][:sticky] = params[:blog_comment][:sticky] || 0
    # params[:blog_comment][:locked] =  params[:blog_comment][:locked] || 0
    @org_document.title = params[:org_document_comment][:title]
    @org_document.content = params[:org_document_comment][:content]
    @org_document.content = @quote + @org_document.content
    #@org_document.title = "RE: #{@article.title}" unless params[:blog_comment][:title]
    @document.children << @org_document
    # @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(@org_document, params[:attachments])
    # render_attachment_warning_if_needed(@org_document)
    #@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 org_document_comment_path(:id => @document.id, :organization_id => @document.organization_id)
        # end

      }
      format.js
    end
  end

  private
  def find_subdomain_and_subfield
    @subdomain = SubDomain.find(params[:sub_domain_id])
    @org_subfield = OrgSubfield.find(params[:org_subfield_id])
    @organization = @org_subfield.organization
  rescue ActiveRecord::RecordNotFound
    # source_subdomain_subfield not found
    render_404
  end

  def find_subfield_content
    @subfield_content = @organization.org_subfields.order("priority")
  end

  def authorize_allowed
    unless User.current.logged?
      redirect_to signin_url
      return
    end
  end
end
