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

data=read_csv("results.csv")    
r_=function(x){round(x,digits=1)}
color=function(){scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))}
color2=function(){scale_fill_brewer(palette = "Dark2")}
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","NbIoT"))
}
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
} 

energy60=data%>%filter(wakeupfor==60)
energy60Snd=energy60%>%filter(isSender==1)%>%group_by(simkey,wireless)%>%summarise(sd=sd(energy),energy=mean(energy),type="Sender")
energy60Rcv=energy60%>%filter(isSender==0)%>%group_by(simkey,wireless)%>%summarise(sd=sd(energy),energy=mean(energy),type="Receiver")
energy60=energy60Snd%>%ungroup()%>%rbind(energy60Rcv)
energy60=energy60%>%mutate(sd_min=energy-sd,sd_max=energy+sd)%>%mutate(sd_min_txt=paste0("-",r_(sd_min)))%>%mutate(sd_max_txt=paste0("+",r_(sd_max)))
energy60Baseline=energy60%>%filter(simkey=="baseline")%>%uncount(nsimkeys,.id="id")%>%mutate(simkey=simkeys[id])
energy60=energy60%>%left_join(energy60Baseline,by=c("simkey","wireless","type"),suffix = c("","_baseline"))
energy60=energy60%>%mutate(ovhd=energy*100/energy_baseline-100)
energy60=energy60%>%rowwise()%>%mutate(ovhd_txt=paste0("(",s_(ovhd),r_(ovhd),"%)"))
success60Snd=data%>%filter(wakeupfor==60)%>%filter(isSender!=0)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(success_orig=mean(nSend),success=r_(mean(nSend)),type="Sender")
success60Rcv=data%>%filter(wakeupfor==60)%>%filter(isSender!=0)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(success_orig=mean(nSend),success=r_(mean(nSend)),type="Receiver")
success60=rbind(success60Snd,success60Rcv)
success60=success60%>%rowwise()%>%mutate(success_txt=paste0("[",success,"]"))
energy60=energy60%>%left_join(success60)%>%rowwise()%>%mutate(infos=paste0(ovhd_txt,"\n{",r_(energy/success_orig),"}"))
energy60=energy60%>%mutate(type=factor(type,levels = c("Sender","Receiver")))


energy60P=ggplot(energy60%>%dformat,aes(x=simkey,y=energy,fill=type))+
    geom_bar(colour="black", stat="identity",position=position_dodge())+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=energy-sd, ymax=energy+sd),width=0.3,size=1)+
    geom_text(aes(label = r_(sd_max), y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = r_(sd_min), y= sd_min),vjust=+1.5,position=position_dodge(0.9))+
    geom_text(aes(label = paste0(r_(energy),"\n",infos), y= min(energy)/2),fontface="bold",colour="white",position=position_dodge(0.9))+
    geom_label(data=success60%>%dformat(),aes(label = success_txt, y=35),label.padding=unit(0.35,"lines"),label.r=unit(0.09,"lines"),fill="#f0f0f0",label.size=0.5,fontface="bold",colour="black",position=position_dodge(0.9))+
    facet_wrap(~wireless)+
    xlab(element_blank())+ylab("Energy consumption (J)")+
    labs(fill="Energy:")+theme(legend.position="top")+color()+coord_cartesian(ylim=c(0,1200))
#ggsave("energy-60sec.png",width = 12,height=4.8)

energy180=data%>%filter(wakeupfor==180)
energy180Snd=energy180%>%filter(isSender==1)%>%group_by(simkey,wireless)%>%summarise(sd=sd(energy),energy=mean(energy),type="Sender")
energy180Rcv=energy180%>%filter(isSender==0)%>%group_by(simkey,wireless)%>%summarise(sd=sd(energy),energy=mean(energy),type="Receiver")
energy180=energy180Snd%>%ungroup()%>%rbind(energy180Rcv)%>%mutate(type=factor(type,levels = c("Sender","Receiver")))
energy180=energy180%>%mutate(sd_min=energy-sd,sd_max=energy+sd)%>%mutate(sd_min_txt=paste0("-",r_(sd_min)))%>%mutate(sd_max_txt=paste0("+",r_(sd_max)))
energy180Baseline=energy180%>%filter(simkey=="baseline")%>%uncount(nsimkeys,.id="id")%>%mutate(simkey=simkeys[id])
energy180=energy180%>%left_join(energy180Baseline,by=c("simkey","wireless","type"),suffix = c("","_baseline"))
energy180=energy180%>%mutate(ovhd=energy*100/energy_baseline-100)
energy180=energy180%>%rowwise()%>%mutate(ovhd_txt=paste0("(",s_(ovhd),r_(ovhd),"%)"))
success180Snd=data%>%filter(wakeupfor==180)%>%filter(isSender!=0)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(success_orig=mean(nSend),success=r_(mean(nSend)),type="Sender")
success180Rcv=data%>%filter(wakeupfor==180)%>%filter(isSender!=0)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(success_orig=mean(nSend),success=r_(mean(nSend)),type="Receiver")
success180=rbind(success180Snd,success180Rcv)
success180=success180%>%rowwise()%>%mutate(success_txt=paste0("[",success,"]"))
energy180=energy180%>%left_join(success180)%>%rowwise()%>%mutate(infos=paste0(ovhd_txt,"\n{",r_(energy/success_orig),"}"))
energy180=energy180%>%mutate(type=factor(type,levels = c("Sender","Receiver")))

