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

require "time"
require "fileutils"
$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/page"
require "qwik/site-attach"
require "qwik/siteconfig"
require "qwik/sitelog"
require "qwik/sitemember"
require "qwik/sitemonitor"
require "qwik/memory"

module Qwik
  class Site
    include Enumerable

    def initialize(config, memory, sitename)
      @config = config
      @memory = memory

      @sitename = sitename
      @dir = @config.data_dir+"/"+sitename
      @path = @dir.path
      @pages = Pages.new(@config, @path)

      @cache_path = @config.cache_dir.path
      @cache_path.mkdir if !@cache_path.directory?

      @hostname = @config.default_hostname
      @hostname = @sitename if external_site?
    end
    attr_reader :sitename
    attr_reader :dir
    attr_reader :cache_path

    # FIXME: Why?
    def get_pages # only for test
      @pages
    end

    def inspect();	"#<Site:"+@sitename+">";	end

    def backupdb();	@pages.backupdb;	end
    def db();		@pages.db;	end
    def last_page_time; @pages.last_page_time;	end
    def [](k)		@pages[k];		end
    def exist?(k)	@pages.exist?(k);	end
    def each(*a, &b)	@pages.each(*a, &b);	end
    def each_all(*a, &b) @pages.each_all(*a, &b);	end
    def to_a(*a)	@pages.to_a(*a);	end
    def create(k)	@pages.create(k);	end
    def create_new();	@pages.create_new;	end
    def get_new_id();	@pages.get_new_id;	end
    def delete(k)	@pages.delete(k);	end
    def title_list();	@pages.title_list;	end
    def date_list();	@pages.date_list;	end
    def path(f)		@pages.path(f);	end
    def close();	@pages.close;	end
    def pagetitle();	@pages.pagetitle;	end
    def get_by_title(t)	@pages.get_by_title(t);	end

    # URL
    def domainurl
      "http://"+@hostname+"/"
    end

    def urlpath
      pa = ""
      pa += @sitename+"/" unless top_site? || external_site?
      pa
    end

    def url(k=nil)
      u = domainurl
      u += urlpath
      page = self[k] if k
      u += page.url if page
      u
    end

    def ml
      return "info@"+@sitename if external_site?
      return @sitename+"@"+@hostname
    end

    # QuickML support
    def forward?;	exist_file?(",forward");	end
    def permanent?;	exist_file?(",permanent");	end
    def exist_file?(f)
      (@dir.path+f).exist?
    end

    def inactive?
      return false if forward? or permanent?
      mtime = @pages.last_article_time.to_i
      return true if mtime.nil? # no file
      ml_life_time = siteconfig["ml_life_time"].to_i
      mtime + ml_life_time <= Time.now.to_i
    end

    # siteconfig.rb
    def siteconfig
      @siteconfig = SiteConfig.new(@config, self) unless defined? @siteconfig
      @siteconfig
    end

    def is_open?
      siteconfig["open"] == "true"
    end

    def title
      sitetitle = siteconfig["sitename"]
      sitetitle = "" if sitetitle == @config.default_hostname
      #qp sitetitle
      return sitetitle
    end

    def external_site? # external qwik site
      @sitename.include?(".")
    end

    def top_site?
      # @sitename == "www"
      @sitename == @config.default_sitename
    end
    private :top_site?

    # act-interwiki
    def interwiki
      @interwiki = InterWiki.new(@config, self) unless defined? @interwiki
      @interwiki
    end

    # act-sitelog
    def log(time, user, cmd, pagename)
      sitelog.add(time, user, cmd, pagename)
    end

    def sitelog
      @sitelog = SiteLog.new(@config, self) unless defined? @sitelog
      @sitelog
    end

    # act-member
    def member
      @member = SiteMember.new(@config, self) unless defined? @member
      @member
    end

    # act-monitor
    def sitemonitor
      @sitemonitor = SiteMonitor.new(@config, self) unless defined? @sitemonitor
      @sitemonitor
    end

    # act-theme
    def theme
      self.siteconfig["theme"]
    end

    def theme_path
      ac = @sitename+".css"
      return "/"+@sitename+"/.css/"+ac if self.attach.exist?(ac)
      t = self.theme
      if /\Ahttp:\/\// =~ t
	return "/.css/"+t
      end
      ".theme/#{t}/#{t}.css"
    end

    # siteattach
    def attach
      @attach = SiteAttach.new(@dir) unless defined? @attach
      @attach
    end

    # pagefiles
    def files(k)
      @files = {} unless defined? @files
      @files[k] = PageFiles.new(@dir, k) if @files[k].nil?
      @files[k]
    end

    # sitedb
    def sitedb
      @sitedb = SiteDB.new(@config, self) unless defined? @sitedb
      @sitedb
    end
  end
end

if $0 == __FILE__
  require "qwik/test-module-session"
  require "qwik/farm"
  $debug = true
end

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

    def test_site
      # test_url_www
      site = @memory.farm.get_site("www")
      assert_equal("http://example.com/", site.domainurl)
      assert_equal("http://example.com/", site.url)
      assert_equal("www@example.com", site.ml)
#      assert_equal("example.com", site.title)
      site.erase_all

      # test_url_with_dot
      with_site("e.com"){
	site = @memory.farm.get_site("e.com")
	assert_equal("http://e.com/", site.domainurl)
	assert_equal("http://e.com/", site.url)
	assert_equal("info@e.com", site.ml)
#	assert_equal("e.com", site.title)
	site.erase_all
      }

      # test_inactive
      site = @memory.farm.get_site("www")
      @site = site
      #assert_equal(true, @site.inactive?) # should test?
      page = @site.create_new
      assert_equal(false, @site.inactive?)
      page = @site["_SiteConfig"]
      page.store(":ml_life_time:0") # die soon.
      assert_equal(true, @site.inactive?)
      site.erase_all
    end
  end
end
