library(tidyverse)

game.data <- read_csv("../input/game_data.csv")
play.info <- read_csv("../input/play_information.csv")
play.player.role <- read_csv("../input/play_player_role_data.csv")
player.punt.data <- read_csv("../input/player_punt_data.csv")

video.footage.control <- read_csv("../input/video_footage-control.csv")
video.footage.injury <- read_csv("../input/video_footage-injury.csv")
video.review <- read_csv("../input/video_review.csv")

# video.review fix
video.review <- video.review %>%  mutate(Primary_Partner_GSISID = as.integer(replace(Primary_Partner_GSISID, Primary_Partner_GSISID == "Unclear", NA)))

# get clean injury df
injuries <- video.review %>% 
    select(Season_Year, GameKey, PlayID, GSISID, 
           Primary_Partner_GSISID, Player_Activity_Derived, 
           Primary_Partner_Activity_Derived, Primary_Impact_Type, 
           Friendly_Fire)

# get role primary
injuries <- injuries %>% left_join(play.player.role, 
                                   by = c("Season_Year", "GameKey", "PlayID", 
                                          "GSISID"), 
                                   suffix = c("","_Player"))

# get role partner
injuries <- injuries %>% left_join(play.player.role, 
                                   by = c("Season_Year", "GameKey", "PlayID", 
                                          "Primary_Partner_GSISID" = "GSISID"),
                                   suffix = c("","_Primary_Partner"))

# replace common names
injuries <- injuries %>% mutate(Role = replace(Role, Role %in% c("GR", "GL"),
                                               "G"))
injuries <- injuries %>% mutate(Role_Primary_Partner = replace(
    Role_Primary_Partner, Role_Primary_Partner %in% c("GR", "GL"), "G"))

injuries <- injuries %>% mutate(Role = replace(Role, Role %in% c("PDR1", 
                                                                 "PDR2",
                                                                 "PDR3",
                                                                 "PDL1",
                                                                 "PDL2",
                                                                 "PDL3"), 
                                               "PD"))

injuries <- injuries %>% mutate(Role_Primary_Partner
                                = replace(Role_Primary_Partner, 
                                          Role_Primary_Partner %in% c("PDR1", 
                                                                      "PDR2",
                                                                      "PDR3",
                                                                      "PDL1",
                                                                      "PDL2",
                                                                      "PDL3"), 
                                          "PD"))

injuries <- injuries %>% mutate(Role = replace(Role, Role %in% c("PLG", "PRG"),
                                               "PG"))

injuries <- injuries %>% mutate(Role_Primary_Partner
                                = replace(Role_Primary_Partner, 
                                          Role_Primary_Partner 

                                          %in% c("PLG", "PRG"), "PG"))

injuries <- injuries %>% mutate(Role = replace(Role, Role %in% c("PLT", "PRT"),
                                               "PT"))

injuries <- injuries %>% mutate(Role_Primary_Partner
                                = replace(Role_Primary_Partner, 
                                          Role_Primary_Partner 
                                          %in% c("PLT", "PRT"), "PT"))

injuries <- injuries %>% mutate(Role = replace(Role, Role %in% c("PLW", "PRW"),
                                               "PW"))

injuries <- injuries %>% mutate(Role_Primary_Partner
                                = replace(Role_Primary_Partner, 
                                          Role_Primary_Partner 
                                          %in% c("PLW", "PRW"), "PW"))

injuries <- injuries %>% mutate(Role = replace(Role, 
                                               Role %in% c("PLL", "PLR", "PLM"),
                                               "PL"))

injuries <- injuries %>% mutate(Role_Primary_Partner
                                = replace(Role_Primary_Partner, 
                                          Role_Primary_Partner 
                                          %in% c("PLL", "PLR", "PLM"), "PL"))

# injury by position
injuries %>% group_by(Role) %>% summarise(Count = n())

# injury by action
injuries %>% group_by(Role, Player_Activity_Derived) %>% summarise(Count = n())

# fair catch rate
play.info %>% mutate(FC = grepl("fair catch", PlayDescription)) %>% summarise(Fair_Catch_Rate = sum(FC)/n())

# average punt return
play.info <- play.info %>% 
    mutate(outcome = as.integer(gsub("^.*for *(.*?) +yard.*$", "\\1", 
                                     PlayDescription, ignore.case = T)))
play.info$outcome[grep("no gain", play.info$PlayDescription)] = 0
summary(play.info$outcome)

# average on punts in between -5 and 20
play.info %>% filter(outcome > -5 & outcome < 20) %>% summarise(avg_yds = mean(outcome, na.rm = T))

# density plot
p <- ggplot(play.info, aes(x = outcome)) + geom_density(color="darkblue", fill="lightblue")

p + geom_vline(aes(xintercept= -5),color="blue",  size=0.5) +geom_vline(aes(xintercept= 20),color="blue",  size=0.5)+theme_bw()+
  labs(title="Punt Return Density",x="Return Yards", y = "Density")