# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

module Qwik
  class QwikError < StandardError; end
  class RequireLogin < QwikError; end
  class RequirePost < QwikError; end
  class PageNotFound < QwikError; end

  class NoPathArgs < QwikError; end

  class RequireMember < QwikError; end
  class InvalidUserError < QwikError; end
  class BaseIsNotSitename < QwikError; end

  class Action
    #------------------------------------------------------------ error check
    def c_login?
      return @req.user != nil
    end

    def c_require_login # You must logged in.
      raise RequireLogin unless c_login?
    end

    def c_member?
      c_login? && @site.member.exist?(@req.user)
    end

    def c_require_member # You must be a member of this group.
      raise RequireMember unless c_member?
    end

    def c_require_post
      raise RequirePost unless @req.is_post?
    end

    def c_require_page_exist
      raise PageNotFound if @site[@req.base].nil?
    end

    def c_require_no_path_args
      raise NoPathArgs if 0 < @req.path_args.length
      #return c_nerror("Path args are not acceptable.")
    end

    def c_require_no_ext_args
      raise "no ext args" if 0 < @req.ext_args.length
    end

    def c_require_pagename
      raise "pagename is nil" if @req.base.nil?
      # FIXME: But, pagename is always not nil for now.
    end

    def c_require_base_is_sitename
      raise BaseIsNotSitename if @req.base != @req.sitename
    end

    def c_require_lib(lib)
      begin
	require lib
      rescue
	raise "there is no #{lib} library"
      end
    end
  end
end

if $0 == __FILE__
  $LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
  require "t/test"
  $debug = true
end

if $debug
  class TC_ActCondition < Test::Unit::TestCase
    include TestSession

    def test_condition
      res = session

      # test_c_login
      assert_equal(true, @action.c_login?)

      # test_c_require_login
      @action.c_require_login # ok, nothing happen.

      # test_c_member
      assert_equal(false, @action.c_member?)

      # test_c_require_member
      assert_raise(Qwik::RequireMember) {
	@action.c_require_member
      }

      t_add_user

      # test_c_member, again
      assert_equal(true, @action.c_member?)

      # test_c_require_member, again
      @action.c_require_member # nothing happen

      # test_c_require_post
      session {|req|
	assert_equal(false, req.is_post?)
      }
      assert_raise(Qwik::RequirePost) {
	@action.c_require_post
      }
      session("POST /test/1.html")
      @action.c_require_post # nothing happen

      # test_c_require_page_exist
      session("/test/1.html") {|req|
	assert_equal(nil, @site[req.base])
      }
      assert_raise(Qwik::PageNotFound) {
	@action.c_require_page_exist
      }

      # test_c_require_no_path_args
      session("/test/1.html/1") {|req|
	assert_equal(["1"], req.path_args)
      }
      assert_raise(Qwik::NoPathArgs) {
	@action.c_require_no_path_args
      }

      # test_c_require_no_ext_args
      session("/test/1.t.html") {|req|
	assert_equal(["t"], req.ext_args)
      }
      assert_raise(RuntimeError) {
	@action.c_require_no_ext_args
      }

      # test_c_require_pagename
      # FIXME: Pagename is always not nil for now.

      # test_c_require_base_is_sitename
      session("/test/1.t.html") {|req|
	assert_equal(["t"], req.ext_args)
      }
      assert_raise(RuntimeError) {
	@action.c_require_no_ext_args
      }

    end
  end
end
