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

r_=function(x){round(x,digits=2)}
color=function(){scale_fill_brewer(palette = "Accent")}
color2=function(){scale_fill_brewer(palette = "Set2")}
nolegend=function(){theme(legend.position="none")}
simkeys=unique(read_csv("results.csv")$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+\nExtended")
    return(key)
}
dformat=function(data){
    data%>%rowwise()%>%mutate(simkey=simkey_rename(simkey))%>%mutate(wireless=ifelse(wireless=="lora","LoRa","NbIoT"))
}

build_stats=function(file){
    data=read_csv(file)

    energy=data%>%group_by(simkey,wireless,seed,isSender,wakeupfor)%>%summarise(sd=sd(energy),energy=mean(energy))%>%mutate(type=ifelse(isSender,"Sender","Receiver"))

    # Only sender knows success:
    success=data%>%filter(isSender==1)%>%group_by(simkey,wireless,seed,wakeupfor)%>%summarise(success_orig=mean(nSend),success=mean(nSend))

    return(energy%>%ungroup()%>%left_join(success))

    
energy60=data%>%filter(wakeupfor==60)
energy60Snd=energy60%>%filter(isSender==1)%>%group_by(simkey,wireless,seed)%>%summarise(sd=sd(energy),energy=mean(energy),type="Sender")
energy60Rcv=energy60%>%filter(isSender==0)%>%group_by(simkey,wireless,seed)%>%summarise(sd=sd(energy),energy=mean(energy),type="Receiver")
energy60=energy60Snd%>%ungroup()%>%rbind(energy60Rcv)

print(energy60)
stopifnot(1)

success60Snd=data%>%filter(wakeupfor==60)%>%filter(isSender!=0)%>%group_by(simkey,wireless,seed)%>%summarise(success_orig=mean(nSend),success=mean(nSend),type="Sender")
success60Rcv=data%>%filter(wakeupfor==60)%>%filter(isSender!=0)%>%group_by(simkey,wireless,seed)%>%summarise(success_orig=mean(nSend),success=mean(nSend),type="Receiver")
success60=rbind(success60Snd,success60Rcv)

total60=energy60%>%left_join(success60,by=c("simkey","wireless","type"))

energy180=data%>%filter(wakeupfor==180)
energy180Snd=energy180%>%filter(isSender==1)%>%group_by(simkey,wireless,seed)%>%summarise(sd=sd(energy),energy=mean(energy),type="Sender")
energy180Rcv=energy180%>%filter(isSender==0)%>%group_by(simkey,wireless,seed)%>%summarise(sd=sd(energy),energy=mean(energy),type="Receiver")
energy180=energy180Snd%>%ungroup()%>%rbind(energy180Rcv)

success180Snd=data%>%filter(wakeupfor==180)%>%filter(isSender!=0)%>%group_by(simkey,wireless,seed)%>%summarise(success_orig=mean(nSend),success=mean(nSend),type="Sender")
success180Rcv=data%>%filter(wakeupfor==180)%>%filter(isSender!=0)%>%group_by(simkey,wireless,seed)%>%summarise(success_orig=mean(nSend),success=mean(nSend),type="Receiver")
success180=rbind(success180Snd,success180Rcv)

total180=energy180%>%left_join(success180,by=c("simkey","wireless","type"))

return(rbind(total60%>%mutate(wakeupfor=60),total180%>%mutate(wakeupfor=180)))
}

# Load stats
stats_sor=build_stats("logs/sor_results/results.csv")%>%filter(type=="Sender")%>%mutate(strategy="Shutdown on\nreceive")
stats_uor=build_stats("logs/uor_results/results.csv")%>%filter(type=="Sender")%>%mutate(strategy="Unschedule on\nreceive")
stats_fh=build_stats("logs/farhint_results/results.csv")%>%filter(type=="Sender")%>%mutate(strategy="Far hint")
stats=rbind(stats_sor,stats_uor,stats_fh)%>%filter(wireless=="nbiot",wakeupfor==60) # Change filter as you which (which plot you want down bellow)


pareto=tibble()
stats%>%ungroup()%>%group_by(wakeupfor,wireless)%>%group_walk(function(data, t){
for(i in 1:NROW(data)){
    pt=data[i,]
    e=pt$energy
    s=pt$success

    domE=data$energy<e
    domS=data$success>s

    if(!any(domE & domS)){
        pareto<<-rbind(pareto,cbind(pt,t))
    }
}})
pareto = pareto %>% mutate(energy = as.numeric(energy))
pareto = pareto %>% mutate(success = as.numeric(success))
pareto = pareto %>% arrange(energy,success)
pareto = pareto %>%dformat()

ggplot(stats%>%dformat(),aes(energy,success,color=simkey,shape=strategy))+
    geom_line(data=pareto,aes(energy,success),linetype="dashed", size=1,inherit.aes=FALSE)+
    geom_point(alpha=0.1,size=4)+
    geom_point(data=pareto,size=4)+scale_y_reverse()+
    labs(color="Policies:",shape="Strategies:")+scale_color_brewer(palette = "Spectral")+theme_minimal()+theme(legend.position="top")+
    xlab("Sender energy consumption (J)")+ylab(TeX(r'(#Succ$_p$)'))
#    +facet_wrap(~wakeupfor+wireless,scale="free")
ggsave("pareto.pdf",width=9,height=9)
