class RelativeMemosController < ApplicationController
  
  helper :sort
  include SortHelper
  helper :apply_project_masters
  include ApplyProjectMastersHelper
  helper :no_uses
  include NoUsesHelper
  
  before_filter :find_memo, :except => [:new, :create]
  before_filter :find_osp, :only => [:create]
  before_filter :require_login, :only => [:new, :create]
  
  layout 'base_opensource_p'
  
  # GET /open_source_projects
  # GET /open_source_projects.json
  def index
    # per_page_option = 10
# 
    # @open_source_projects = OpenSourceProject.all
# 
    # @os_project_count = @open_source_projects.count
    # @os_project_pages = Paginator.new @os_project_count, per_page_option, params['page']
# 
    # @open_source_projects = OpenSourceProject.all
# 
    # respond_to do |format|
      # format.html # index.html.erb
      # format.json { render json: @open_source_projects }
    # end
  end

  # GET /open_source_projects/1
  # GET /open_source_projects/1.json
  REPLIES_PER_PAGE = 10 unless const_defined?(:REPLIES_PER_PAGE)
  def show
    pre_count = REPLIES_PER_PAGE

    # @memo = @memo.root # 取出楼主，防止输入帖子id让回复作为主贴显示
    @memo.update_column(:viewed_count_local, (@memo.viewed_count_local.to_i + 1))

    page = params[:page]
    if params[:r] && page.nil?
      offset = @memo.children.where("#{Memo.table_name}.id < ?", params[:r].to_i).count
      page = 1 + offset / pre_count
    else
      
    end
    @reply_count = @memo.children.count
    @reply_pages = Paginator.new @reply_count, pre_count, page
    @replies = @memo.children.
                includes(:author, :attachments).
                reorder("#{RelativeMemo.table_name}.created_at ASC").
                limit(@reply_pages.per_page).
                offset(@reply_pages.offset).
                all

    @mome_new = RelativeMemo.new


    # @memo = Memo.find_by_id(params[:id])
    # @forum = Forum.find(params[:forum_id])
    # @replies = @memo.replies
    # @mome_new = Memo.new
    
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @memo }
    end
  end
  

  # GET /open_source_projects/new
  # GET /open_source_projects/new.json
  def new
    @open_source_project = OpenSourceProject.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @open_source_project }
    end
  end

  # GET /open_source_projects/1/edit
  def edit
    @open_source_project = OpenSourceProject.find(params[:id])
  end

  # POST /open_source_projects
  # POST /open_source_projects.json
  def create
    @memo = RelativeMemo.new(params[:relative_memo])
    # @memo.url = "http://forge.trustie.net/open_source_projects"
    # @memo.osp_id = params[:open_source_project_id]
    @memo.author_id = User.current.id

    @memo.save_attachments(params[:attachments] || (params[:relative_memo] && params[:relative_memo][:uploads]))

    respond_to do |format|
      if @memo.save
        RelativeMemoToOpenSourceProject.create(:osp_id => params[:open_source_project_id], :relative_memo_id => @memo.id)
        format.html { redirect_to back_memo_url, notice: "#{l :label_memo_create_succ}" }
        format.json { render json: @memo, status: :created, location: @memo }
      else
        flash[:error] = "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}"
        # back_error_page = @memo.parent_id.nil? ? forum_path(@forum) : forum_memo_path(@forum, @memo.parent_id)
        format.html { redirect_to back_memo_url}#, error: "#{l :label_memo_create_fail}: #{@memo.errors.full_messages[0]}" }
        format.json { render json: @memo.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /open_source_projects/1
  # PUT /open_source_projects/1.json
  def update
    @open_source_project = OpenSourceProject.find(params[:id])

    respond_to do |format|
      if @open_source_project.update_attributes(params[:open_source_project])
        format.html { redirect_to @open_source_project, notice: 'Open source project was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @open_source_project.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /open_source_projects/1
  # DELETE /open_source_projects/1.json
  def destroy
    @open_source_project = OpenSourceProject.find(params[:id])
    @open_source_project.destroy

    respond_to do |format|
      format.html { redirect_to open_source_projects_url }
      format.json { head :no_content }
    end
  end
  
  private

  def find_memo
    return unless find_osp
    @memo = @open_source_project.topics.find(RelativeMemo.find(params[:id]).root.id)
  rescue ActiveRecord::RecordNotFound
    render_404
    nil
  end

  def find_osp
    @open_source_project = OpenSourceProject.find(params[:open_source_project_id])
  rescue ActiveRecord::RecordNotFound
    render_404
    nil
  end
  
  def back_memo_url
    open_source_project_relative_memo_path(@open_source_project, (@memo.parent_id.nil? ? @memo : @memo.parent_id))
  end
end
