import org.lsst.ccs.description.groovy.CCSBuilder;
import org.lsst.ccs.subsystem.mmm.MMM_IR2;
import org.lsst.ccs.subsystem.mmm.MinionIR2;
import org.lsst.ccs.subsystem.mmm.MinionIR2Group;
import org.lsst.ccs.subsystem.mmm.AlertDispatcher.AlertHandler;
import org.lsst.ccs.subsystem.mmm.AlertDispatcher.AlertAction;
import org.lsst.ccs.subsystem.mmm.AlertDispatcher.EmailAlertAction;
import org.lsst.ccs.subsystem.mmm.AlertDispatcher.EmailAlertDeliveryStrategy;
import org.lsst.ccs.subsystem.mmm.AlertDispatcher.CommandAlertAction;
import org.lsst.ccs.subsystem.mmm.AlertDispatcher;
import org.lsst.ccs.subsystem.mmm.AlertNotification;
import org.lsst.ccs.subsystem.rafts.alerts.RaftAlert;
import org.lsst.ccs.bus.states.AlertState;
import java.util.function.Predicate;
import java.time.Duration;
import org.lsst.ccs.bootstrap.BootstrapResourceUtils;

CCSBuilder builder = ["comcam-mmm"]

Properties props = BootstrapResourceUtils.getBootstrapSystemProperties()
def isSimulated = props.getProperty("org.lsst.ccs.run.mode", "normal").equals("simulation")


def fromEmail = isSimulated ? "SIMULATED-ccs-alarm@lsst-mmm.slac.stanford.edu" : "comcam@lsst.org"
def replyToEmail = isSimulated ? "SIMULATED-lsstcam-opsprob@slac.stanford.edu" : "BStalder@lsst.org";

def infrastructureList = ["turri@slac.stanford.edu", "homer@slac.stanford.edu"];
def comcamList = isSimulated ? infrastructureList : ["turri@slac.stanford.edu","homer@SLAC.Stanford.EDU","gmorris@slac.stanford.edu","tonyj@slac.stanford.edu","margaux@SLAC.Stanford.EDU","reil@SLAC.Stanford.EDU","BStalder@lsst.org","MReuter@lsst.org"];



builder.
    main(MMM_IR2, 
        minions:[(MinionIR2Group.INFRASTRUCTURE):["localdb":MinionIR2.LOCALBD, "rest-server":MinionIR2.REST_SERVER, "comcam-mmm":MinionIR2.MMM, "cluster-monitor":MinionIR2.CLUSTER_MONITOR],
                 (MinionIR2Group.COMCAM) :["comcam-vacuum":MinionIR2.VACUUM, "comcam-fp":MinionIR2.FOCAL_PLANE, "comcam-rebpower":MinionIR2.POWER, , "comcam-quadbox":MinionIR2.QUADBOX]
                ] ) {

    infrastructure (AlertDispatcher, group: MinionIR2Group.INFRASTRUCTURE) {
        infrastructureEmailNotification (AlertHandler, handlerName: "infrastructureEmailNotification") {
            infrastructurePublicMailAction (EmailAlertAction, emailList: infrastructureList, fromEmail:"$fromEmail", replyToEmail:"$replyToEmail", smtpHost:"mail.lsst.org") {
                localDbEmailStrategy (EmailAlertDeliveryStrategy, sendFirstEmail: false, origin:"localdb", maxAccumulation: Duration.ofMinutes(30))
                defaultEmailStrategy (EmailAlertDeliveryStrategy, sendFirstEmail: true, origin:".*", maxAccumulation: Duration.ofMinutes(0))
            }
        }        
    }
    

    comcam (AlertDispatcher, group: MinionIR2Group.COMCAM) {
        comcamEmailNotification (AlertHandler, handlerName: "comcamEmailNotification") {
             comcamPublicMailAction (EmailAlertAction, emailList: comcamList, fromEmail:"$fromEmail", replyToEmail:"$replyToEmail", smtpHost:"mail.lsst.org")
        }        
    }
}