energy180P=ggplot(energy180%>%dformat,aes(x=simkey,y=energy,fill=type))+
    geom_bar(colour="black",stat="identity",position=position_dodge())+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=energy-sd, ymax=energy+sd),width=0.3,size=1)+
    geom_text(aes(label = r_(sd_max), y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = r_(sd_min), y= sd_min),vjust=+1.5,position=position_dodge(0.9))+
    geom_text(aes(label = paste0(r_(energy),"\n",infos), y= min(energy)/2),fontface="bold",colour="white",position=position_dodge(0.9))+
    geom_label(data=success180%>%dformat(),aes(label = success_txt, y=100),label.padding=unit(0.35,"lines"),label.r=unit(0.09,"lines"),fill="#f0f0f0",label.size=0.5,fontface="bold",colour="black",position=position_dodge(0.9))+
    facet_wrap(~wireless)+
    xlab(element_blank())+ylab("Energy consumption (J)")+
    labs(fill="Energy:")+theme(legend.position="top")+color()+coord_cartesian(ylim=c(0,2600))
#ggsave("energy-180sec.png",width = 12,height=4.8)

totalUptime60=data%>%filter(wakeupfor==60)
totalUptime60Snd=totalUptime60%>%filter(isSender==1)%>%group_by(simkey,wireless)%>%summarise(sd=sd(totalUptime),totalUptime=mean(totalUptime),type="Sender")
totalUptime60Rcv=totalUptime60%>%filter(isSender==0)%>%group_by(simkey,wireless)%>%summarise(sd=sd(totalUptime),totalUptime=mean(totalUptime),type="Receiver")
totalUptime60=totalUptime60Snd%>%ungroup()%>%rbind(totalUptime60Rcv)%>%mutate(type=factor(type,levels = c("Sender","Receiver")))
totalUptime60=totalUptime60%>%mutate(sd_min=totalUptime-sd,sd_max=totalUptime+sd)%>%mutate(sd_min_txt=paste0("-",r_(sd_min)))%>%mutate(sd_max_txt=paste0("+",r_(sd_max)))
totalUptime60Baseline=totalUptime60%>%filter(simkey=="baseline")%>%uncount(nsimkeys,.id="id")%>%mutate(simkey=simkeys[id])
totalUptime60=totalUptime60%>%left_join(totalUptime60Baseline,by=c("simkey","wireless","type"),suffix = c("","_baseline"))
totalUptime60=totalUptime60%>%mutate(ovhd=totalUptime-totalUptime_baseline)
totalUptime60=totalUptime60%>%rowwise()%>%mutate(ovhd_txt=paste0("(",s_(ovhd),r_(ovhd),")"))
totalUptime60=totalUptime60%>%mutate(type=factor(type,levels = c("Sender","Receiver")))

totalUptime60P=ggplot(totalUptime60%>%dformat,aes(x=simkey,y=totalUptime,fill=type))+
    geom_bar(colour="black", stat="identity",position=position_dodge())+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=totalUptime-sd, ymax=totalUptime+sd),width=0.3,size=1)+
    geom_text(aes(label = r_(sd_max), y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = r_(sd_min), y= sd_min),vjust=+1.5,position=position_dodge(0.9))+
    geom_text(aes(label = paste0(r_(totalUptime),"\n",ovhd_txt), y= min(totalUptime)/2),fontface="bold",colour="white",position=position_dodge(0.9))+
    facet_wrap(~wireless)+
    xlab("Policy")+ylab("Accumulated uptime (s)")+
    labs(fill="Total uptime:")+color2()+theme(legend.position="top")+coord_cartesian(ylim=c(0,2500))

