% 
% 	moz - The "Moo in OZ" project.
%	Version: 1.0.C039
% 	Copyright (C) 2003 Robin Lee Powell.  All rights reserved.
% 	Written by Robin Lee Powell <rlpowell@digitalkingdom.org>
% 
% 	Redistribution and use in source and binary forms, with or without
% 	modification, are permitted provided that the following conditions are
% 	met:
% 	
% 	 1. Redistributions of source code must retain the above copyright
% 	 notice, this list of conditions and the following disclaimer.
% 	
% 	 2. Redistributions in binary form must reproduce the above copyright
% 	 notice, this list of conditions and the following disclaimer in the
% 	 documentation and/or other materials provided with the distribution.
% 	
% 	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
% 	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% 	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% 	DISCLAIMED. IN NO EVENT SHALL THE AUTHER OR CONTRIBUTORS BE LIABLE FOR
% 	ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
% 	DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
% 	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
% 	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
% 	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
% 	ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% 	POSSIBILITY OF SUCH DAMAGE.
%

Control = class $ from Mobile
    %%%%%%%%%
    %
    % Properties
    %
    %%%%%%%%%

    %%%%%%%%%
    %
    % Attributes
    %
    %%%%%%%%%
    attr 
    % An object record, hopefully complete, for the object we
    % control.
    controlled

    %%%%%%%%%%%%%%
    %
    % Features
    %
    %%%%%%%%%%%%%%
    feat

    % The list of things that the Server class saves.
    % ozName#name description#string name#string 
    exports: {Append
	Mobile.exports
	[ controlled#objectRef ]
    }

    % The list of methods we allow the outside world to see, at
    % least potentially
    methodList: Mobile.methodList
    %{Append
    %Mobile.methodList
    %[ advertiseVerb ] %}

    % The name of this class.
    className: 'Control'

    %%%%%%%%%%%%%%
    %
    % Methods
    %
    %%%%%%%%%%%%%%


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % init:
    %
    % One-time object initialization sets various values.
    %
    % controlled:  Full capabilities to the object we control.
    %
    %%%%%%%%%%%%%%%%%%%%
    meth init( controlled: Controlled <= nil ... ) = InitHead
	{MozLog
	    "Starting init on Control."
	    debug
	    InitHead
	}
	Mobile, InitHead

	controlled <- Controlled

	{MozLog
	    "Ending init on Control."
	    debug
	    InitHead
	}
    end






    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % start:
    %
    % Called whenever the object is loaded or re-loaded.
    % 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    meth start( ... ) = MethHead
	% An object reference is another object that "just"
	% (mostly) calls the Wrapper.
	class ReferenceClass from BaseObject
	    attr wrapper ozName storageRef
	    meth wrapper( 1: MyWrapper1 <= _ wrapper: MyWrapper2 <= _ )
		MyWrapper1 = @wrapper
		MyWrapper2 = @wrapper
	    end

	    meth capabilities( 1: MyCap1 <= _ capabilities: MyCap2 <= _ )
		MyCap1 = {@storageRef
		    getCapabilitiesFromOzName(
			ozName: @ozName
			capabilities: $
		    )
		}
		MyCap2 = {@storageRef
		    getCapabilitiesFromOzName(
			ozName: @ozName
			capabilities: $
		    )
		}
	    end

	    meth ozName( 1: MyOzName1 <= _ ozName: MyOzName2 <= _ )
		MyOzName1 = @ozName
		MyOzName2 = @ozName
	    end

	    meth init( wrapper: Wrapper ozName: OzName storageRef: StorageRef )
		wrapper <- Wrapper
		ozName <- OzName
		storageRef <- StorageRef
	    end

	    meth otherwise( Method )
		Capabilities = {@storageRef
		    getCapabilitiesFromOzName(
			ozName: @ozName
			capabilities: $
		    )
		}
	    in
		% Check to make sure we have a capability for
		% what we're supposed to do
		if {HasFeature Capabilities {Label Method} } then
		    {@wrapper
			Capabilities.{Label Method}
			Method
		    }
		else
		    raise
			moz_error( 'You do not have the capability to do that.' )
		    end
		end
	    end
	end
    in
	if {IsFree @controlled} andthen
	{IsObject @controlled} then
	    {MozLog
		"Starting start on Control."
		debug
		MethHead
	    }


	    % Make sure we can never lose our capabilities.
	    controlled <- {New
		ReferenceClass
		init(
		    wrapper: {@controlled wrapper($)}
		    ozName: {@controlled ozName($)}
		    storageRef: @storageRef
		)
		$
	    }

	    {MozLog
		"In start on Control, done setting up control object."
		debug
		MethHead
	    }

	    % Set our name
	    name <- {self getName( name: $ )}

	    {MozLog
		"Done start on Control."
		debug
		MethHead
	    }
	end
    end


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % stop:
    %
    % Stops any processing that may have been started by start.
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    meth stop( ... )
	skip
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % selfMatch
    %
    % Match only based on our generated name.
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    meth selfMatch( ... ) = MethHead
	name <- {self getName( name: $ )}

	Mobile, MethHead
    end


    %%%%%%%%%%%%%%%%%%%%
    %
    % getMethodList
    %
    % Returns our method list plus that of the object we control.
    % 
    %%%%%%%%%%%%%%%%%%%%
    meth getMethodList( methodList: N ) = MethHead
	{MozLog
	    "Starting getMethodList on Control."
	    debug
	    MethHead
	}

	if {IsObject @controlled} then
	    N = {Append
		self.methodList
		{@controlled
		    getMethodList( methodList: $ )
		}
	    }
	else
	    N = self.methodList
	end

	{MozLog
	    "Starting getMethodList on Control."
	    debug
	    MethHead
	}

    end



    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % getName
    %
    % Generate a name based on our controlled item.
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    meth getName( name: StrName ) = MethHead
	{MozLog
	    "Starting getName on Control."
	    debug
	    MethHead
	}


	if {IsObject @controlled} then
	    {MozLog
		"In getName on Control, have @controlled."
		debug
		MethHead
	    }

	    {MozLog
		"In getName on Control, @controlled: "
		# {Value.toVirtualString @controlled 100 100}
		debug
		MethHead
	    }

	    {MozLog
		"In getName on Control, @controlled articled name:"
		# {Value.toVirtualString 
		    {@controlled
			getArticledName( name: $ )
		    }
		    100 100
		}
		debug
		MethHead
	    }

	    StrName = 
	    {Flatten
		[
		    controlGetNamePre

		    {@controlled
			getArticledName( name: $ )
		    }

		    controlGetNamePost
		]
	    }
	else
	    {MozLog
		"In getName on Control, don't have @controlled."
		debug
		MethHead
	    }

	    StrName =
	    {Flatten
		[
		    controlGetNamePre

		    string( any: "Unknown!" )

		    controlGetNamePost
		]
	    }
	end

	{MozLog
	    "In getName on Control, StrName: "
	    # {Value.toVirtualString StrName 100 100}
	    debug
	    MethHead
	}

	name <- StrName

	{MozLog
	    "Done getName on Control."
	    debug
	    MethHead
	}
    end



    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % getVerbs
    %
    % Get the list of controlVerbs from our @controlled object.
    % Those are our verbs.
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    meth getVerbs( verbs: Verbs ) = MethHead
	{MozLog
	    "In getVerbs on Control, MethHead: "
	    # {Value.toVirtualString MethHead 100 100 }
	    debug
	    MethHead
	}

	if {IsObject @controlled} then
	    {@controlled
		getControlVerbs( verbs: Verbs )
	    }
	else
	    Verbs = nil
	end

	{MozLog
	    "In getVerbs on Control, Verbs: "
	    # {Value.toVirtualString Verbs 100 100 }
	    debug
	    MethHead
	}

    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % publicSelfReference:
    %
    % We need to return *everything* here, unfortunately.
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    meth publicSelfReference( selfRef: SelfRef ... )
	{self
	    selfReference(
		selfRef: SelfRef
	    )
	}
    end





    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % Send everything else to @controlled.
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    meth otherwise( Meth ) = MethHead
	{MozLog
	    "In otherwise on Control, MethHead: "
	    # {Value.toVirtualString MethHead 100 100 }
	    debug
	    MethHead
	}

	if {IsObject @controlled} then
	    {@controlled
		Meth
	    }
	end

	{MozLog
	    "Done otherwise on Control."
	    debug
	    MethHead
	}

    end


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % 		Verb-Related Methods
    %
    % These are all the methods that are used to implement the various verbs
    % on the parser class.
    %
    % Standard Arguments:
    % 
    % caller:	The objectRef for the calling object.
    % player: 	The objectRef for the player object.
    % language:	The language of the verb itself, for when the player is
    % 		able to run verbs in multiple languages (i.e. help has
    % 		language en, sidju has language lb).
    % result:	The result of the verb, a record like
    %    result(
    %       status: success|failure|other  -- default success
    %       certainty: float from 0 through 1  -- default 1
    % 	    comments: <string> -- default nil, only relevant for failure
    %    )
    %  See programmer's guide for more information.
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



end
