#libraries ----
library("leaflet")
library("readxl")
library("janitor")
library("dplyr")
library("tidyr")
library("tidygeocoder")
library("sf")

# choose columns ---
cols <- c(
  "account_number",
  "owner_1",
  "owner_2",
  "subdivision", 
  "situs_address_property_address",
  "situs_city",
  "situs_state",
  "situs_zip_code",
  "homestead_exemption_yes_or_no"
  )

# filter to St. Andrews (388) ----
df <- 
  readxl::read_xlsx(
    path = "./data-raw/property/SCPA Public.xlsx", 
    n_max = Inf, 
    .name_repair = ~janitor::make_clean_names(.x)
  ) %>% 
  filter(
    subdivision %in%  c("8120", "8113", "8171", "8195", "8221",
       "8163", "8240", "8159", "8149", "8110", "8254", "8215", "8143")
  ) %>% 
  #select(cols) %>% 
  rename(
    situs_address = situs_address_property_address,
    homestead = homestead_exemption_yes_or_no
  ) %>% 
  filter(!grepl("^0", situs_address)) %>%
  drop_na(situs_address)

# create location ----
df1 <-
  df %>% 
  separate(
    situs_address, 
    into = c("street_no", "street", "suffix", "unit", "bldg", "no"), 
    sep = "\\s+"
  ) %>% 
  mutate(unit = ifelse(is.na(bldg), NA, unit)) %>% 
  mutate(location = paste(
    street_no, street, suffix, ",",
    situs_city, situs_state, situs_zip_code, 
    sep = " ")) %>%
  mutate(location = gsub(" ,", ",", location)) %>%
  mutate(label = paste(street_no, street, suffix, sep = " ")) %>%
  arrange(account_number)

# geotag unique location (205) ----
if(!file.exists("./data-raw/geotagged_street_addresses.rds")) {
  addresses <- df1 %>% select(location) %>% distinct()
  lat_longs <- addresses %>%
    geocode(
      location, 
      method = 'google', 
      lat = lat, 
      long = lng
    )
  saveRDS(lat_longs, "./data-raw/geotagged_street_addresses.rds")
} else {
  lat_longs <- readRDS("./data-raw/geotagged_street_addresses.rds")
}

# append coords ----
df2 <-
  df1 %>% 
  left_join(lat_longs[, !names(lat_longs) %in% "account_number"], 
            by = c("location" = "location"), 
            relationship = "many-to-many") %>% 
  distinct() %>% 
  st_as_sf(coords = c("lng", "lat"), crs = 4326)


# write as esri shp file for qgis ----
sf::st_write(
  df2, 
  "./data-raw/addresses/owners_raw.gpkg",
  driver = "GPKG", 
  delete_dsn = TRUE
)

#####
# adjust points in QGIS
####

# after QGIS, shp --> rds ----
owners_moved <- sf::st_read(
  "./data-raw/addresses/owners_moved.gpkg",
  layer = "owners_raw")
owners_moved %>% 
  leaflet() %>% 
  addTiles() %>% 
  addMarkers()
saveRDS(owners_moved, "./data/owners.rds")

# add plats ----
plats <- sf::st_read("./data/plats/plats.shp")
owners <- readRDS("./data/owners.rds")
# display ----
library("leafpop")
m <- 
  leaflet() %>% 
  addTiles() %>%
  addPolygons(
    data = plats,
    color = "red",
    weight = 2,
    opacity = 0.5,
    fillOpacity = 0.2,
    label = ~sub_name,
    group = "Subdivisions"
  ) %>% 
  addMarkers(
    data = owners,
    lat = ~lat,
    lng = ~lng,
    popup = popupTable(
      owners, 
      zcol = c(
        "account_number",
        "location",
        "owner_1",
        "owner_2"
      )
    ),
    group = "Owners"
  ) %>% 
  addLayersControl(
    overlayGroups = c("Subdivisions", "Owners"), # Specify overlay groups
    options = layersControlOptions(collapsed = FALSE) # Control options
  )
m 
# add condo layer


