package fr.eni.enchere.dal;

import fr.eni.enchere.bo.Retrait;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
@Primary
public class RetraitRepositoryImpl implements RetraitRepository {

    private JdbcTemplate jdbcTemplate;
    private NamedParameterJdbcTemplate namedJdbcTemplate;

    public RetraitRepositoryImpl(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
        this.namedJdbcTemplate = namedJdbcTemplate;
    }

    public class RetraitRowMapper implements RowMapper<Retrait> {

        @Override
        public Retrait mapRow(ResultSet rs, int rowNum) throws SQLException {
            Retrait retrait = new Retrait();
            retrait.setRue(rs.getString("rue"));
            retrait.setVille(rs.getString("ville"));
            retrait.setCode_postale(rs.getString("code_postal"));
            retrait.setNumArticle(rs.getInt("no_article"));
            return retrait;
        }
    }

    @Override
    public Retrait findById(int id) {
        return null;
    }

    @Override
    public Retrait findByNumArticle(int numArticle) {
        String sql = "select * from RETRAITS where no_article = ?";
        Retrait retrait = jdbcTemplate.queryForObject(sql, new RetraitRowMapper(), numArticle);
        if (retrait == null) {
            return null;
        } else {
            return retrait;
        }
    }

    @Override
    public void save(Retrait retrait) {
        String sqlIsUpdate = "SELECT COUNT(*) FROM RETRAITS WHERE no_article = :numArticle";
        MapSqlParameterSource parametersIsUpdate = new MapSqlParameterSource();
        parametersIsUpdate.addValue("numArticle", retrait.getNumArticle());
        int count = namedJdbcTemplate.queryForObject(sqlIsUpdate, parametersIsUpdate, Integer.class);
        if (count > 0) {
            //Mettre à jour les informations

        } else {
            //Ajouter le retrait
            String sql = "INSERT INTO RETRAITS (no_article, rue, code_postal, ville) VALUES (:no_article, :rue, :code_postal, :ville)";
            MapSqlParameterSource parameters = new MapSqlParameterSource();
            parameters.addValue("no_article", retrait.getNumArticle());
            parameters.addValue("rue", retrait.getRue());
            parameters.addValue("code_postal", retrait.getCode_postale());
            parameters.addValue("ville", retrait.getVille());
            namedJdbcTemplate.update(sql, parameters);
        }
    }

    @Override
    public void update(Retrait retrait) {
        String sql = "UPDATE RETRAITS " +
                "SET rue = ?, " +
                "code_postal = ?, " +
                "ville = ? " +
                "WHERE no_article = ?";
        jdbcTemplate.update(sql, retrait.getRue(), retrait.getCode_postale(), retrait.getVille(), retrait.getNumArticle());
    }
}
