class TestController < ApplicationController

  before_filter :authenticate, :check_viewability

#
#  COMMENT OUT: filter in each action instead
#
#  before_filter :verify_time_limit, :only => [:submit]

  verify :method => :post, :only => [:submit],
         :redirect_to => { :action => :index }

  def index
    prepare_index_information
  end

  def submit
    @user = User.find(session[:user_id])

    @submitted_test_request = TestRequest.new_from_form_params(@user,params[:test_request])

    if ! @submitted_test_request.errors.empty?
      prepare_index_information
      render :action => 'index' and return
    end

    if GraderConfiguration.time_limit_mode?
      if @user.contest_finished?
        @submitted_test_request.errors.add(:base,'Contest is over.')
        prepare_index_information
        render :action => 'index' and return
      end

      if !GraderConfiguration.allow_test_request(@user)
        prepare_index_information
        flash[:notice] = 'Test request is not allowed during the last 30 minutes'
        redirect_to :action => 'index' and return
      end
    end

    if @submitted_test_request.save
      redirect_to :action => 'index'
    else
      prepare_index_information
      render :action => 'index'
    end
  end
  
  def read
    user = User.find(session[:user_id])
    begin
      test_request = TestRequest.find(params[:id])
    rescue
      test_request = nil
    end
    if test_request==nil or test_request.user_id != user.id
      flash[:notice] = 'Invalid output'
      redirect_to :action => 'index'
      return
    end
    if test_request.output_file_name!=nil
      data = File.open(test_request.output_file_name).read(2048)
      if data==nil
        data=""
      end
      send_data(data,
                {:filename => 'output.txt',
                  :type => 'text/plain'})
      return
    end
    redirect_to :action => 'index'
  end

  def result
    @user = User.find(session[:user_id])
    begin
      @test_request = TestRequest.find(params[:id])
    rescue
      @test_request = nil
    end
    if @test_request==nil or @test_request.user_id != @user.id
      flash[:notice] = 'Invalid request'
      redirect_to :action => 'index'
      return
    end
  end
    
  protected
  
  def prepare_index_information
    @user = User.find(session[:user_id])
    @submissions = Submission.find_last_for_all_available_problems(@user.id)
    all_problems = @submissions.collect { |submission| submission.problem }
    @problems = []
    all_problems.each do |problem|
      if problem.test_allowed
        @problems << problem
      end
    end
    @test_requests = []
    @user.test_requests.each do |ts|
      if ts.problem and ts.problem.available
        @test_requests << ts
      end
    end
  end

  def check_viewability
    user = User.find(session[:user_id])
    if !GraderConfiguration.show_tasks_to?(user)
      redirect_to :controller => 'main', :action => 'list'
    end
    if (!GraderConfiguration.show_submitbox_to?(user)) and (action_name=='submit')
      redirect_to :controller => 'test', :action => 'index'
    end
  end

end
