/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

import java.util.ArrayList;
import jm.music.data.Score;

/**
 *
 * @author smuRf
 */
public class IMotif implements BasicDataElement{
    private IScore iScore;
    private int srcPhraseId;
    private int srcPartId;
    private int srcNoteId;
    private String type = "";
    private ArrayList<IMotif> equiMotifs = new ArrayList();
    
    public IMotif(IScore _iScore, int phraseId, int partId, int noteId)
    {
        iScore = _iScore;
        srcPhraseId = phraseId;
        srcPartId = partId;
        srcNoteId = noteId;
    }
    
    public void setMotifType(String type)
    {
        this.type = type;
    }
    
    public String getMotifType()
    {
        return type;
    }
    
    public void addEquiMotif(IMotif motif)
    {
        equiMotifs.add(motif);
    }
    
    public boolean equals(ArrayList<INote> cmpMotifNotes)
    {
        boolean ret = true;
        
        if(cmpMotifNotes.size() != getMotifLength())
            return false;
        
        for(int i = 0; i < getMotifLength(); i++)
        {
            INote iNote = iScore.getIPart(0).getNormalizedIPhrase(0).getINote(i);
            if(iNote.getIPitch() != cmpMotifNotes.get(i).getIPitch())
                ret = false;
            if(iNote.getRhythmValue() != cmpMotifNotes.get(i).getRhythmValue())
                ret = false;
        }
        
        return ret;
    }
    
    public boolean equals(IMotif motif)
    {
        ArrayList<INote> cmpMotifNotes = motif.getScore().getIPart(0).getOriginalIPhrase(0).getINotes();
        
        return equals(cmpMotifNotes);
    }
    
    public boolean containsEquiMotif(IMotif motif)
    {
        boolean ret = false;
        
        for(IMotif equiMotif:equiMotifs)
        {
            if(equiMotif.equals(motif))
            {
                ret = true;
                break;
            }
        }
        return ret;
    }
    
    public ArrayList<IMotif> getEquiMotifs()
    {
        return equiMotifs;
    }
    
    public IMotif getEquiMotif(int id)
    {
        return equiMotifs.get(id);
    }
    
    public String getName()
    {
        return iScore.getName();
    }
    
    public double getTempo()
    {
        return iScore.getTempo();
    }
    
    public int getDefaultInstrument()
    {
        return iScore.getIPart(0).getDefaultInstrument();
    }
    
    public ArrayList<INote> getNotes()
    {
        return iScore.getIPart(0).getNormalizedIPhrase(0).getINotes();
    }
    
    public IScore getScore()
    {
        return iScore;
    }
    
    public int getEquiMotifCount()
    {
        return equiMotifs.size();
    }
    
    public int getSrcPhraseId()
    {
        return srcPhraseId;
    }
    
    public int getSrcPartId()
    {
        return srcPartId;
    }
    
    public int getSrcNoteId()
    {
        return srcNoteId;
    }
    
    public IScore getIScore()
    {
        return iScore;
    }
    
    public int getMotifLength()
    {
        return iScore.getIPart(0).getOriginalIPhrase(0).getINotes().size();
    }

    @Override
    public Score getMusicStructure() {
        return iScore.getMusicStructure();
    }
}
