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

import data.IScore;
import gui.ComposingDialog;
import java.util.Random;
import jm.music.data.Note;
import jm.music.data.Phrase;
import jm.music.data.Score;
import jm.music.tools.Mod;
import midi.MIDIAnalyser;

/**
 *
 * @author smuRf
 */
public class Composer {
    protected Score score = null;
    protected ComposingDialog parent;
    
    public IScore getIScore()
    {
        MIDIAnalyser analyser = new MIDIAnalyser();
        IScore iScore = analyser.analyseTask(score);
        
        return iScore;
    }
    
    public Phrase createAnswerPhrase(Phrase base)
    {
        Phrase answer = base.copy();
        Mod.transpose(answer, -12);
        
        return answer;
    }
    
    public Phrase createDrumPhrase(Phrase base, int drum)
    {
        Phrase drums = new Phrase();
        drums.add(new Note(drum, 1.0));
        Mod.repeat(drums, (int)base.getBeatLength() / 480);
        
        return drums;
    }
    
    public Phrase createCounterPhrase(Phrase base)
    {
        Random rnd = new Random();
        Phrase counter = new Phrase();
        
        Note[] noteArray = base.getNoteArray();
        
        for(int i = 0; i < noteArray.length; i++)
        {
            if(i + 1 < noteArray.length)
            {
                double prop = rnd.nextDouble();
                if(noteArray[i].getRhythmValue() == 0.5 && noteArray[i + 1].getRhythmValue() == 0.5)
                {
                    if(prop < 0.5)
                        counter.add(new Note(noteArray[i].getPitch(), 1.0));
                    else
                    {
                        counter.add(new Note(noteArray[i].getPitch(), 0.25)); 
                        counter.add(new Note(noteArray[i + 1].getPitch(), 0.25)); 
                        counter.add(new Note(noteArray[i].getPitch(), 0.25)); 
                        counter.add(new Note(noteArray[i + 1].getPitch(), 0.25)); 
                    }
                    i++;
                }
                else if(noteArray[i].getRhythmValue() == 0.5 && noteArray[i + 1].getRhythmValue() != 0.5)
                {
                    counter.add(new Note(noteArray[i].getPitch(), 0.5));
                }
            }
            if(noteArray[i].getRhythmValue() == 1.0)
            {
                counter.add(new Note(noteArray[i].getPitch(), 0.5));
                counter.add(new Note(noteArray[i].getPitch(), 0.5));
            }
            if(noteArray[i].getRhythmValue() == 2.0)
            {
                counter.add(new Note(noteArray[i].getPitch(), 1.0));
                counter.add(new Note(noteArray[i].getPitch(), 1.0));
            }
            if(noteArray[i].getRhythmValue() == 4.0)
            {
                double prop = rnd.nextDouble();
                
                if(prop < 0.5)
                {
                    counter.add(new Note(noteArray[i].getPitch(), 2.0));
                    counter.add(new Note(noteArray[i].getPitch(), 2.0));
                }
                else
                {
                    counter.add(new Note(noteArray[i].getPitch(), 1.0));
                    counter.add(new Note(noteArray[i].getPitch(), 1.0));
                    counter.add(new Note(noteArray[i].getPitch(), 1.0));
                    counter.add(new Note(noteArray[i].getPitch(), 1.0));
                }
            }
        }
        
        Mod.transpose(counter, 12);
        
        return counter;
    }
    
    public void muteCounterPhrase(boolean mute)
    {
        if(score != null)
            score.getPart(2).getPhrase(0).setMute(mute);
    }
    
    public void muteAnswerPhrase(boolean mute)
    {
        if(score != null)
            score.getPart(1).getPhrase(0).setMute(mute);
    }
    
    public void muteDrumPhrase(boolean mute)
    {
        if(score != null)
            score.getPart(3).getPhrase(0).setMute(mute);
    }
    
    public void setTempo(double tempo)
    {
        if(score != null)
            score.setTempo(tempo);
    }
    
    public void setPhraseInstrument()
    {
        if(score != null)
            score.getPart(0).setInstrument(parent.getPhraseInstrument());
    }
    
    public void setAnswerInstrument()
    {
        if(score != null)
            score.getPart(1).setInstrument(parent.getAnswerPhraseInstrument());
    }
    
    public void setCounterInstrument()
    {
        if(score != null)
            score.getPart(2).setInstrument(parent.getCounterPhraseInstrument());
    }
    
    public void setDrumInstrument()
    {
        if(score != null)
        {
            score.getPart(3).removeAllPhrases();
            Phrase drums = createDrumPhrase(score.getPart(0).getPhrase(0), parent.getDrumPhraseInstrument());
            for(Note n:drums.getNoteArray())
            {
                n.setRhythmValue(n.getRhythmValue() * 480);
                n.setDynamic(80);
            }
            score.getPart(3).add(drums);
        }
    }
}
