use utf8;
use warnings; no warnings "redefine";
use strict;
use Dbase::Help qw(DoFn Do qquote);
use Dbase::Globals qw(get_descr);
use Fehler qw(problem warnung);
use Loader qw(
  log_update
  select_descr
  set_messtyp_collectd_typ
  set_messtyp_rrd_faktor
  set_messtyp_snmp_faktor
);


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

    defined(
        my $rrd_typ = select_descr
          rrd_typ => 1024 | 2048,
        "l - Liste\n", 'RRD Typ', '!hide'
    ) or return;
    $rrd_typ = qquote $rrd_typ;
    return $rrd_typ unless defined $idi;

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

    # Von NICHT COUNTER auf COUNTER
    if ( $orrd_typ != 1 and $rrd_typ eq "'1'" ) {

        # dann wird collectd_typ automagisch zu work
        warnung 'Da der RRD-Typ auf ' . get_descr( "rrd_typ", 1 )
          . " gesetzt wurde, wird der collectd_typ auf 'work' gesetzt";
        set_messtyp_collectd_typ($idi, $kn, "'work'");
    }

    # Von COUNTER auf NICHT COUNTER
    else {

        my ($ocollectd_typ) = DoFn("SELECT collectd_typ FROM mess_typ WHERE id = $idi");
        warnung 'Collectd Typ darf nicht auf "work" sein, wenn der RRD Typ nicht auf '
              . get_descr( "rrd_typ", 1 ) . ' gesetzt ist!';

        # darf er alles ausser work eingeben
        my $collectd_typ = set_messtyp_collectd_typ( undef, $kn );
        if (   (  defined $collectd_typ and $collectd_typ eq "'work'" )
            or ( !defined $collectd_typ and $ocollectd_typ eq 'work'  ) )
        {
            return problem
              'Collectd Typ darf nicht auf "work" sein, wenn der RRD Typ nicht auf '
              . get_descr( "rrd_typ", 1 ) . ' gesetzt ist!';
        }
        else {
            set_messtyp_collectd_typ( $idi, $kn, $collectd_typ );
        }
    }

    if ( $rrd_typ eq "'1'" ) {
        warnung 'Da der RRD-Typ auf '
          . get_descr( "rrd_typ", 1 )
          . ' gesetzt wurde, wird der SNMP-Faktor auf 1 gesetzt!';
        set_messtyp_snmp_faktor($idi, $kn, '1');
    }
    else {
        warnung 'Da der RRD-Typ auf nicht '
          . get_descr( "rrd_typ", 1 )
          . ' gesetzt wurde, wird der RRD-Faktor auf 1 gesetzt!';
        set_messtyp_rrd_faktor($idi, $kn, '1');
    }

    log_update mess_typ =>
      id                => $idi,
      undef, 'rrd_typ', undef, get_descr( rrd_typ => $orrd_typ, 1 );
    Do "UPDATE mess_typ SET rrd_typ = $rrd_typ WHERE id = $idi";

}

1;
