#include "local-key-item.hh"
#include "molecule.hh"
#include "scalar.hh"
#include "lookup.hh"
#include "paper-def.hh"
#include "musical-request.hh"
#include "notehead.hh"
#include "misc.hh"



Local_key_item::Local_key_item(int i)
{
    c0_position  = i;
}

void
Local_key_item::add(Item*head_l)
{
    support_items_.push(head_l);
    add_dependency(head_l);
}

void
Local_key_item::add(Melodic_req*m_l)
{
    add(m_l->octave_i_, m_l->notename_i_, m_l->accidental_i_);
}
void
Local_key_item::add (int o, int p , int a)
{
    Local_acc l;
    l.octave = o;
    l.name = p;
    l.acc = a;
    accs.push(l);
}

void
Local_key_item::do_pre_processing()
{
    accs.sort(Local_acc::compare);
}

Molecule*
Local_key_item::brew_molecule_p()const
{
    Molecule* output = new Molecule;
    Molecule*octmol = 0;
    int lastoct = -100;
    for  (int i = 0; i <  accs.size(); i++) {
	// do one octave
	if (accs[i].octave != lastoct) {
	    if (octmol){
		Real dy =lastoct*7*paper()->internote();
		octmol->translate(Offset(0, dy));
		output->add(*octmol);
		delete octmol;
	    }
	    octmol= new Molecule;
	}
	lastoct = accs[i].octave;
	Symbol s =paper()->lookup_l()->accidental(accs[i].acc);   
	Atom a(s);
	Real dy = (accs[i].name + c0_position) * paper()->internote();
	a.translate(Offset(0,dy));

	octmol->add_right(a);
    }

    if (octmol){
	Real dy =lastoct*7*paper()->internote();
	octmol->translate(Offset(0, dy));
	output->add(*octmol);
	delete octmol;
    }

    Interval head_width=itemlist_width(support_items_);
    output->translate(Offset(-output->extent().x.right + head_width.left ,0));
    
    return output;
}

int
Local_acc::compare(Local_acc&a, Local_acc&b)
{
    if (a.octave - b.octave)
	return a.octave - b.octave;
    if (a.name - b.name)
	return a.name - b.name;
    
    assert(false);
};
IMPLEMENT_STATIC_NAME(Local_key_item);
