use utf8;
use warnings;
no warnings 'redefine';
use strict;

use Dbase::Globals qw(content);
use Dbase::Help qw(DoFn);
use Fehler qw(problem report_fehler);
use Loader qw(
  line_in
  list_hostgroup
  log_view
  set_hostgroup_beschreibung
  set_hostgroup_kunde
  set_hostgroup_name
  strip_kn
  toggle_host_in_group
  valid_kunde
  warn_arbeit
  warn_kunde
);

sub edit_hostgroup($;$$) {
	my ( $idi, $id, $kn ) = @_;
	{
		my $kunde = DoFn("SELECT kunde FROM hostgroup WHERE id = $idi")
		  or return problem("Hostgroup #$idi nicht gefunden.");

		return unless valid_kunde( $id ||= $kunde, 1 );
	}
	$kn = strip_kn($kn) . "Hostgroup_$idi";

	warn_arbeit( hostgroup => $idi, '*', $kn );
	warn_kunde( hostgroup => $idi, $id );

	while ( content( my $act = line_in( "$kn >", 4 ) ) ) {

		if ( $act eq '?' ) {
			print <<'_';
h   Host hinzufügen oder löschen
n   Namen ändern
b   Beschreibung
k   Kunden ändern
l   anzeigen
del Hostgroup löschen
_
			next;
		}

		if ( $act eq 'b' ) { set_hostgroup_beschreibung($idi); next; }
		if ( $act eq 'h' ) {
			toggle_host_in_group( $id, $kn, undef, $idi );
			next;
		}
		if ( $act eq 'H' ) { log_view( $kn, hostgroup => id => $idi ); next; }
		if ( $act eq 'k' )   { set_hostgroup_kunde($idi); next; }
		if ( $act eq 'l' )   { list_hostgroup($idi);      next; }
		if ( $act eq 'n' )   { set_hostgroup_name($idi);  next; }
		if ( $act eq 'del' ) { delete_hostgroup($idi);    return; }

		print "Aktion '$act' unbekannt.\n";
		next;
		fehler: report_fehler(4);
	}
	$idi;
}

1;