totalUptime180=data%>%filter(wakeupfor==180)
totalUptime180Snd=totalUptime180%>%filter(isSender==1)%>%group_by(simkey,wireless)%>%summarise(sd=sd(totalUptime),totalUptime=mean(totalUptime),type="Sender")
totalUptime180Rcv=totalUptime180%>%filter(isSender==0)%>%group_by(simkey,wireless)%>%summarise(sd=sd(totalUptime),totalUptime=mean(totalUptime),type="Receiver")
totalUptime180=totalUptime180Snd%>%ungroup()%>%rbind(totalUptime180Rcv)%>%mutate(type=factor(type,levels = c("Sender","Receiver")))
totalUptime180=totalUptime180%>%mutate(sd_min=totalUptime-sd,sd_max=totalUptime+sd)%>%mutate(sd_min_txt=paste0("-",r_(sd_min)))%>%mutate(sd_max_txt=paste0("+",r_(sd_max)))
totalUptime180Baseline=totalUptime180%>%filter(simkey=="baseline")%>%uncount(nsimkeys,.id="id")%>%mutate(simkey=simkeys[id])
totalUptime180=totalUptime180%>%left_join(totalUptime180Baseline,by=c("simkey","wireless","type"),suffix = c("","_baseline"))
totalUptime180=totalUptime180%>%mutate(ovhd=totalUptime-totalUptime_baseline)
totalUptime180=totalUptime180%>%rowwise()%>%mutate(ovhd_txt=paste0("(",s_(ovhd),r_(ovhd),")"))
totalUptime180=totalUptime180%>%mutate(type=factor(type,levels = c("Sender","Receiver")))

totalUptime180P=ggplot(totalUptime180%>%dformat,aes(x=simkey,y=totalUptime,fill=type))+
    geom_bar(colour="black",stat="identity",position=position_dodge())+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=totalUptime-sd, ymax=totalUptime+sd),width=0.3,size=1)+
    geom_text(aes(label = r_(sd_max), y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = r_(sd_min), y= sd_min),vjust=+1.5,position=position_dodge(0.9))+
        geom_text(aes(label = paste0(r_(totalUptime),"\n",ovhd_txt), y= min(totalUptime)/2),fontface="bold",colour="white",position=position_dodge(0.9))+
    facet_wrap(~wireless)+
    xlab("Policy")+ylab("Accumulated uptime (s)")+
    labs(fill="Total uptime:")+theme(legend.position="top")+color2()+coord_cartesian(ylim=c(0,5600))

success60=data%>%filter(wakeupfor==60)
success60=success60%>%filter(isSender==1)%>%group_by(simkey,wireless)%>%summarise(sd=sd(nSend),success=mean(nSend))
success60=success60%>%mutate(sd_min=success-sd,sd_max=success+sd)

success60P=ggplot(success60%>%dformat,aes(x=simkey,y=success))+
    geom_bar(stat="identity",position=position_dodge(),fill="gray50")+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=success-sd, ymax=success+sd),width=0.2,size=1)+
    geom_text(aes(label = r_(sd_max), y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = r_(sd_min), y= sd_min),vjust=+1.5,position=position_dodge(0.9))+
    geom_text(aes(label = r_(success), y= success/2),fontface="bold",colour="white",position=position_dodge(0.9))+
    facet_wrap(~wireless)+
    xlab("Policy")+ylab("Number of success")+
    labs(fill="Node type")+theme(legend.position="none")+color()+coord_cartesian(ylim=c(0,11))
#ggsave("success-60sec.png",width = 5.8,height=4.2)

success180=data%>%filter(wakeupfor==180)
success180=success180%>%filter(isSender==1)%>%group_by(simkey,wireless)%>%summarise(sd=sd(nSend),success=mean(nSend))
success180=success180%>%mutate(sd_min=success-sd,sd_max=success+sd)

success180P=ggplot(success180%>%dformat,aes(x=simkey,y=success))+
    geom_bar(stat="identity",position=position_dodge(),fill="gray50")+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=success-sd, ymax=success+sd),width=0.2,size=1)+
    geom_text(aes(label = r_(sd_max), y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = r_(sd_min), y= sd_min),vjust=+1.5,position=position_dodge(0.9))+
    geom_text(aes(label = r_(success), y= success/2),fontface="bold",colour="white",position=position_dodge(0.9))+
    facet_wrap(~wireless)+
    xlab("Policy")+ylab("Number of success")+
    labs(fill="Node type")+theme(legend.position="none")+color()+coord_cartesian(ylim=c(0,13))
#ggsave("success-180sec.png",width = 5.8,height=4.2)


ggsave("60sec.pdf",plot=(energy60P)/(totalUptime60P)+ plot_layout(guides = "collect")&theme(legend.position = "top"),width=12,height=7)
ggsave("180sec.pdf",plot=(energy180P)/(totalUptime180P)+ plot_layout(guides = "collect")&theme(legend.position = "top"),width=12,height=7)

