package com.naaturel.ANN.domain.model.neuron;
import com.naaturel.ANN.domain.abstraction.ActivationFunction;
import com.naaturel.ANN.domain.abstraction.Model;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class Neuron implements Model {

    protected List<Synapse> synapses;
    protected Bias bias;
    protected ActivationFunction activationFunction;

    public Neuron(List<Synapse> synapses, Bias bias, ActivationFunction func){
        this.synapses = synapses;
        this.bias = bias;
        this.activationFunction = func;
    }

    public void updateBias(Weight weight) {
        this.bias.setWeight(weight.getValue());
    }

    public void updateWeight(int index, Weight weight) {
        this.synapses.get(index).setWeight(weight.getValue());
    }

    protected void setInputs(List<Input> inputs){
        for(int i = 0;  i < inputs.size() && i < synapses.size(); i++){
            Synapse syn = this.synapses.get(i);
            syn.setInput(inputs.get(i));
        }
    }

    @Override
    public int synCount() {
        return this.synapses.size()+1; //take the bias in account
    }

    @Override
    public List<Float> predict(List<Input> inputs) {
        this.setInputs(inputs);
        return List.of(activationFunction.accept(this));
    }

    @Override
    public void forEachNeuron(Consumer<Neuron> consumer) {
        consumer.accept(this);
    }

    @Override
    public void forEachSynapse(Consumer<Synapse> consumer) {
        consumer.accept(this.bias);
        this.synapses.forEach(consumer);
    }

    public float calculateWeightedSum() {
        float res = 0;
        res += this.bias.getWeight() * this.bias.getInput();
        for(Synapse syn : this.synapses){
            res += syn.getWeight() * syn.getInput();
        }
        return res;
    }

}
