library("tidyverse")
library("knitr")

data=read_csv("results.csv")    
ccgrid=read_csv("ccgrid.csv")


r_=function(x){round(x,digits=2)}

finalEnergy=data%>%group_by(simkey,wireless,wakeupfor)%>%summarize(wireless=first(wireless),sd=sd(energy),wakeupfor=first(wakeupfor),energy=mean(energy))
#finalStats=data%>%group_by(simkey,wireless,wakeupfor,seed)%>%
 #   summarise(success=sum(nSend))%>%
  #  ungroup()%>%group_by(simkey,wireless,wakeupfor)%>%
   # summarise(sd_success=sd(success),success=mean(success))

# Success
finalStats=data%>%filter(isSender!=0)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(sd_success=sd(nSend),success=mean(nSend))

# Hint Added
#hintAdded=data%>%filter(isSender==0)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(hint_added=mean(hint_added))
#finalStats=finalStats%>%left_join(hintAdded,by=c("simkey","wireless","wakeupfor"))

# Sender energy
senderEnergy=data%>%filter(isSender!=0)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(senderEnergy=mean(energy),senderEnergy_sd=sd(energy))
receiverEnergy=data%>%filter(isSender==0)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(receiverEnergy=mean(energy),receiverEnergy_sd=sd(energy))
finalStats=finalStats%>%left_join(senderEnergy,by=c("simkey","wireless","wakeupfor"))
finalStats=finalStats%>%left_join(receiverEnergy,by=c("simkey","wireless","wakeupfor"))

# J per success
finalStats=finalStats%>%mutate(senderJPS=senderEnergy/success)
finalStats=finalStats%>%mutate(receiverJPS=receiverEnergy/success)

# Uptime
senderUptime=data%>%filter(isSender!=0)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(senderUptime=mean(totalUptime))
receiverUptime=data%>%filter(isSender==0)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(receiverUptime=mean(totalUptime))
finalStats=finalStats%>%left_join(senderUptime,by=c("simkey","wireless","wakeupfor"))
finalStats=finalStats%>%left_join(receiverUptime,by=c("simkey","wireless","wakeupfor"))

# avgTimedataRcv
avgTimeDataRcvData=data%>%filter(timeDataRcv!=-1)%>%group_by(simkey,wireless,wakeupfor)%>%summarise(avgTimeDataRcv=mean(timeDataRcv))
finalStats=finalStats%>%left_join(avgTimeDataRcvData,by=c("simkey","wireless","wakeupfor"))

# Uptime Overhead compare to baseline
baseline=finalStats%>%filter(simkey=="baseline")%>%select(senderUptime,receiverUptime,simkey,wireless,wakeupfor)
keys=unique(finalStats$simkey)
baseline=baseline%>%uncount(length(keys),.id="id")%>%mutate(simkey=keys[id])
finalStats=finalStats%>%left_join(baseline,by=c("simkey","wireless","wakeupfor"),suffix = c("","_baseline"))
finalStats=finalStats%>%mutate(senderUptimeOverhead=senderUptime-senderUptime_baseline)
finalStats=finalStats%>%mutate(receiverUptimeOverhead=receiverUptime-receiverUptime_baseline)

# Energy Overhead (percent) compare to baseline
baseline=finalStats%>%filter(simkey=="baseline")%>%select(senderEnergy,receiverEnergy,simkey,wireless,wakeupfor)
baseline=baseline%>%uncount(length(keys),.id="id")%>%mutate(simkey=keys[id])
finalStats=finalStats%>%left_join(baseline,by=c("simkey","wireless","wakeupfor"),suffix = c("","_baseline"))
finalStats=finalStats%>%mutate(senderEnergyOverheadPercent=r_((senderEnergy-senderEnergy_baseline)/senderEnergy*100))
finalStats=finalStats%>%mutate(receiverEnergyOverheadPercent=r_((receiverEnergy-receiverEnergy_baseline)/receiverEnergy*100))

# Comparison
finalStats=finalStats%>%left_join(ccgrid,by=c("simkey","wireless","wakeupfor"),suffix = c("","_ccgrid"))
finalStats=finalStats%>%mutate(senderEnergy_err=abs(r_((senderEnergy-senderEnergy_ccgrid)/senderEnergy)*100))
finalStats=finalStats%>%mutate(receiverEnergy_err=abs(r_((receiverEnergy-receiverEnergy_ccgrid)/receiverEnergy)*100))
finalStats=finalStats%>%mutate(senderUptime_err=abs(r_((senderUptime-senderUptime_ccgrid)/senderUptime)*100))
finalStats=finalStats%>%mutate(receiverUptime_err=abs(r_((receiverUptime-receiverUptime_ccgrid)/receiverUptime)*100))

# Add nSendFail to stats
sendFail=data%>%filter(node=="on0")%>%group_by(simkey,wireless,wakeupfor)%>%summarise(nSendFail=mean(nSendFail))
finalStats=finalStats%>%left_join(sendFail,by=c("simkey","wireless","wakeupfor"))

