# Scratch file
library(janitor)
library(dplyr)
#library(ggmap)
library(photon)
library(readr)
source("osm_api.R")
Geo2017 <- clean_names(read_csv("data/AllDHBsGeocoded.csv"), case = "snake") %>%
    select(-facility_opening_date) %>%
    mutate(dhb_name = ifelse(is.na(dhb_name), "Missing", dhb_name),
           health_facility_code = ifelse(is.na(health_facility_code),
                                         "Missing", health_facility_code))
    
library(readxl)
newFacil <- clean_names(read_excel("data/facilities20190902.xlsx"), 
                        case = "snake") %>%
    filter(facility_type_name == "GP Practice") %>% 
    select(-facility_closing_date, -facility_opening_date) %>%
    mutate(dhb_name = ifelse(is.na(dhb_name), "Missing", dhb_name),
           health_facility_code = ifelse(is.na(health_facility_code),
                                         "Missing", health_facility_code))
    
DHBs <- unique(c(newFacil$dhb_name, Geo2017$dhb_name))
unique(newFacil$dhb_name) %in% unique(Geo2017$dhb_name)

DHBs[!(DHBs %in% newFacil$dhb_name)]
DHBs[!(DHBs %in% Geo2017$dhb_name)]
nrow(newFacil)

nrow(Geo2017)

colSums(is.na(newFacil))
colSums(is.na(Geo2017))

gp_prac <- left_join(newFacil, Geo2017, by = c("name", "health_facility_code", 
                                               "hpi_facility_id",
                                               "address", "dhb_name",
                                               "fac_type", "facility_type_name"))
colSums(is.na(gp_prac))

gp_same <- filter(gp_prac, !is.na(gp_prac$calculated_address))
gp_new <- filter(gp_prac, is.na(gp_prac$calculated_address)) %>%
    select(-lon, -lat, -calculated_address)

table(newFacil$facility_type_name)
table(Geo2017$facility_type_name)

# newcoded <- osm_geocode("21 Ruakura Road, Hamilton East, Hamilton 3216")

newcoded <- geocode(gp_new$address, limit = 1)
