library("tidyverse")
library("gridExtra")

data=read_csv("results.csv")    
r_=function(x){round(x,digits=1)}
color=function(){scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))}

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)%>%mutate(type=factor(type,levels = c("Sender","Receiver")))
energy60=energy60%>%mutate(sd_min=energy-sd,sd_max=energy+sd)%>%mutate(sd_min_txt=paste0("-",r_(sd)))%>%mutate(sd_max_txt=paste0("+",r_(sd)))

energy60P=ggplot(energy60,aes(x=simkey,y=energy,fill=type))+
    geom_bar(stat="identity",position=position_dodge())+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=energy-sd, ymax=energy+sd),width=0.5,size=1.5)+
    geom_text(aes(label = sd_max_txt, y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = sd_min_txt, y= sd_min),vjust=+1.5,position=position_dodge(0.9))+
    geom_text(aes(label = r_(energy), y= energy/2),fontface="bold",colour="white",position=position_dodge(0.9))+
    facet_wrap(~wireless)+
    xlab("Policy")+ylab("Energy Consumption (J)")+
    labs(fill="Node type")+theme(legend.position="top")+color()
#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)))%>%mutate(sd_max_txt=paste0("+",r_(sd)))

energy180P=ggplot(energy180,aes(x=simkey,y=energy,fill=type))+
    geom_bar(stat="identity",position=position_dodge())+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=energy-sd, ymax=energy+sd),width=0.5,size=1.5)+
    geom_text(aes(label = sd_max_txt, y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = sd_min_txt, y= sd_min),vjust=+1.5,position=position_dodge(0.9))+
    geom_text(aes(label = r_(energy), y= energy/2),fontface="bold",colour="white",position=position_dodge(0.9))+
    facet_wrap(~wireless)+
    xlab("Policy")+ylab("Energy Consumption (J)")+
    labs(fill="Node type")+theme(legend.position="top")+color()
#ggsave("energy-180sec.png",width = 12,height=4.8)


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)%>%mutate(sd_min_txt=paste0("-",r_(sd)))%>%mutate(sd_max_txt=paste0("+",r_(sd)))



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)))%>%mutate(sd_max_txt=paste0("+",r_(sd)))

totalUptime60P=ggplot(totalUptime60,aes(x=simkey,y=totalUptime,fill=type))+
    geom_bar(stat="identity",position=position_dodge())+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=totalUptime-sd, ymax=totalUptime+sd),width=0.5,size=1.5)+
    geom_text(aes(label = sd_max_txt, y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = sd_min_txt, y= sd_min),vjust=+1.5,position=position_dodge(0.9))+
    geom_text(aes(label = r_(totalUptime), y= totalUptime/2),fontface="bold",colour="white",position=position_dodge(0.9))+
    facet_wrap(~wireless)+
    xlab("Policy")+ylab("Accumulated Uptime Consumption (s)")+
    labs(fill="Node type")+theme(legend.position="none")+color()

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)))%>%mutate(sd_max_txt=paste0("+",r_(sd)))

totalUptime180P=ggplot(totalUptime180,aes(x=simkey,y=totalUptime,fill=type))+
    geom_bar(stat="identity",position=position_dodge())+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=totalUptime-sd, ymax=totalUptime+sd),width=0.5,size=1.5)+
    geom_text(aes(label = sd_max_txt, y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = sd_min_txt, y= sd_min),vjust=+1.5,position=position_dodge(0.9))+
    geom_text(aes(label = r_(totalUptime), y= totalUptime/2),fontface="bold",colour="white",position=position_dodge(0.9))+
    facet_wrap(~wireless)+
    xlab("Policy")+ylab("Accumulated Uptime Consumption (s)")+
    labs(fill="Node type")+theme(legend.position="none")+color()




success60P=ggplot(success60,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.5,size=1.5)+
    geom_text(aes(label = sd_max_txt, y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = sd_min_txt, 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()
#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)%>%mutate(sd_min_txt=paste0("-",r_(sd)))%>%mutate(sd_max_txt=paste0("+",r_(sd)))

success180P=ggplot(success180,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.5,size=1.5)+
    geom_text(aes(label = sd_max_txt, y= sd_max),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = sd_min_txt, 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()
#ggsave("success-180sec.png",width = 5.8,height=4.2)

sec60P=grid.arrange(energy60P,totalUptime60P,success60P,heights=c(2,2,2),widths=1,ncol=1)
ggsave("60sec.png",plot=sec60P,width=11,height=15)

sec180P=grid.arrange(energy180P,totalUptime180P,success180P,heights=c(2,2,2),widths=1,ncol=1)
ggsave("180sec.png",plot=sec180P,width=11,height=15)

