# -------------------------------
# 1. Load Required Libraries
# -------------------------------
library(bangladesh)
## Warning: package 'bangladesh' was built under R version 4.5.2
library(sf)
## Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(leaflet)

# -------------------------------
# 2. Load Union Level Map
# -------------------------------
dhaka_union_map <- map_union 

# -------------------------------
# 3. Filter Only Dhaka District
# -------------------------------
dhaka_unions <- dhaka_union_map %>%
  filter(District == "Dhaka") %>%
  rename(
    Union_Name = Union,
    Upazila_Name = Upazila
  )

# -------------------------------
# 4. Mosquito Data Table (Your Data)
# -------------------------------
mosquito_data <- data.frame(
  Area = c("Gulshan", "Mirpur", "Tejgaon", "Cantonment", "Khilkhet", "Turag"),
  Total_Mosquito = c(132, 210, 310, 97, 123, 321),
  Aedes_Found = c(20, 26, 27, 13, 14, 23),
  Kit_Used = c(3, 4, 4, 2, 2, 4),
  Kit_Positive = c(1, 1, 1, 1, 1,1 )
)

# -------------------------------
# 5. Join Mosquito Data with Map
# -------------------------------
dhaka_unions <- dhaka_unions %>%
  left_join(mosquito_data, by = c("Upazila_Name" = "Area"))

# -------------------------------
# 6. Define RED Areas
# -------------------------------
red_areas <- mosquito_data$Area

# -------------------------------
# 7. Original Color Palette
# -------------------------------
color_pal <- colorFactor(
  palette = "Set3",
  domain = dhaka_unions$Upazila_Name
)

# -------------------------------
# 8. Final Color Logic
# -------------------------------
dhaka_unions <- dhaka_unions %>%
  mutate(
    final_fill = ifelse(
      Upazila_Name %in% red_areas,
      "red",
      color_pal(Upazila_Name)
    )
  )
## Warning: There was 1 warning in `stopifnot()`.
## ℹ In argument: `final_fill = ifelse(Upazila_Name %in% red_areas, "red",
##   color_pal(Upazila_Name))`.
## Caused by warning in `RColorBrewer::brewer.pal()`:
## ! n too large, allowed maximum for palette Set3 is 12
## Returning the palette you asked for with that many colors
# -------------------------------
# 9. Popup Logic (Different for RED & Others)
# -------------------------------
dhaka_unions <- dhaka_unions %>%
  mutate(
    popup_text = ifelse(
      Upazila_Name %in% red_areas,
      
      # 🔴 RED AREA POPUP WITH MOSQUITO DATA
      paste0(
        "<b>Area:</b> ", Upazila_Name, "<br>",
        "<b>Total Mosquito:</b> ", Total_Mosquito, "<br>",
        "<b>Aedes Found:</b> ", Aedes_Found, "<br>",
        "<b>Kit Used:</b> ", Kit_Used, "<br>",
        "<b>Positive Kit:</b> ", Kit_Positive
      ),
      
      # 🟢 NORMAL AREA POPUP
      paste0(
        "<b>Union:</b> ", Union_Name, "<br>",
        "<b>Upazila/Thana:</b> ", Upazila_Name
      )
    )
  )

# -------------------------------
# 10. Create Leaflet Map
# -------------------------------
interactive_union_map <- leaflet(dhaka_unions) %>%
  addTiles() %>%
  
  setView(lng = 90.40, lat = 23.83, zoom = 11) %>%
  
  addPolygons(
    fillColor = ~final_fill,
    weight = 1,
    opacity = 1,
    color = "black",
    dashArray = "1",
    fillOpacity = 0.8,
    
    popup = ~popup_text,
    
    highlightOptions = highlightOptions(
      weight = 3,
      color = "yellow",
      fillOpacity = 0.95,
      bringToFront = TRUE
    )
  ) %>%
  
  # -------------------------------
# 11. Legends
# -------------------------------
addLegend(
  pal = color_pal,
  values = ~Upazila_Name,
  title = "Parent Upazila/Thana"
) %>%
  
  addLegend(
    position = "bottomright",
    colors = c("red"),
    labels = c("Mosquito Survey Areas"),
    title = "Highlight",
    opacity = 1
  )
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Set3 is 12
## Returning the palette you asked for with that many colors
# -------------------------------
# 12. Display Map
# -------------------------------
interactive_union_map