library("tidyverse")
library("ggthemes")
library("gridExtra")
library("patchwork")
library(RColorBrewer)

data=read_csv("results_scalability.csv")    
r_=function(x){round(x,digits=1)}
color=function(){scale_fill_brewer(palette = "Accent")}
color2=function(){scale_fill_brewer(palette = "Set2")}
nolegend=function(){theme(legend.position="none")}
simkeys=unique(data$simkey)
nsimkeys=length(simkeys)
s_=function(x){if(x<0){return("")}else{return("+")}}
simkey_rename=function(key){
    if(key=="hint")
        return("Hints")
    if(key=="baseline")
        return("Baseline")
    if(key=="extended")
        return("Extended")
    if(key=="hintandextended")
        return("Hints+Extended")
    return(key)
}
dformat=function(data){
    data%>%rowwise()%>%mutate(simkey=simkey_rename(simkey))%>%mutate(wireless=ifelse(wireless=="lora","LoRa","Nb-IoT"))
}
g_legend <- function(a.gplot){ 
    tmp <- ggplot_gtable(ggplot_build(a.gplot)) 
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
    legend <- tmp$grobs[[leg]] 
    legend
}

custom_theme=function(){theme_clean()+ theme(plot.background=element_blank())}

data=data%>%dformat()

# Compute delivery success
statsSuccess=data%>%filter(isSender!=0)%>%group_by(wireless,wakeupfor,n_nodes,seed,simkey)%>%summarize(success=mean(nSend))%>%ungroup()
data=data%>%left_join(statsSuccess,by=c("wireless","wakeupfor","n_nodes","seed","simkey"))

# Computer stats senders
statsSender=data%>%filter(isSender!=0)%>%group_by(wireless,wakeupfor,n_nodes,seed,simkey)%>%summarize(success=mean(success),energy=mean(energy))%>%ungroup()
statsSender=statsSender%>%group_by(wireless,wakeupfor,n_nodes,simkey)%>%summarize(success_sd=sd(success),success=mean(success),energy_sd=sd(energy),energy=mean(energy))%>%ungroup()

# Computer stats receiver
statsReceiver=data%>%filter(isSender==0)%>%group_by(wireless,wakeupfor,n_nodes,seed,simkey)%>%summarize(success=mean(success),energy=mean(energy))%>%ungroup()
statsReceiver=statsReceiver%>%group_by(wireless,wakeupfor,n_nodes,simkey)%>%summarize(success_sd=sd(success),success=mean(success),energy_sd=sd(energy),energy=mean(energy))%>%ungroup()


# Energy 
sender60sPlotEnergy=ggplot(statsSender%>%filter(wakeupfor==60),aes(n_nodes,energy,color=simkey,group=simkey))+
    geom_ribbon(aes(ymin=energy-energy_sd, ymax=energy+energy_sd,fill=simkey),linetype=1,alpha=0.4)+
    geom_point()+geom_line()+
    facet_wrap(~wireless)+xlab("Number of node")+ylab("Energy consumption (J)")+labs(colour="Policy")+custom_theme()+theme(legend.position="top")+ggtitle("Sender")+
    guides(colour=FALSE,fill=guide_legend(title="Policy"))

sender180sPlotEnergy=ggplot(statsSender%>%filter(wakeupfor==180),aes(n_nodes,energy,color=simkey,group=simkey))+
    geom_ribbon(aes(ymin=energy-energy_sd, ymax=energy+energy_sd,fill=simkey),linetype=1,alpha=0.4)+
    geom_point()+geom_line()+
    facet_wrap(~wireless)+xlab("Number of node")+ylab("Energy consumption (J)")+labs(colour="Policy")+custom_theme()+theme(legend.position="top")+ggtitle("Sender")+
    guides(colour=FALSE,fill=guide_legend(title="Policy"))


receiver60sPlotEnergy=ggplot(statsReceiver%>%filter(wakeupfor==60),aes(n_nodes,energy,color=simkey,group=simkey))+
    geom_ribbon(aes(ymin=energy-energy_sd, ymax=energy+energy_sd,,fill=simkey),linetype=1,alpha=0.4)+
    geom_point()+geom_line()+
    facet_wrap(~wireless)+xlab("Number of node")+ylab("Energy consumption (J)")+labs(colour="Policy") +  custom_theme()+theme(legend.position="top")+ggtitle("Receiver")+
    guides(colour=FALSE,fill=guide_legend(title="Policy"))

