package com.evolute.siprp.client.panels.apps.AnaliseAcidentesTrabalho.seguimento;

import java.util.Vector;

import com.evolute.siprp.client.panels.apps.AnaliseAcidentesTrabalho.AnaliseAcidentesTrabalhoService;
import com.evolute.siprp.client.panels.apps.AnaliseAcidentesTrabalho.AnaliseAcidentesTrabalhoServiceAsync;
import com.evolute.siprp.client.panels.utils.navigation.PageNavigation;
import com.evolute.siprp.client.vo.ActualTableRow;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.FlexTable;

public class SeguimentoTable extends FlexTable 
{
	private final DateTimeFormat D_F = DateTimeFormat.getFormat( "yyyy-MM-dd" );
	
	private AnaliseAcidentesTrabalhoServiceAsync aatService;
	
	public SeguimentoTable()
	{
		super();
		
		this.aatService = ( AnaliseAcidentesTrabalhoServiceAsync ) GWT.create( AnaliseAcidentesTrabalhoService.class );
		ServiceDefTarget serviceDef = ( ServiceDefTarget ) this.aatService;
		serviceDef.setServiceEntryPoint( GWT.getModuleBaseURL() + "aatService" );
	
		this.setText( 0, 0, "Data do acidente" );
		this.setText( 0, 1, "Data da abertura" );
		this.setText( 0, 2, "Nº acidente" );
		this.setText( 0, 3, "POR" );
		this.setText( 0, 4, "Nome do acidentado" );
		this.setText( 0, 5, "Fase" );
		
		CellFormatter cellFormatter = this.getCellFormatter();
		cellFormatter.setStyleName( 0, 0, "lblBold" );
		cellFormatter.setStyleName( 0, 1, "lblBold" );
		cellFormatter.setStyleName( 0, 2, "lblBold" );
		cellFormatter.setStyleName( 0, 3, "lblBold" );
		cellFormatter.setStyleName( 0, 4, "lblBold" );
		cellFormatter.setStyleName( 0, 5, "lblBold" );
		
		this.setText( 1, 0, "loading ..." );
		this.getFlexCellFormatter().setColSpan( 1, 0, 6 );
		
		this.aatService.getSeguimentoTableData( PageNavigation.getProvider().getUserLogged(), new AsyncCallback< Vector< ActualTableRow > >() {
			@Override
			public void onFailure( Throwable caught ) 
			{
				Window.alert( "Oops ! unable to fetch 'Actual' table data." );
			}

			@Override
			public void onSuccess( Vector< ActualTableRow > result ) 
			{
				showTableData( result );
			}
		});
	}
	
	
	private void showTableData( Vector< ActualTableRow > tableRows )
	{
		if ( tableRows == null || tableRows.size() == 0 )
		{
			this.setText( 1, 0, "No items found." );
		}
		else 
		{
			this.getFlexCellFormatter().setColSpan( 1, 0, 0 );
			
			for ( int rowNumber = 0, columnNumber = 0; rowNumber < tableRows.size(); rowNumber++, columnNumber = 0 ) 
			{
				ActualTableRow row = tableRows.get( rowNumber );
				
				this.setText( (rowNumber+1), columnNumber++, D_F.format( row.getDataAcidente() ) );
				this.setText( (rowNumber+1), columnNumber++, D_F.format( row.getDataAbertura() ) );
				this.setText( (rowNumber+1), columnNumber++, row.getNrAcidente() );
				this.setText( (rowNumber+1), columnNumber++, row.getPOR() );
				this.setText( (rowNumber+1), columnNumber++, row.getNomeAcidentado() );
				this.setText( (rowNumber+1), columnNumber++, String.valueOf( row.getFase() ) );
			}
		}
	}
	
}
