library(readODS)
library(magrittr)
library(dplyr)
library(tidyr)
#library(osmdata)
library(tidywikidatar)

##############################
## Arventennoù
# Cheñch teuliad
source_path <- rstudioapi::getActiveDocumentContext()$path
setwd(dirname(dirname(source_path)))

# Anv ar c'holonennoù, hervez an niver a dud aterset
anv_kolon <- function(n, gant_lechierezh=TRUE, gant_evezhiou=TRUE) {
  anviou <- c("anv_latin")
  anviou <- c(anviou, paste0("anv_bzg_", seq(1,n)))
  anviou <- c(anviou, "anv_glg")
  if (gant_lechierezh) {
    anviou <- c(anviou, "lechierezh")
  }
  if (gant_evezhiou) {
    anviou <- c(anviou, "evezhiou")
  }
  anviou <- c(anviou, "strollad")
  return(anviou)
}

# WikiData
tw_enable_cache()
tw_set_cache_folder(path = fs::path(getwd(), "tmp/tw_data"))
tw_set_language(language = "br")
tw_create_cache_folder(ask = FALSE)

## Renabl eus ar restroù
# Brieg
brieg1 <- read_ods("./data/Brieg.ods", sheet=1, range="A1:G44") %>%
  as_tibble() %>%
  mutate(strollad=1)
names(brieg1) <- anv_kolon(3)
brieg2 <- read_ods("./data/Brieg.ods", sheet=2, range="A1:E23") %>%
  as_tibble() %>%
  mutate(strollad=2)
names(brieg2) <- anv_kolon(1)
gps_brieg <- tw_search(search="Brieg") %>%
  tw_filter_first(p="P31", q="Q484170") %>%
  tw_get_property(p="P625") %>%
  mutate(kumun="Brieg", led=as.numeric(strsplit(value, ",")[[1]][1]), hed=as.numeric(strsplit(value, ",")[[1]][2])) %>%
  select(kumun, led, hed)
brieg = bind_rows(brieg1, brieg2) %>%
  bind_cols(., gps_brieg)


# Louergad
louergad1 <- read_ods("./data/Louergad (Seubil).ods", sheet=1, range="A1:E44") %>%
  as_tibble() %>%
  mutate(strollad=1)
names(louergad1) <- anv_kolon(2, gant_lechierezh=FALSE)
louergad2 <- read_ods("./data/Louergad (Seubil).ods", sheet=2, range="A1:D23") %>%
  as_tibble() %>%
  mutate(strollad=2)
names(louergad2) <- anv_kolon(2, gant_lechierezh=FALSE, gant_evezhiou=FALSE)
gps_louergad <- tw_search(search="Louergad") %>%
  tw_filter_first(p="P31", q="Q484170") %>%
  tw_get_property(p="P625") %>%
  mutate(kumun="Louergad", led=as.numeric(strsplit(value, ",")[[1]][1]), hed=as.numeric(strsplit(value, ",")[[1]][2])) %>%
  select(kumun, led, hed)
louergad = bind_rows(louergad1, louergad2) %>%
  bind_cols(., gps_louergad)


# Ploneour Lanwern
ploneour_lanwern1 <- read_ods("./data/Ploneour Lanwern.ods", sheet=1, range="A1:E44") %>%
  as_tibble() %>%
  mutate(strollad=1)
names(ploneour_lanwern1) <- anv_kolon(1)
ploneour_lanwern2 <- read_ods("./data/Ploneour Lanwern.ods", sheet=2, range="A1:E23") %>%
  as_tibble() %>%
  mutate(strollad=2)
names(ploneour_lanwern2) <- anv_kolon(1)
gps_ploneour_lanwern <- tw_search(search="Ploneour-Lanwern") %>%
  tw_filter_first(p="P31", q="Q484170") %>%
  tw_get_property(p="P625") %>%
  mutate(kumun="Ploneour-Lanwern", led=as.numeric(strsplit(value, ",")[[1]][1]), hed=as.numeric(strsplit(value, ",")[[1]][2])) %>%
  select(kumun, led, hed)
ploneour_lanwern = bind_rows(ploneour_lanwern1, ploneour_lanwern2) %>%
  bind_cols(., gps_ploneour_lanwern)


data = bind_rows(brieg, 
                 louergad,
                 ploneour_lanwern)