#include <Layer.h>
#include <QGraphicsView>

Layer::Layer()
{
    this->type = HIDDEN;
    this->xPos = 0.0;
    hBias = Neuron(HBIAS, xPos, 100.0, 0.0);
}

Layer::Layer(int type, float x)
{
    this->type = type;
    this->xPos = x;

    if(type == HIDDEN)
    {
        hBias = Neuron(HBIAS, xPos, 100, 1.0);
        hBias.setBias(1.0);
    }
    else if(type == INPUT)
    {
        iBias = Neuron(IBIAS, xPos, 100, 1.0);
        iBias.setBias(1.0);
    }
}

Layer::~Layer()
{

}

void Layer::draw(QGraphicsScene *scene)
{
    foreach(Neuron *n, neurons)
    {
        n->draw(scene);
    }

    if(type == HIDDEN)
    {
        hBias.draw(scene);
    }

    if(type == INPUT)
    {
        iBias.draw(scene);
    }
}

void Layer::addNeuron(QGraphicsView *v)
{
    if(this->type == INPUT)
    {
        if(neurons.length() >= 2)
        {
            return;
        }
        else
        {
            switch(neurons.length())
            {
            case 0:
            {
                Neuron *n = new Neuron(INPUT, v->geometry().left() + 150, 200, 0.01);
                neurons.append(n);
            }
                break;
            case 1:
            {
                Neuron *nn = new Neuron(INPUT, v->geometry().left() + 150, 300, 0.0);
                neurons.append(nn);
            }
                break;
            }
        }
    }
    else if(this->type == OUTPUT)
    {
        if(neurons.length() >= 1)
        {
            return;
        }
        else
        {
            Neuron *n = new Neuron(OUTPUT, this->xPos, 100, 0.04);
            neurons.append(n);
        }
    }
    else if(this->type == HIDDEN)
    {
        float y = 100 + ((neurons.length()+1)*100);
        Neuron *n = new Neuron(HIDDEN, this->xPos, y, 0.0);
        neurons.append(n);
    }
}

Neuron Layer::getBias()
{
    if(this->type == INPUT)
    {
        return iBias;
    }
    else if(this->type == HIDDEN)
    {
        return hBias;
    }
}

void Layer::testMode()
{
    neurons[0]->setWeight(0.04);
    neurons[1]->setWeight(0.1);
}
