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

import java.util.ArrayList;
import jm.music.data.Note;
import jm.music.data.Part;
import jm.music.data.Phrase;
import jm.music.data.Score;


/**
 *
 * @author smuRf
 */
public class IPart implements BasicDataElement{
    private int defaultInstrument;
    private int instrument;
    private int channel;
    private ArrayList<IPhrase[]> phrases;
    private String name;
    private IScore parent;
    
    public IPart(String _name, int _instrument, int _channel)
    {
        name = "part";
        instrument = defaultInstrument = _instrument;
        channel = _channel;
        phrases = new ArrayList<>();
    }
    
    public void updateName(int id)
    {
        name+=id;
    }
    
    public void setParent(IScore iScore)
    {
        parent = iScore;
    }
    
    public IScore getParent()
    {
        return parent;
    }
            
    
    public String getName()
    {
        return name;
    }
    
    public int getChannel()
    {
        return channel;
    }
    
    public double getShortestPhraseStartTime()
    {
        double shortestStartTime = Double.MAX_VALUE;
        
        for(IPhrase[] iPhrase:phrases)
        {
            if(iPhrase[0].getStartTime() < shortestStartTime)
                shortestStartTime = iPhrase[0].getStartTime();
        }
        
        return shortestStartTime;
    }
    
    public void addIPhrase(IPhrase phrase)
    {
        phrase.updateName(phrases.size());
        phrase.setParent(this);
        IPhrase[] p = new IPhrase[2];
        p[0] = phrase;
        p[1] = phrase.normalize();
        phrases.add(p);
    }
    
    public IPhrase getOriginalIPhrase(int i)
    {
        return phrases.get(i)[0];
    }
    
    public IPhrase getNormalizedIPhrase(int i)
    {
        return phrases.get(i)[1];
    }
    
    public ArrayList<IPhrase> getOriginalIPhrases()
    {
        ArrayList<IPhrase> orgPhrases = new ArrayList();
        
        for(IPhrase[] iPhrase:phrases)
            orgPhrases.add(iPhrase[0]);
        
        return orgPhrases;
    }
    
    public int getDefaultInstrument()
    {
        return defaultInstrument;
    }
    
    public int getInstrument()
    {
        return instrument;
    }
    
    public void setInstrument(int _instrument)
    {
        instrument = _instrument;
    }
    
    public void setInstrumentToDefault()
    {
        instrument = defaultInstrument;
    }
    
    public Part getPart()
    {
        return new Part(name, defaultInstrument, channel);
    }
    
    @Override
    public Score getMusicStructure() {
        Score score = parent.getScore();
        Part part = getPart();
        
        double shortestStartTime = getShortestPhraseStartTime();

        for(IPhrase iPhrase:getOriginalIPhrases())
        {
            Phrase phrase = iPhrase.getPhrase();
            phrase.setStartTime(phrase.getStartTime() - shortestStartTime);

            for(INote iNote: iPhrase.getINotes())
            {
                Note note = iNote.getNote();
                phrase.add(note);
            }
            part.add(phrase);
        }
        score.add(part);

        return score;
    }
}