# Add nWakeUp to stats
nWakeUpRcv=data%>%filter(node!="on0")%>%group_by(simkey,wireless,wakeupfor)%>%summarise(nWakeUpRcv=mean(nWakeUp))
finalStats=finalStats%>%left_join(nWakeUpRcv,by=c("simkey","wireless","wakeupfor"))
nWakeUpSend=data%>%filter(node=="on0")%>%group_by(simkey,wireless,wakeupfor)%>%summarise(nWakeUpSender=mean(nWakeUp))
finalStats=finalStats%>%left_join(nWakeUpSend,by=c("simkey","wireless","wakeupfor"))


ggplot(finalStats,aes(x=simkey,senderEnergy_err,color=wireless,group=wireless))+geom_point()+geom_line()+facet_wrap(~wakeupfor)+xlab("Scenarios") + ylab("Sender Energy Relative Error (%)") + expand_limits(x = 0, y = 0)
ggsave("err_sender_energy.png",width=7,height = 5)

ggplot(finalStats,aes(x=simkey,receiverEnergy_err,color=wireless,group=wireless))+geom_point()+geom_line()+facet_wrap(~wakeupfor)+xlab("Scenarios") + ylab("Receiver Energy Relative Error (%)") + expand_limits(x = 0, y = 0)
ggsave("err_receiver_energy.png",width=7,height = 5)

ggplot(finalStats,aes(x=simkey,senderUptime_err,color=wireless,group=wireless))+geom_point()+geom_line()+facet_wrap(~wakeupfor)+xlab("Scenarios") + ylab("Sender AccUptime Energy Relative Error (%)") + expand_limits(x = 0, y = 0)
ggsave("err_sender_accuptime.png",width=7,height = 5)

ggplot(finalStats,aes(x=simkey,receiverUptime_err,color=wireless,group=wireless))+geom_point()+geom_line()+facet_wrap(~wakeupfor)+xlab("Scenarios") + ylab("Receiver AccUptime Energy Relative Error (%)") + expand_limits(x = 0, y = 0)
ggsave("err_rcv_accuptime.png",width=7,height = 5)


# Generate plots
ggplot(finalEnergy,aes(x=simkey,y=energy,fill=wireless))+
    geom_bar(stat="identity",position=position_dodge())+
    xlab("Scenarios")+ylab("Energy (J)")+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=energy-sd, ymax=energy+sd),width=0.5,size=1.5,) +
    facet_wrap(~ wakeupfor) +
    geom_text(aes(label = r_(energy+sd), y= energy+sd),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = r_(energy-sd), y= energy-sd),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))
ggsave("energy.png",width=13,height = 6)

ggplot(finalStats,aes(x=simkey,y=success,fill=wireless))+
    geom_bar(stat="identity",position=position_dodge())+
    xlab("Scenarios")+ylab("Number of Transmission Success")+
    geom_errorbar(position=position_dodge(0.9),aes(ymin=success-sd_success, ymax=success+sd_success),width=0.5,size=1.5,) +
    geom_text(aes(label = r_(success+sd_success), y= success+sd_success),vjust=-0.4,position=position_dodge(0.9))+
    geom_text(aes(label = r_(success-sd_success), y= success-sd_success),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(~ wakeupfor) +
ggsave("success.png")

ggplot(finalStats,aes(x=simkey,y=avgTimeDataRcv,fill=wireless))+
    geom_bar(stat="identity",position=position_dodge())+
    xlab("Scenarios")+ylab("Average Data Received Time")+
    facet_wrap(~ wakeupfor) +
ggsave("avgDataTimeRcv.png")

ggplot(finalStats,aes(x=simkey,y=nWakeUpRcv,fill=wireless))+
    geom_bar(stat="identity",position=position_dodge())+
    xlab("Scenarios")+ylab("Average Number of receiver Wake up")+
    facet_wrap(~ wakeupfor) +
ggsave("nWakeUpRcv.png")

ggplot(finalStats,aes(x=simkey,y=nWakeUpSender,fill=wireless))+
    geom_bar(stat="identity",position=position_dodge())+
    xlab("Scenarios")+ylab("Average Number of sender Wake up")+
    facet_wrap(~ wakeupfor) +
ggsave("nWakeUpSender.png")



##### Save tables #####
tf="tables-energy.org"
write("",file=tf)
for(wake in unique(data$wakeupfor)){
    write(kable(finalEnergy%>%filter(wireless=="lora",wakeupfor==wake)),file=tf,append=TRUE)
    write("\n",file=tf,append=TRUE)
    write(kable(finalEnergy%>%filter(wireless=="nbiot",wakeupfor==wake)),file=tf,append=TRUE)
    write("\n",file=tf,append=TRUE)
}

tf="tables-stats.org"
write("",file=tf)
for(wake in unique(data$wakeupfor)){
    write(kable(finalStats%>%filter(wireless=="lora",wakeupfor==wake)),file=tf,append=TRUE)
    write("\n",file=tf,append=TRUE)
    write(kable(finalStats%>%filter(wireless=="nbiot",wakeupfor==wake)),file=tf,append=TRUE)
    write("\n",file=tf,append=TRUE)
}

