# title:  Thruk Panorama Dashboards
# user:   thrukadmin
# groups: [{"*" : "read-only"}]

# do not show more that this number
my $maximum_number = 200;

$dashboard = load_data();
$dashboard->{'ts'} = [stat($c->config->{etc_path}.'/panorama/.')]->[9];

my $own    = Thruk::Utils::Panorama::get_dashboard_list($c, 'my');
my $public = Thruk::Utils::Panorama::get_dashboard_list($c, 'public');
my $total  = scalar @{$own} + scalar @{$public};

my $_add_panlet = sub {
    my($x, $y, $d) = @_;
    return unless($d && $d->{'nr'});
    return unless $d->{'name'};
    if($d->{'ts'} && $dashboard->{'ts'} < $d->{'ts'}) {
        $dashboard->{'ts'} = $d->{'ts'};
    }
    my $name = $d->{'name'};
    $name =~  s/['"]//gmx;
    return({
        "xdata" => {
            "appearance" => {
                "iconset" => "default_64",
                "type" => "icon"
            },
            "cls" => $total > 20 ? "TP.StaticIcon" : "TP.DashboardStatusIcon",
            "general" => {
                "dashboard" => $d->{'nr'},
                "src" => "../usercontent/images/status/default_64/ok.png"
            },
            "label" => {
                "bgcolor" => "#cccccc",
                "bordercolor" => "#aaaaaa",
                "bordersize" => 1,
                "fontsize" => 9,
                "labeltext" => "<center title='".$name."'>".$name."</center>",
                "offsety" => 42,
                "position" => "center",
                "width" => "64"
            },
            "layout" => {
                "x" => $x,
                "y" => $y,
            },
            "link" => {
                "link" => "dashboard://".$d->{'nr'},
                "newtab" => "on"
            },
            "nsize" => [ 64, 64 ],
            "size" => 64
        }
    });
};

my $_add_label = sub {
    my($x, $y, $text) = @_;
    return({
        "xdata" => {
            "cls" => "TP.TextLabelWidget",
            "label" => {
                "fontbold" => "1",
                "fontsize" => 18,
                "labeltext" => $text,
                "position" => "top-left",
            },
            "layout" => {
                "x" => $x,
                "y" => $y,
            }
        }
    });
};


my $nr = 2;
$dashboard->{'panlet_'.++$nr} = &{$_add_label}(20, 40, "Create New Dashboard:");
$dashboard->{'panlet_'.++$nr} = &{$_add_label}(500, 40, "Load Dashboard:");

# fetch my maps
my $x  = 20;
my $y  = 210;
my $shown = 0;
if(scalar @{$own} > 0) {
    $dashboard->{'panlet_'.++$nr} = &{$_add_label}(20, 190, "Open Existing Dashboards:");
    for my $d (@{$own}) {
       last if $shown >= $maximum_number;
        next if $d->{'nr'} eq $dashboard->{'nr'}; # avoid recursion
        if($x > 650) {
            $x = 20;
            $y = $y + 90;
        }
        $dashboard->{'panlet_'.++$nr} = &{$_add_panlet}($x, $y, $d);
        $x = $x + 70;
        $shown++;
    }
    $y = $y + 130;
}

# shared maps
$dashboard->{'panlet_'.++$nr} = &{$_add_label}(20, $y, "Shared Dashboards:");
$y = $y + 20;
$x = 20;
for my $d (@{$public}) {
    last if $shown >= $maximum_number;
    next if $d->{'nr'} eq $dashboard->{'nr'}; # avoid recursion
    if($x > 800) {
        $x = 20;
        $y = $y + 90;
    }
    $dashboard->{'panlet_'.++$nr} = &{$_add_panlet}($x, $y, $d);
    $shown++;
    $x = $x + 70;
}

__DATA__
{
   "ts" : 0,
   "tab" : {
      "xdata" : {
         "refresh" : 120,
         "background_color" : "#EEE",
         "hide_tab_header" : 1,
      }
   },
   "panlet_0" : {
      "xdata" : {
         "appearance" : {
            "type" : "icon"
         },
         "cls" : "TP.StaticIcon",
         "general" : {
            "src" : "../usercontent/images/status/default_64/ok.png"
         },
         "label" : {
            "bgcolor" : "#cccccc",
            "bordercolor" : "#aaaaaa",
            "bordersize" : 1,
            "fontsize" : 10,
            "labeltext" : "<center>Create New</center>",
            "offsety" : 42,
            "position" : "center",
            "width" : "60"
         },
         "layout" : {
            "x" : 20,
            "y" : 60,
         },
         "link" : {
            "link" : "javascript:TP.add_pantab({ id: 'new' })"
         },
        "nsize" : [ 64, 64 ],
        "size" : 64
      }
   },
   "panlet_1" : {
      "xdata" : {
         "appearance" : {
            "type" : "icon"
         },
         "cls" : "TP.StaticIcon",
         "general" : {
            "src" : "../plugins/panorama/images/map_64.png"
         },
         "label" : {
            "bgcolor" : "#cccccc",
            "bordercolor" : "#aaaaaa",
            "bordersize" : 1,
            "fontsize" : 10,
            "labeltext" : "<center>Geomap</center>",
            "offsety" : 42,
            "position" : "center",
            "width" : "60"
         },
         "layout" : {
            "x" : 90,
            "y" : 60,
         },
         "link" : {
            "link" : "javascript:TP.add_pantab({ id: 'new_geo' })"
         },
        "nsize" : [ 64, 64 ],
        "size" : 64
      }
   },
   "panlet_2" : {
      "xdata" : {
         "appearance" : {
            "type" : "icon"
         },
         "cls" : "TP.StaticIcon",
         "general" : {
            "src" : "../plugins/panorama/images/folder_picture_64.png"
         },
         "label" : {
            "bgcolor" : "#cccccc",
            "bordercolor" : "#aaaaaa",
            "bordersize" : 1,
            "fontsize" : 10,
            "labeltext" : "<center>Load Dashboard</center>",
            "offsety" : 42,
            "position" : "center",
            "width" : "80"
         },
         "layout" : {
            "x" : 500,
            "y" : 60,
         },
         "link" : {
            "link" : "javascript:TP.loadDashboardWindow()"
         },
        "nsize" : [ 64, 64 ],
        "size" : 64
      }
   }
}
