package siprp.update.updates;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;

import com.evolute.module.updater.AbstractUpdate;
import com.evolute.utils.arrays.Virtual2DArray;
import com.evolute.utils.db.Executer;
import com.evolute.utils.sql.Assignment;
import com.evolute.utils.sql.BlobUpdate;
import com.evolute.utils.sql.Field;
import com.evolute.utils.sql.Insert;
import com.evolute.utils.sql.Select2;

public class V11_8_To_V11_9 extends AbstractUpdate
{
	
	public V11_8_To_V11_9()
	{
		super(11.8,11.9,"A actualizar informacao dos tecnicos de higiene e seguranca");
	}
	
	@Override
	public void doUpdate() throws Exception
	{
		Executer executer = getExecuter();
		
		Integer assDavidID = null;
		String assDavid = "Assinatura(David Afonso)";
		BufferedImage assDavidImage = ImageIO.read( getClass().getClassLoader().getResourceAsStream( "siprp/update/updates/V11_8_To_V11_9_David_Afonso.png" ) );
		ByteArrayOutputStream assDavidBytesStream = new ByteArrayOutputStream();
		ImageIO.write( assDavidImage, "PNG", assDavidBytesStream );
		if( assDavidImage == null || assDavidBytesStream.size() == 0 )
		{
			throw new Exception("Erro ao ler assinatura: " + assDavid );
		}
		Insert insertQuery = new Insert( "image", new Assignment[]{
				new Assignment(new Field("name"),assDavid)
		} );
		Virtual2DArray array = executer.executeQuery( insertQuery );
		Select2 selectQuery = new Select2(new String[]{"image"},null,null,new String[]{"id"},new Field("name").isEqual( assDavid ),null,null,null,null);
		array = executer.executeQuery( selectQuery );
		if( array.columnLength() == 1 )
		{
			assDavidID = (Integer) array.get( 0, 0 );
		}
		if( assDavidID == null )
		{
			throw new Exception("Erro ao inserir assinatura: " + assDavid );
		}
		BlobUpdate blobUpdate = new BlobUpdate("image","image_data",assDavidBytesStream.toByteArray(),new Field("id").isEqual( assDavidID ) );
		executer.executeQuery( blobUpdate );
		com.evolute.utils.sql.Update update = new com.evolute.utils.sql.Update(
					"update marcacoes_tecnicos_hst set cap='em emiss\u00e3o', formacao='', assinatura=" + assDavidID + " where id = 3; "
		);
		executer.executeQuery( update );
	}
	
}
