package com.vasoftware.sf.plugin;

import com.collabnet.ce.soap60.webservices.ClientSoapStubFactory;
import com.collabnet.ce.soap60.webservices.cemain.RelationshipSoapDO;
import com.collabnet.ce.soap60.webservices.tracker.ArtifactSoapDO;
import com.collabnet.ce.soap60.webservices.tracker.ITrackerAppSoap;
import com.vasoftware.sf.common.logger.Logger;
import com.vasoftware.sf.events.EventHandler60;
import org.apache.axis.AxisFault;

import javax.xml.namespace.QName;
import java.io.InputStream;
import java.util.Properties;

public class AsynchronousRelationshipEventListener extends EventHandler60 {

    private static final String WEB_SERVICE_ENDPOINT_PROPERTY = "webServiceEndpoint";
    private static final String FORMATSTRING_PROPERTY = "formatString";
    private static final String PROPERTIES_FILE = "META-INF/config.properties";
    private static String defaultWebserviceEndpoint = "http://localhost:8080";
    private static String defaultFormatString = "<OPERATION> association with artifact <TARGET>\n <DESCRIPTION>";
    private static final Logger log = Logger.getLogger(AsynchronousRelationshipEventListener.class);
    private boolean initialized = false;
    
    public  AsynchronousRelationshipEventListener() {
      initialize();
    }
    
    private synchronized void initialize() {
        if (initialized ) {
            return;
        }
        initialized = true;
        
        // read format string
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
            Properties prop = new Properties();
            prop.load(inputStream);
            String formatString = prop.getProperty(FORMATSTRING_PROPERTY);
            if (formatString != null) {
                defaultFormatString = formatString;
            }
            String webServiceEndpoint = prop.getProperty(WEB_SERVICE_ENDPOINT_PROPERTY);
            if (webServiceEndpoint != null) {
                defaultWebserviceEndpoint = webServiceEndpoint;
            }
        } catch (Exception e) {
            log.error("While reading the properties file " + PROPERTIES_FILE + " an error occured", e);
        }
    }

    @Override
    public void processEvent() throws Exception {    
        
        RelationshipSoapDO relationShip = (RelationshipSoapDO) getUpdatedData();
        String originId = relationShip.getOriginKey();
        String targetId = relationShip.getTargetKey();
        String description = relationShip.getDescription();
        String operation = getEventContext().getOperation();
        if (operation.equals("create")) {
            operation = "created ";
        } else if (operation.equals("update")) {
            operation = "updated ";
        } else if (operation.equals("delete")) {
            operation = "deleted  ";
        } else if (operation.equals("move")) {
            operation = "moved  ";
        }
        ITrackerAppSoap trackerClient = (ITrackerAppSoap) ClientSoapStubFactory.getSoapStub(ITrackerAppSoap.class,
                defaultWebserviceEndpoint);
        
        String comment = defaultFormatString.replace("<OPERATION>", operation);
        comment = comment.replace("<DESCRIPTION>", description);
        comment = comment.replace("<USER>", getEventContext().getUsername());
        
        if (originId.startsWith("artf")) {
            String finalComment = comment.replace("<TARGET>", targetId);
            finalComment = finalComment.replace("<SOURCE>", originId);
            
            boolean finishedUpdate = false;
            int numberOfTries = 0;
            while (!finishedUpdate && numberOfTries++ < 10) {   
                try {
                    ArtifactSoapDO artifact = trackerClient.getArtifactData(getSessionKey(), originId);
                    trackerClient.setArtifactData(getSessionKey(), artifact, finalComment, null,
                            null, null);
                    finishedUpdate = true;
                } catch (AxisFault e) {
                   QName faultCode = e.getFaultCode();
                   if (!faultCode.getLocalPart().equals("VersionMismatchFault")) {
                       throw e;
                   }
                }
            }
        }
        
        if (targetId.startsWith("artf")) {
            String finalComment = comment.replace("<TARGET>", originId);
            finalComment = finalComment.replace("<SOURCE>", targetId);
            boolean finishedUpdate = false;
            int numberOfTries = 0;
            while (!finishedUpdate && numberOfTries++ < 10) {   
                try {
                    ArtifactSoapDO artifact = trackerClient.getArtifactData(getSessionKey(), targetId);
                    trackerClient.setArtifactData(getSessionKey(), artifact, finalComment, null,
                            null, null);
                    finishedUpdate = true;
                } catch (AxisFault e) {
                   QName faultCode = e.getFaultCode();
                   if (!faultCode.getLocalPart().equals("VersionMismatchFault")) {
                       throw e;
                   }
                }
            }
        }
    }
}
