/**
 * EventChannelPair.java
 *
 * @author Created by Charles Groves
 */

package edu.sc.seis.sod.hibernate.eventpair;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.sc.seis.seisFile.fdsnws.stationxml.Channel;
import edu.sc.seis.sod.BuildVersion;
import edu.sc.seis.sod.LocalSeismogramArm;
import edu.sc.seis.sod.Start;
import edu.sc.seis.sod.hibernate.SodDB;
import edu.sc.seis.sod.model.event.StatefulEvent;
import edu.sc.seis.sod.model.station.ChannelIdUtil;
import edu.sc.seis.sod.model.status.Stage;
import edu.sc.seis.sod.model.status.Standing;
import edu.sc.seis.sod.model.status.Status;
import edu.sc.seis.sod.util.exceptionHandler.GlobalExceptionHandler;

public class EventChannelPair extends AbstractEventChannelPair {
    
    /** for hibernate */
    protected EventChannelPair() {}
    
    public EventChannelPair(StatefulEvent event, Channel chan, EventStationPair esp) {
        this(event, chan, Status.get(Stage.EVENT_CHANNEL_POPULATION, Standing.INIT), esp);
    }

    public EventChannelPair(StatefulEvent event, Channel chan, Status status, EventStationPair esp) {
        super(event, status, esp);
        setChannel(chan);
    }

    public void run() {
        try {
            ((LocalSeismogramArm)Start.getWaveformRecipe()).processLocalSeismogramArm(this);
            SodDB.commit();
            logger.debug("Finish ECP: "+this);
        } catch(Throwable t) {
            System.err.println(EventChannelPair.BIG_ERROR_MSG);
            t.printStackTrace(System.err);
            GlobalExceptionHandler.handle(EventChannelPair.BIG_ERROR_MSG, t);
            try {
                SodDB.rollback();
                update(t, Status.get(Stage.PROCESSOR, Standing.SYSTEM_FAILURE));
                SodDB.commit();
            } catch(Throwable tt) {
                System.err.println("SOD cannot update status of ecp, this indicates a significant problem with the database. SOD is now exiting with shame and dispair");
                Start.cataclysmicFailureOfUnbelievableProportions();
            }
        }
    }

    public boolean equals(Object o){
        if(!(o instanceof EventChannelPair)) return false;
        EventChannelPair ecp = (EventChannelPair)o;
        if(ecp.getEventDbId() == getEventDbId() &&
           ecp.getChannelDbId() == chan.getDbid()){
            return true;
        }
        return false;
    }

    public int hashCode(){
        int code = 47 * getChannelDbId();
        code += 23 * getEventDbId();
        return code;
    }

    public String toString(){
        return "EventChannelPair: ("+getDbid()+") " + getEvent() + " " +
            ChannelIdUtil.toStringNoDates(getChannel()) + " " + getStatus();
    }

    public int getChannelDbId(){ return chan.getDbid(); }

    public Channel getChannel() { return chan; }
    
    /** for use by hibernate */
    protected void setChannel(Channel chan) {
        this.chan = chan;
    }

    private Channel chan;

    public static final String BIG_ERROR_MSG = BuildVersion.getVersion()+" An exception occured that would've croaked a waveform worker thread!  These types of exceptions are certainly possible, but they shouldn't be allowed to percolate this far up the stack.  If you are one of those esteemed few working on SOD, it behooves you to attempt to trudge down the stack trace following this message and make certain that whatever threw this exception is no longer allowed to throw beyond its scope.  If on the other hand, you are a user of SOD it would be most appreciated if you would send an email containing the text immediately following this mesage to sod@seis.sc.edu ";

    private static Logger logger = LoggerFactory.getLogger(EventChannelPair.class);
}
