# setup -------------------------------------------------------------------
library(tidyverse)
library(stringr)
library(broom)
library(highcharter)
library(ggrepel)
library(viridis)
library(jbkmisc)
library(hrbrthemes)
theme_set(theme_jbk())


# data --------------------------------------------------------------------
data <- read_csv("../matrices-oea/all.csv")

data <- data %>% 
  gather(origin, qty, -Destination, -periodo) %>% 
  rename(destination = Destination, year = periodo) %>% 
  filter(!is.na(qty))

data <- data %>% 
  mutate_if(is.character, str_replace_all, "\\(.*\\)", "") %>% 
  mutate_if(is.character, str_trim)
        
# destination -------------------------------------------------------------
df_dest <- data %>% 
  group_by(destination, year) %>% 
  summarise(qty = sum(qty)) %>% 
  arrange(destination, year) %>% 
  mutate(qty_std = qty/first(qty),
         year0 = year - 1990)

ggplot(df_dest, aes(year, qty_std)) +
  geom_line(aes(group = destination, color = destination),
            size = 1.2, alpha = 0.5) +
  geom_text_repel(aes(label = destination),
                  color = "gray60", size = 3,
                  nudge_x = 10, force = 1,
                  data = filter(df_dest, year == 2015)) + 
  scale_color_viridis(discrete = TRUE) + 
  scale_x_continuous(limits = c(NA, 2020)) + 
  theme(legend.position = "none") +
  labs(
    title = "Exploratory Analysis Inmigration"
  )

df_dest_country <- nest(df_dest, -destination)

do_model <- function(df)  lm(qty_std ~ year0, data = df)

models_dest <- df_dest_country %>% 
  mutate(
    model = map(data, do_model),
    glance = map(model, glance),
    rsq = map_dbl(glance, "r.squared"),
    tidy = map(model, tidy),
    augment = map(model, augment)
    )

models_dest %>%
  unnest(tidy) %>% 
  select(destination, rsq, term, estimate) %>% 
  spread(term, estimate) %>% 
  ggplot(aes(`(Intercept)`, year0)) + 
  geom_point()

# origin ------------------------------------------------------------------
df_orig <- data %>% 
  group_by(origin, year) %>% 
  summarise(qty = sum(qty)) %>% 
  arrange(origin, year) %>% 
  mutate(qty_std = qty/first(qty),
         year0 = year - 1990)


ggplot(df_orig, aes(year, qty_std)) +
  geom_line(aes(group = origin, color = origin),
            size = 1.2, alpha = 0.5) +
  geom_text_repel(aes(label = origin),
                  color = "gray60", size = 3,
                  nudge_x = 10, force = 1,
                  data = filter(df_orig, year == 2015)) + 
  scale_color_viridis(discrete = TRUE) + 
  scale_x_continuous(limits = c(NA, 2020)) + 
  theme(legend.position = "none") +
  labs(
    title = "Exploratory Analysis for Migration"
  )

df_orig_country <- nest(df_orig, -origin)

models_orig <- df_orig_country %>% 
  mutate(
    model = map(data, do_model),
    glance = map(model, glance),
    rsq = map_dbl(glance, "r.squared"),
    tidy = map(model, tidy),
    augment = map(model, augment)
  )

models_orig %>%
  unnest(tidy) %>% 
  select(origin, rsq, term, estimate) %>% 
  spread(term, estimate) %>% 
  ggplot(aes(`(Intercept)`, year0)) + 
  geom_point()

# join --------------------------------------------------------------------
models_orig2 <- models_orig %>%
  unnest(tidy) %>% 
  select(origin, term, estimate) %>% 
  spread(term, estimate)

models_dest2 <- models_dest %>%
  unnest(tidy) %>% 
  select(destination, term, estimate) %>% 
  spread(term, estimate)

models <- left_join(models_dest2, models_orig2, by = c("destination" = "origin"))
  

