package com.atolcd.parapheur.repo.impl;

import com.atolcd.parapheur.model.ParapheurModel;
import com.atolcd.parapheur.repo.EtapeCircuit;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.alfresco.model.ContentModel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;

public class EtapeCircuitImpl implements EtapeCircuit {

    private NodeRef nodeRef;

    private NodeRef parapheur;

    private NodeRef delegateur;

    private String delegateurName;

    private boolean approved;

    private String transition; /* PARAPHEUR, CHEF_DE */

    private String actionDemandee; /* 1-VISA, 2-SIGNATURE, 3-TDT, 4-DIFF_EMAIL, 5-ARCHIVAGE */

    private Set<NodeRef> listeNotification;

    private String annotation;

    private String annotationPrivee;

    private String signataire;

    private Date dateValidation;

    private String signature;

    private String signatureEtape;
    //private String notificationsExternes;

    public EtapeCircuitImpl() {
        this.nodeRef = null;
        this.parapheur = null;
        this.approved = false;
        this.transition = null;
        this.actionDemandee = null;
        this.listeNotification = null;
    }

    public EtapeCircuitImpl(NodeService nodeService, NodeRef nodeRef) {
        this.nodeRef = nodeRef;

        Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
        this.parapheur = (NodeRef) properties.get(ParapheurModel.PROP_PASSE_PAR);
        this.delegateur = (NodeRef) properties.get(ParapheurModel.PROP_DELEGATEUR);
        if (this.delegateur != null) {
            this.delegateurName = (String) properties.get(ContentModel.PROP_NAME);
        } else {
            this.delegateurName = null;
        }
        Boolean bApproved = (Boolean) properties.get(ParapheurModel.PROP_EFFECTUEE);
        if (bApproved != null) {
            this.approved = bApproved;
        }
        this.actionDemandee = (String) properties.get(ParapheurModel.PROP_ACTION_DEMANDEE);
        List<NodeRef> tListeNotifs = (List<NodeRef>) properties.get(ParapheurModel.PROP_LISTE_NOTIFICATION);
        if (tListeNotifs != null) {
            this.listeNotification = new HashSet<NodeRef>(tListeNotifs);
        } else {
            this.listeNotification = null;
        }
        this.annotation = (String) properties.get(ParapheurModel.PROP_ANNOTATION);
        this.annotationPrivee = (String) properties.get(ParapheurModel.PROP_ANNOTATION_PRIVEE);
        this.signataire = (String) properties.get(ParapheurModel.PROP_SIGNATAIRE);
        this.dateValidation = (Date) properties.get(ParapheurModel.PROP_DATE_VALIDATION);
        this.signature = (String) properties.get(ParapheurModel.PROP_SIGNATURE);
        this.signatureEtape = (String) properties.get(ParapheurModel.PROP_SIGNATURE_ETAPE);
    }

    public NodeRef getNodeRef() {
        return nodeRef;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#getParapheur()
     */
    public NodeRef getParapheur() {
        return this.parapheur;
    }

    public NodeRef getDelegateur() {
        return this.delegateur;
    }

    public String getDelegateurName() {
        return this.delegateurName;
    }

    public void setParapheur(NodeRef noderef) {
        this.parapheur = noderef;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#isApproved()
     */
    public boolean isApproved() {
        return this.approved;
    }

    public String getTransition() {
        return this.transition;
    }

    public void setTransition(String str) {
        this.transition = str;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#getActionDemandee()
     */
    public String getActionDemandee() {
        return this.actionDemandee;
    }

    public void setActionDemandee(String str) {
        this.actionDemandee = str;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#getListeDiffusion()
     */
    @Override
    public Set<NodeRef> getListeNotification() {
        /*
        if (this.listeNotification.trim().isEmpty())
        {
        return null;
        }
        Set<NodeRef> res = new HashSet<NodeRef>();
        if (this.listeNotification.endsWith(","))   // nettoyage du ',' final
        this.listeNotification.substring(0, this.listeNotification.length());
        String[] tab = this.listeNotification.split(",");
        for (String str : tab)
        res.add(new NodeRef(str));*/
        return this.listeNotification;
    }

    /**
     *
     * @param notifiables
     */
    public void setListeDiffusion(Set<NodeRef> notifiables) {
        this.listeNotification = notifiables;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#setAndRecordListeDiffusion(Set<NodeRef> liste)
     */
    public void setAndRecordListeDiffusion(NodeService nodeService, Set<NodeRef> liste) {
        this.listeNotification = liste;
        if (this.nodeRef != null) {
            nodeService.setProperty(nodeRef, ParapheurModel.PROP_LISTE_DIFFUSION, (Serializable) liste);
        }
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#getAnnotation()
     */
    public String getAnnotation() {
        return this.annotation;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#getAnnotationPrivee()
     */
    public String getAnnotationPrivee() {
        return this.annotationPrivee;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#getSignataire()
     */
    public String getSignataire() {
        return this.signataire;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#getDateValidation()
     */
    public Date getDateValidation() {
        return this.dateValidation;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#getSignature()
     */
    public String getSignature() {
        return signature;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#getSignatureElectronique()
     */
    public String getSignatureEtape() {
        return signatureEtape;
    }

    /**
     * @see com.atolcd.parapheur.repo.EtapeCircuit#getNotificationsExternes()
     */
    @Override
    public String getNotificationsExternes() {
        // Pour l'instant, ca sert a rien
        return null;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }

        if (!(o instanceof EtapeCircuitImpl)) {
            return false;
        }
        
        EtapeCircuitImpl etape = (EtapeCircuitImpl) o;
        return etape.nodeRef.equals(this.nodeRef);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 41 * hash + (this.nodeRef != null ? this.nodeRef.hashCode() : 0);
        return hash;
    }
    
}
