package core.processor.impl;

import com.sun.net.httpserver.HttpExchange;
import core.db.repositories.CarRepository;
import core.models.Car;
import core.processor.Processor;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class CarsProcessor extends Processor
{
    private final CarRepository repository;

    public CarsProcessor() throws SQLException
    {
        this.repository = new CarRepository();
    }

    @Override
    public void process(HttpExchange exchange) throws SQLException, IOException
    {
        String template = reader.read("cars");
        String partial = reader.read("car");

        StringBuilder builder = new StringBuilder();

        List<Car> cars = repository.get();
        for (Car car : cars)
        {
            builder.append(partial.formatted
            (
                    car.getId(),
                    car.getName(),
                    car.getType(),
                    car.getColor(),
                    car.getColor(),
                    car.getVolume()
            ));
        }

        process(exchange, template.formatted(builder.toString()));
    }
}