ggplot(models, aes(year0.x, year0.y)) +
  geom_point(color = "royalblue", size = 1.5) + 
  geom_text_repel(aes(label = destination), color = "gray30", size = 3) + 
  coord_fixed() +
  geom_vline(xintercept = 0, color = "gray60") + 
  geom_hline(yintercept = 0, color = "gray60") + 
  geom_abline(aes(slope = 1, intercept = 0), color = "gray60") + 
  scale_x_percent(limits = c(-0.05, 0.20)) + 
  scale_y_percent(limits = c(-0, 0.15)) + 
  theme_jbk() +
  labs(
    x = "Tendencia Inmigración*",
    y = "Tendencia Migración*",
    caption = "* Tasa de crecimiento de acuerdo a regresión lineal"
  )

modelsf <- models %>% 
  rename(pais = destination, x = year0.x, y = year0.y) %>% 
  select(pais, x, y) %>% 
  left_join(
    models_orig %>% select(origin, data) %>% rename(pais = origin, orig = data)    
  ) %>% 
  left_join(
    models_dest %>% select(destination, data) %>% rename(pais = destination, dest = data)
  ) %>% 
  mutate(
    orig = map(orig, "qty"),
    dest = map(dest, "qty")
  )
  

modelsf
## # A tibble: 35 x 5
##                   pais           x           y      orig      dest
##                  <chr>       <dbl>       <dbl>    <list>    <list>
##  1 Antigua and Barbuda 0.065338734 0.047913047 <dbl [6]> <dbl [6]>
##  2           Argentina 0.042623675 0.029433725 <dbl [6]> <dbl [6]>
##  3             Bahamas 0.063110428 0.020232016 <dbl [6]> <dbl [6]>
##  4            Barbados 0.004361434 0.008297509 <dbl [6]> <dbl [6]>
##  5              Belize 0.029658758 0.026548648 <dbl [6]> <dbl [6]>
##  6             Bolivia 0.031536471 0.075082670 <dbl [6]> <dbl [6]>
##  7              Brazil 0.030369309 0.046807995 <dbl [6]> <dbl [6]>
##  8              Canada 0.035353155 0.005769499 <dbl [6]> <dbl [6]>
##  9               Chile 0.186127295 0.002686614 <dbl [6]> <dbl [6]>
## 10            Colombia 0.014039708 0.051529150 <dbl [6]> <dbl [6]>
## # ... with 25 more rows
library(highcharter)

hchart(modelsf, "bubble", hcaes(x, y, name = pais), color = "skyblue", maxSize = "3%") %>%
  hc_plotOptions(
    series = list(
      dataLabels = list(enabled = TRUE, format = "{point.name}")
    )
  ) %>% 
  hc_add_theme(hc_theme_smpl()) %>% 
  hc_xAxis(title = "Crecimiento Inmigración") %>% 
  hc_yAxis(title = "Crecimiento Migración") %>% 
  hc_tooltip(
    useHTML = TRUE,
    positioner = JS("function () { return { x: 900, y: 25 }; }"),
    headerFormat = "{point.pais}",
    pointFormatter = JS("
function(){
  
  var thiz = this;
  console.log(thiz);

  setTimeout(function() {
          $('#minichart').highcharts({
            title : {
              text: ''
            },
            subtitle: {
                text: thiz.pais,
              align: 'left'
            },
            tooltip : {enabled: true},
            exporting: {
              enabled: false
            },
            legend: {
                enabled: true
            },
            credits: {
                enabled: false
            },
            series: [{
              animation: false,
              color: 'blue',
              name: 'Migracion',
              data: thiz.orig
            },{
              animation: false,
              color: 'red',
              name: 'Inmigracion',
              data: thiz.dest
            }],
            yAxis: {
              title: ''
            },
            xAxis: {
                title: '',
                categories: [1990, 1995, 2000, 2005, 2010, 2015]
            }
          });
        }, 0);
        return '<div id=\"minichart\" style=\"width: 300px; height: 200px;\"></div>';
}                        
                        ")
  )