package com.utopiaindustries.dao.ctp;

import com.utopiaindustries.model.ctp.CutPieceType;
import com.utopiaindustries.util.KeyHolderFunctions;
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.util.ArrayList;
import java.util.List;

@Repository
public class CutPieceTypeDAO {

	private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	private final String TABLE_NAME = "cut_to_pack.cut_piece_type";
	private final String SELECT_QUERY = String.format( "SELECT * FROM %s WHERE id = :id", TABLE_NAME );
	private final String SELECT_ALL_QUERY = String.format( "SELECT * FROM %s ORDER BY id DESC", TABLE_NAME );
	private final String DELETE_QUERY = String.format( "DELETE FROM %s WHERE id = :id", TABLE_NAME );
	private final String INSERT_QUERY = String.format( "INSERT INTO %s (id, title) VALUES (:id, :title) ON DUPLICATE KEY UPDATE title = VALUES(title)", TABLE_NAME );

	public CutPieceTypeDAO(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
		this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
	}

	// prepare query params
	private MapSqlParameterSource prepareInsertQueryParams( CutPieceType cutPieceType ) {
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue( "id", cutPieceType.getId() )
			.addValue( "title", cutPieceType.getTitle() );
		return params;
	}

	// find
	public CutPieceType find( long id ) {
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue( "id", id );
		return namedParameterJdbcTemplate.query( SELECT_QUERY, params, new CutPieceTypeRowMapper() )
				.stream()
				.findFirst()
				.orElse( new CutPieceType() );
	}

	// find all
	public List<CutPieceType> findAll() {
		return namedParameterJdbcTemplate.query( SELECT_ALL_QUERY, new CutPieceTypeRowMapper() );
	}

	// save
	public long save( CutPieceType cutPieceType ) {
		KeyHolder keyHolder = new GeneratedKeyHolder();
		MapSqlParameterSource params = prepareInsertQueryParams( cutPieceType );
		namedParameterJdbcTemplate.update( INSERT_QUERY, params, keyHolder );
		return KeyHolderFunctions.getKey( cutPieceType.getId(), keyHolder );
	}

	// save all
	public int[] saveAll( List<CutPieceType> cutPieceTypes ) {
		List<MapSqlParameterSource> batchArgs = new ArrayList<>();
		for ( CutPieceType cutPieceType: cutPieceTypes ) {
			MapSqlParameterSource params = prepareInsertQueryParams( cutPieceType );
			batchArgs.add( params );
		}
		return namedParameterJdbcTemplate.batchUpdate( INSERT_QUERY, batchArgs.toArray(new MapSqlParameterSource[cutPieceTypes.size()]) );
	}

	// delete
	public boolean delete( long id ) {
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue( "id", id );
		return namedParameterJdbcTemplate.update( DELETE_QUERY, params ) > 0;
	}
}