package Cursed::Window;

use strict;
use warnings;
use include;

use Curses;

# Create and return a new prompt window.
sub init {
  my ($class, %dat) = @_;
  my $self = bless \%dat, $class;
	$self->{ChildOf} = delete $self->{Opts}{ChildOf};
	$self->{Subwindows} = [];
  $self->spawn;
  if (%{ $self->{Opts} }) {
    debug $self->{Opts};
    croak "unhandled options!";
  }
  delete $self->{Opts};
  return $self;
}

# Simply refresh the window
sub draw {
  my $self = shift;
	my $code = "";
	if ($self->can("windraw")) {
		$code = $self->windraw;
	}
  $self->{Win}->refresh unless $code eq "-norefresh";

	# Now update any subwindows lying on top of us.
	foreach my $sub (@{ $self->{Subwindows} }) {
		$sub->draw;
	}
}

# Give a reference to the HexedUI singleton
sub ui {
  my $self = shift;
  return $self->{Frame}{_HexedUI};
}

# Tell our frame to completely redraw us and get rid of residue.
sub redraw {
  my $self = shift;
  $self->{Frame}->redraw($self->{Name});
}

# Unregister subwindows overlapping us.
sub remove_subwins {
  my ($self, $win) = @_;
  # Messy pattern. Modifying arrays in for loops is... owch.
  my @new;
  foreach (@{ $self->{Subwindows} }) {
    push @new, $_ unless $_->{Name} eq $win;
  }
  @{ $self->{Subwindows} } = @new;
}

42;
