use utf8;
use warnings; no warnings "redefine";
use strict;
use Dbase::Globals qw(content get_descr);
use Dbase::Help qw(DoFn qquote);
use Fehler qw(problem report_fehler);
use Loader qw(
  edit_messstelle
  line_in
  list_messtyp
  list_messstellen
  set_messtyp_name
  set_messtyp_dienst
  set_messtyp_wertart
  set_messtyp_einheit
  set_messtyp_collectd_typ
  set_messtyp_collectd_intervall
  set_messtyp_rrd_typ
  set_messtyp_rrd_min
  set_messtyp_rrd_max
  set_messtyp_rrd_faktor
  set_messtyp_snmp_faktor
  set_messtyp_oid_praefix
  set_messtyp_info
  set_messtyp_status
  log_view
);

sub edit_messtyp($;$) {
    my ( $idi, $kn ) = @_;

    my ( $name ) = DoFn("SELECT name FROM mess_typ WHERE id = $idi");

    while ( content( my $act = line_in "$kn Messtyp $idi:$name>", 4 ) ) {
        if ( $act eq '?' ) {
            print <<_;
l      Details des Messtyps
ls     Auflistung aller Messstellen dieses Messtyps
n      Namen ändern (Abbruch, falls Name bereits vorhanden)
ct     Collectd_Typ und ändern
rt     RRD-Typ ändern (erzwingt Änderung von collectd_typ)
rmin   RRD-Min-Wert ändern
rmax   RRD-Max-Wert ändern
fr     Faktor für RRD ändern (geht wenn RRD Typ = ${\get_descr("rrd_typ", "1")})
fs     Faktor für SNMP ändern (geht wenn RRD Typ = ${\get_descr("rrd_typ", "2")})
oid    OID-Präfix ändern
ci     Collectd-Intervall ändern
d      Dienst ändern
w      Wertart ändern
e      Einheit ändern
s      Status ändern
i      Info ändern
H      History anzeigen

###    Wählt eine Messstelle anhand der ID aus

_
            next;
        }
        if ( $act eq 'l' )    { list_messtyp($idi); next; }
        if ( $act eq 'ls' )   { list_messstellen( 2, "typ = $idi" ); next; }
        if ( $act eq 'n' )    { set_messtyp_name($idi, $kn); next; }
        if ( $act eq 'ct' )   { set_messtyp_collectd_typ( $idi, $kn ); next; }
        if ( $act eq 'rt' )   { set_messtyp_rrd_typ( $idi, $kn ); next; }
        if ( $act eq 'rmin' ) { set_messtyp_rrd_min($idi, $kn); next; }
        if ( $act eq 'rmax' ) { set_messtyp_rrd_max($idi, $kn); next; }
        if ( $act eq 'fr' ) {
            my ($rrd_typ) = DoFn("SELECT rrd_typ FROM mess_typ WHERE id = $idi");
            if ( $rrd_typ != 1 ) {
                problem('Ändern des RRD-Faktors für nicht-'
                      . get_descr( "rrd_typ", "1" )
                      . '-Werte nicht zulässig. Bitte erst den RRD-Typ ändern.'
                );
                next;
            }
            set_messtyp_rrd_faktor( $idi, $kn );
            next;
        }
        if ( $act eq 'fs' ) {
            my ($rrd_typ) = DoFn("SELECT rrd_typ FROM mess_typ WHERE id = $idi");
            if ( $rrd_typ != 2 ) {
                problem('Ändern des SNMP-Faktors für '
                      . get_descr( "rrd_typ", "1" )
                      . '-Werte nicht zulässig. Bitte erst den RRD-Typ ändern.'
                );
                next;
            }
            set_messtyp_snmp_faktor( $idi, $kn );
            next;
        }
        if ( $act eq 'oid' ) { set_messtyp_oid_praefix($idi, $kn); next; }
        if ( $act eq 'ci' )  { set_messtyp_collectd_intervall($idi, $kn); next; }
        if ( $act eq 'd' )   { set_messtyp_dienst($idi, $kn);  next; }
        if ( $act eq 'w' )   { set_messtyp_wertart($idi, $kn); next; }
        if ( $act eq 'e' )   { set_messtyp_einheit($idi, $kn); next; }
        if ( $act eq 's' )   { set_messtyp_status($idi, $kn);  next; }
        if ( $act eq 'i' )   { set_messtyp_info($idi, $kn);    next; }
        if ( $act eq 'H' )   { log_view $kn, mess_typ => id => $idi; next; }
        if ( $act =~ /^(\d+)$/ ) {
            if ( my ($id, $mt) = DoFn "SELECT id, typ FROM mess_stelle WHERE id = $1" ) {
               print <<END if $idi != $mt;

*** Vorsicht: Du bearbeitest eine Messtelle die den Messtyp #$mt hat!
              Du bist aber gerade in Messtyp #$idi drin!

END

                edit_messstelle $id, $kn; 
            }
            else {
                print "Mess-Stelle #$1 nicht gefunden.\n";
            }
            next;
        }
        print "Aktion '$act' unbekannt.\n";
        next;
        report_fehler(4);
    }
    $idi;
}

1;
