#*************************************************************************
#*
#*  $Author$
#*  $URL$
#*
#*  This module has been implemented in Tcl by using mostly the corresponding module
#*  Application/Resource/FrontController.php from the Zend framework see: http://framework.zend.com
#*  So the ideas are also copyrighted by Zend Technologies USA Inc. (http://www.zend.com)
#*  The implementation in Tcl uses dicts and lists instead of php arrays.
#* 
#*  The Contents of this file are made available subject to the terms of
#*  the following license see @license below
#*
#*  Copyright 2010 Arnulf Wiedemann
#*
#*  Author: Arnulf Wiedemann
#*
#*  Contributor(s): 
#*
# @category    ATWF
# @package     library
# @subpackage  Applications::Resources
# @license     BSD License (see file license.terms in the source top drectory)
# @version     $Id$
#************************************************************************/

namespace eval ::ATWF {

::itcl::extendedclass Applications::Resources::Frontcontroller {
    inherit ::ATWF::Applications::Resources::Resourcebase

     # @variable ::ATWF::Controller::Front
    protected variable _front [list]

    constructor {options} {
        chain $options
    } {
    }

    public method init {}
    public method getFrontController {}
}

  #============================ init ===========================================
  # Initialize Front Controller
  # 
  # @return ::ATWF::Controller::Front
::itcl::body Applications::Resources::Frontcontroller::init {} {
    set front [getFrontController]
        
    foreach {key value} [getOptions] {
        switch [string tolower $key] {
        controllerdirectory {
            if {![::itcl::is object $value] && ([llength $value] == 1)} {
                $front setControllerDirectory $value
            } else {
	        if {[llength $value] >= 2} {
                    foreach {module directory} $value {
                        $front addControllerDirectory $directory $module
		    }
                }
            }
          }
        modulecontrollerdirectoryname {
            $front setModuleControllerDirectoryName $value
          }
        moduledirectory {
	    foreach dir $value {
                $front addModuleDirectory $dir
	    }
          }
        defaultcontrollername {
            $front setDefaultControllerName $value
          }
        defaultaction {
            $front setDefaultAction $value
          }
        defaultmodule {
            $front setDefaultModule $value
          }
        baseurl {
            $front setBaseUrl $value
          }
        params {
            $front setParams $value
          }
        plugins {
            foreach pluginClass $value {
                set plugin [uplevel #0 $pluginClass #auto]
                $front registerPlugin $plugin
            }
          }
        throwexceptions {
            $front throwExceptions $value
          }
        actionhelperpaths {
            if {[llength $value] >= 2} {
                foreach {helperPrefix helperPath} $value {
                    ::ATWF::Controller::Action::HelperBroker::addPath $helperPath $helperPrefix
                }
            }
          }
        default {
            $front setParam $key $value
          }
        }
    }

    set bootstrap [getBootstrap]
    if {$bootstrap ne ""} {
        [getBootstrap] setDict FrontController $front
    }

    return $front
}

  #============================ getFrontController =============================
  # Retrieve front controller instance
  # 
  # @return ::ATWF::Controller::Front
::itcl::body Applications::Resources::Frontcontroller::getFrontController {} {
    if {$_front eq ""} {
	classIsLoaded ::ATWF::Controllers::Front
        set _front [::ATWF::Controllers::Front::getInstance]
    }
    return $_front
}

} ; # END ::ATWF

