package siprp.update.updates;

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

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;
import com.evolute.utils.sql.Update;

public class V11_9_To_V12_0 extends AbstractUpdate
{
	
	public V11_9_To_V12_0()
	{
		super(11.9,12.0,"A actualizar informacao dos tecnicos de higiene e seguranca");
	}
	
	@Override
	public void doUpdate() throws Exception
	{
		Executer executer = getExecuter();
		
		String nomes[] = new String[]{
				"David Afonso",
				"B\u00e1rbara Gon\u00e7alves",
				"Ana Sofia Jorge"
				};
		Integer ids[] = new Integer[]{ 
				3,
				4,
				5
				};
		String caps[] = new String[]{ 
				"28291002EC3", 
				"em emiss\u00e3o - A 100 60 1820 D", 
				"17050910EC5" };
		String assinaturas[] = new String[]{ 
				null,
				"siprp/update/updates/V11_9_To_V11_10_Barbara_Goncalves.png",
				"siprp/update/updates/V11_9_To_V11_10_Ana_Sofia_Jorge.png" };

		for( int n = 0; n < ids.length; n++ )
		{
			Vector<Assignment> assignments = new Vector<Assignment>();
			assignments.add( new Assignment( "cap", caps[ n ] ) );
			if( assinaturas[ n ] != null )
			{
				Integer assinaturaID = null;
				String assinatura = "Assinatura(" + nomes[ n ] + ")";
				BufferedImage assinaturaImage = ImageIO.read( getClass().getClassLoader().getResourceAsStream( 
						assinaturas[ n ] ) );
				ByteArrayOutputStream assinaturaBytesStream = new ByteArrayOutputStream();
				ImageIO.write( assinaturaImage, "PNG", assinaturaBytesStream );
				if( assinaturaImage == null || assinaturaBytesStream.size() == 0 )
				{
					throw new Exception("Erro ao ler assinatura: " + assinatura );
				}
				Insert insertQuery = new Insert( "image", new Assignment[]{
						new Assignment(new Field("name"),assinatura)
				} );
				Virtual2DArray array = executer.executeQuery( insertQuery );
				Select2 selectQuery = new Select2(new String[]{"image"},null,null,new String[]{"id"},new Field("name").isEqual( assinatura ),null,null,null,null);
				array = executer.executeQuery( selectQuery );
				if( array.columnLength() == 1 )
				{
					assinaturaID = (Integer) array.get( 0, 0 );
				}
				if( assinaturaID == null )
				{
					throw new Exception("Erro ao inserir assinatura: " + assinatura );
				}
				BlobUpdate blobUpdate = new BlobUpdate("image","image_data",assinaturaBytesStream.toByteArray(),new Field("id").isEqual( assinaturaID ) );
				executer.executeQuery( blobUpdate );
				assignments.add( new Assignment( "assinatura", assinaturaID ) );
			}
			Update update =
				new Update( "marcacoes_tecnicos_hst",
					assignments.toArray( new Assignment[ assignments.size() ] ),
					new Field( "id" ).isEqual( ids[ n ] ) );
			executer.executeQuery( update );
		}
	}
	
}