receiver180sPlotEnergy=ggplot(statsReceiver%>%filter(wakeupfor==180),aes(n_nodes,energy,color=simkey,group=simkey))+
    geom_ribbon(aes(ymin=energy-energy_sd, ymax=energy+energy_sd,fill=simkey),linetype=1,alpha=0.4)+
    geom_point()+geom_line()+
    facet_wrap(~wireless)+xlab("Number of node")+ylab("Energy consumption (J)")+labs(colour="Policy")+custom_theme()+theme(legend.position="top")+ggtitle("Receiver")+
    guides(colour=FALSE,fill=guide_legend(title="Policy"))

# Success
sender60sPlotSuccess=ggplot(statsSender%>%filter(wakeupfor==60),aes(n_nodes,success,color=simkey,group=simkey))+
    geom_ribbon(aes(ymin=success-success_sd, ymax=success+success_sd,fill=simkey),linetype=1,alpha=0.4)+
    geom_point()+geom_line()+
    facet_wrap(~wireless)+xlab("Number of node")+ylab("Number of delivery success")+labs(colour="Policy")+custom_theme()+theme(legend.position="top")+
    theme(panel.background = element_rect(fill = '#EFEFEF', color=NA))+guides(colour=FALSE,fill=guide_legend(title="Policy"))

sender180sPlotSuccess=ggplot(statsSender%>%filter(wakeupfor==180),aes(n_nodes,success,color=simkey,group=simkey))+
    geom_ribbon(aes(ymin=success-success_sd, ymax=success+success_sd,fill=simkey),linetype=1,alpha=0.4)+
    geom_point()+geom_line()+
    facet_wrap(~wireless)+xlab("Number of node")+ylab("Number of delivery success")+labs(colour="Policy")+custom_theme()+theme(legend.position="top")+
    theme(panel.background = element_rect(fill = '#EFEFEF', color=NA))+guides(colour=FALSE,fill=guide_legend(title="Policy"))


receiver60sPlotSuccess=ggplot(statsReceiver%>%filter(wakeupfor==60),aes(n_nodes,success,color=simkey,group=simkey))+
    geom_ribbon(aes(ymin=success-success_sd, ymax=success+success_sd,fill=simkey),linetype=1,alpha=0.4)+
    geom_point()+geom_line()+
    facet_wrap(~wireless)+xlab("Number of node")+ylab("Number of delivery success")+labs(colour="Policy") +  custom_theme()+theme(legend.position="top")+
    theme(panel.background = element_rect(fill = '#EFEFEF', color=NA))+guides(colour=FALSE,fill=guide_legend(title="Policy"))

receiver180sPlotSuccess=ggplot(statsReceiver%>%filter(wakeupfor==180),aes(n_nodes,success,color=simkey,group=simkey))+
    geom_ribbon(aes(ymin=success-success_sd, ymax=success+success_sd,fill=simkey),linetype=1,alpha=0.4)+
    geom_point()+geom_line()+
    facet_wrap(~wireless)+xlab("Number of node")+ylab("Number of delivery success")+labs(colour="Policy")+custom_theme()+theme(legend.position="top")+
    theme(panel.background = element_rect(fill = '#EFEFEF', color=NA))+guides(colour=FALSE,fill=guide_legend(title="Policy"))



w1=7
h1=4
w2=15
h2=4
ggsave("scalability_60s_sender_energy.pdf",plot=sender60sPlotEnergy+guides(fill = FALSE, color = FALSE),width=w1,height=h1)
ggsave("scalability_60s_receiver_energy.pdf",plot=receiver60sPlotEnergy+guides(fill = FALSE, color = FALSE),width=w1,height=h1)
ggsave("scalability_60s_success.pdf",plot=sender60sPlotSuccess,width=w2,height=h2)

ggsave("scalability_180s_sender_energy.pdf",plot=sender180sPlotEnergy+guides(fill = FALSE, color = FALSE),width=w1,height=h1)
ggsave("scalability_180s_receiver_energy.pdf",plot=receiver180sPlotEnergy+guides(fill = FALSE, color = FALSE),width=w1,height=h1)
ggsave("scalability_180s_success.pdf",plot=sender180sPlotSuccess,width=w2,height=h2)

