---
title: "FIS-Broker Datasets"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{FIS-Broker Datasets}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

is_ghactions <- identical(Sys.getenv("CI"), "true") && nchar(Sys.getenv("GH_ACTIONS_KWB_R")) > 0

```

## Statistics

### By Type

```{r datasets_stats_type}
library(kwb.fisbroker)

overview <- kwb.fisbroker::get_dataset_overview() %>% 
  dplyr::filter(.data$cmd == "navigationShowService") 

overview_atom <- overview[overview$type == "ATOM",]
overview_wfs <- overview[overview$type == "WFS",]
overview_wms <- overview[overview$type == "WMS",]

overview %>%  
  dplyr::count(.data$type) %>% 
  dplyr::arrange(dplyr::desc(.data$n)) %>% 
  DT::datatable()
```

### By Type and Category

```{r datasets_stats_typecategory}
overview %>%  
  dplyr::count(
    .data$type,
    .data$category_name
  ) %>% 
  dplyr::arrange(dplyr::desc(.data$n)) %>% 
  DT::datatable()
```


## Types 

### ATOM

```{r datasets_atom}
overview_atom %>%
  dplyr::select(
    .data$dataset_id,
    .data$dataset_name,
    .data$type,
    .data$identifier
  ) %>% 
  DT::datatable()
```  

### WFS

```{r datasets_wfs}
overview_wfs %>%
  dplyr::select(
    .data$dataset_id,
    .data$dataset_name,
    .data$type,
    .data$identifier
  ) %>% 
  DT::datatable()
```  

### WMS

```{r datasets_wms}
overview_wms %>%
  dplyr::select(
    .data$dataset_id,
    .data$dataset_name,
    .data$type,
    .data$identifier
  ) %>% 
  DT::datatable()
```  


## Metadata


### Fetch 

```{r fisbroker_metadata_fetch, eval = is_ghactions}

## Only run on KWB-R Github Actions
metadata_atom <- kwb.fisbroker::read_all_metadata(overview_atom, dbg = FALSE)
metadata_wfs <- kwb.fisbroker::read_all_metadata(overview_wfs, dbg = FALSE)
metadata_wms <- kwb.fisbroker::read_all_metadata(overview_wms, dbg = FALSE)
```

### Export

```{r fisbroker_metadata_export, eval = is_ghactions}
## Only run on KWB-R Github Actions
kwb.fisbroker::write_to("csv", metadata_atom)
kwb.fisbroker::write_to("json", metadata_atom)

kwb.fisbroker::write_to("csv", metadata_wfs)
kwb.fisbroker::write_to("json", metadata_wfs)


kwb.fisbroker::write_to("csv", metadata_wms)
kwb.fisbroker::write_to("json", metadata_wms)
```


```{r download, echo = FALSE, results ='asis', eval = is_ghactions}
files_csv <- list.files(pattern = "^metadata_.*\\.csv$")
files_json <- list.files(pattern = "^metadata_.*\\.json$")


download_csv <- sprintf("CSV:\n\n%s", paste0(
   sprintf("- [%s](https://kwb-r.github.io/kwb.fisbroker/%s)", files_csv, files_csv),
   collapse = "\n\n"))

download_json <- sprintf("JSON:\n\n%s", paste0(
   sprintf("- [%s](https://kwb-r.github.io/kwb.fisbroker/%s)", files_json, files_json),
   collapse = "\n\n"))

cat(sprintf("### Download\n\n%s\n\n%s", download_csv, download_json))
```

## Details

How do get `dataset_id` for function `kwb.fisbroker::read_wfs()` ?


```{r details_1, eval = is_ghactions}
overview <- kwb.fisbroker::get_dataset_overview() 

kwb.fisbroker:::lookup_dataset_id(
  overview = overview, 
  dataset_id = 3L, 
  type = "WMS"
)
```


What happens within `get_dataset_overview()`?

```{r details_2, eval = is_ghactions}
library(kwb.fisbroker)

response <- login_to_fis_broker() %>%
  compose_fis_broker_url(cmd = "navigationFrameResult") %>%
  httr_get_or_fail()

#open_response(response)

html_tree <- response %>%
  httr::content(as = "text") %>%
  rvest::read_html()

# To be integrated in kwb.fisbroker::get_dataset_overview()...
overview <- kwb.fisbroker:::extract_overview_table(html_tree)

#View(overview)
```
