package SWOT_INSTANCE;

use vars qw($AUTOLOAD);

{
	#Encapsulated class data
	
	#___________________________________________________________
	#ATTRIBUTES
    my %_attr_data = #     				DEFAULT    	ACCESSIBILITY
		(
		MW			=>	[undef, 		'read/write'],
		);

	#_____________________________________________________________
	#METHODS, to operate on encapsulated class data
	# Is a specified object attribute accessible in a given mode
	sub _accessible  {
	my ($self, $attr, $mode) = @_;
	$_attr_data{$attr}[1] =~ /$mode/
	}

	# Classwide default value for a specified object attribute
	sub _default_for {
	my ($self, $attr) = @_;
	$_attr_data{$attr}[0];
	}

	# List of names of all specified object attributes
	sub _standard_keys {
	keys %_attr_data;
	}

	
}

sub new {
    my ($caller, %args) = @_;
	
    my $caller_is_obj = ref($caller);
    my $class = $caller_is_obj || $caller;

    my $self = bless {}, $class;

    foreach my $attrname ( $self->_standard_keys ) {
    	if (exists $args{$attrname}) {
		$self->{$attrname} = $args{$attrname} }
    elsif ($caller_is_obj) {
		$self->{$attrname} = $caller->{$attrname} }
    else {
		$self->{$attrname} = $self->_default_for($attrname) }
    }

	my $MWmenu = Win32::GUI::MakeMenu(
		"&File"     	=> "File",
		" > &New"		=> "New",
		" > &Open"		=> "Open",
		" > &Save"		=> "Save",
		" > E&xit"		=> "Exit",
		"&Sort"			=> "Sort",
		"> By Impact"	=> "SortByImpact",
		"> By Likelihood" => "SortByLikelihood",
		"> By Overall Importance" => "SortByImportance",
		"&View"			=> "View",
		"> Final Analysis"	=> "FinalAnalysis",
		"&Help"   		=> "Help",
		" > &Info"		=> "Info",
	)or print_and_die ("MenuFailure");

	$self->MW(Win32::GUI::Window->new(
			-name   => 'Main',
			-title   => 'WinSWOT',
			-width  => 300,
			-height => 100,
            -style    => WS_OVERLAPPEDWINDOW,
			-menu   => $MWmenu
			) or print_and_die ("WindowFailure"));

    return $self;

}

sub print_and_die {
    my($text) = @_;
    my $err = Win32::GetLastError();
    die "$text: Error $err\n";
}


sub AUTOLOAD {
    no strict "refs";
    use Carp;
    my ($self, $newval) = @_;
    
    $AUTOLOAD =~ /.*::(\w+)/;

    my $attr=$1;
    if ($self->_accessible($attr,'write')) {

	*{$AUTOLOAD} = sub {
	    if (defined $_[1]) { $_[0]->{$attr} = $_[1] }
	    return $_[0]->{$attr};
	};    ### end of created subroutine

###  this is called first time only
	if (defined $newval) {
	    $self->{$attr} = $newval
	}
	return $self->{$attr};

    } elsif ($self->_accessible($attr,'read')) {

	*{$AUTOLOAD} = sub {
	    return $_[0]->{$attr} }; ### end of created subroutine
	return $self->{$attr}  }

    # Must have been a mistake then...
    croak "No such method: $AUTOLOAD";
}

		
1;
