import com.dvelop.d3.server.Document
import com.dvelop.d3.server.Entrypoint
import com.dvelop.d3.server.User
import com.dvelop.d3.server.core.D3Interface
import com.dvelop.d3.server.DocumentTypeAttribute;
import com.dvelop.d3.server.DocumentType;
import com.dvelop.d3.server.RepositoryField;

class Hook {

	// ValPasswdExit10
 	@Entrypoint(entrypoint="hook_val_passwd_exit_10")
	public int hook_val_passwd_exit_10(D3Interface d3, int errorCode, User user, String appLanguage, String appVersion) {
		
		d3.log.info("ValPasswdExit10: START ...");
		
		// ERV: Restriction-Set "ERV-Kostenstellen"
		d3.log.info("ValPasswdExit10: KST: user.id: '" + user.id + "'");
		d3.log.info("ValPasswdExit10: KST: user.email: '" + user.email + "'");
		
		def d3_email = user.email;
		def d3_username = user.id;
		
		def sqlQuery = "";
		
		List<Object> varArrayKST;
		def resultRowsKST;
		def resultAnzKST = 0;
		
		def resSetKostenstellen = "";
		def result = 0;
		
		
		// DEBUG:
		if (user.id == "ext_dvelo2") {
			d3_email = "ext_dvelo2@test.de"
		}
		
		d3.log.info("ValPasswdExit10: KST: d3_username: '" + d3_username + "'");
		d3.log.info("ValPasswdExit10: KST: d3_email: '" + d3_email + "'");
		d3.log.info("ValPasswdExit10: KST: user.id: '" + user.id + "'");
		
		// Benutzer in Tabelle 'erv_rights_kostenstellen" suchen und Kostenstellen auslesen
		sqlQuery = """
			SELECT kostenstelle as db_kostenstelle 
			FROM dbo.erv_rights_kostenstellen
			WHERE email = ?	
		""";
		
		varArrayKST = [d3_email];
		resultRowsKST = d3.sql.executeAndGet(sqlQuery, varArrayKST);
		resultAnzKST = resultRowsKST.size();
		
		if (resultAnzKST > 0) {
			
			resultRowsKST.each {kst ->
				
				if (resSetKostenstellen == "") {					
					resSetKostenstellen = kst.db_kostenstelle;					
				} else {				
					resSetKostenstellen = resSetKostenstellen + ";" + kst.db_kostenstelle;				
				}
				
			}					
			
			// Restriction-Set "ERV_Kostenstellen" schreiben
				
			d3.log.info("ValPasswdExit10: KST: resSetKostenstellen: '" + resSetKostenstellen + "'");
			
			// d3.log.info("ValPasswdExit10: KST: : Restriktionsmenge 'ERV_Kostenstellen' fuer User '" + user.realName + " [" + d3_email + "]' mit dem Filter '" + resSetKostenstellen + "' erstellt.");
			
			if (resSetKostenstellen) {
				
				result = d3.call.d3set_add_filter("Master", "", "ERV_Kostenstellen", d3_username, resSetKostenstellen, true)
				if (result != 0) {
					d3.log.error("ValPasswdExit10: KST: ERROR '" + result + "' in 'd3set_add_filter' | SET: 'ERV_Kostenstellen' | USER: '" + d3_email + "' | FILTER: '" + resSetKostenstellen + "'");
		
				}
				else {
					d3.log.info("ValPasswdExit10: KST: Restriktionsmenge 'ERV_Kostenstellen' fuer User '" + user.realName + " [" + d3_email + "]' mit dem Filter '" + resSetKostenstellen + "' erstellt.");
				}
			}
			
		}

	d3.log.info("ValPasswdExit10: STOP ...");		

		return 0
		
	}
	
	@Entrypoint( entrypoint = "hook_insert_entry_10" )
	public int hookInsertEntry10(D3Interface d3, User user, DocumentType docType, Document doc)
	{
		d3.log.info("START | hookInsertEntry10 ");
		
		////////////////////////////////////////
		// Validierung Kundennummer calb/d.velop 2025.12.08 START
		
		if ( isRepoIdInDocType( d3, docType.id(), 109 ) )
		{
			if ( doc.field[ 2 ] )
			{
				if ( doc.field[ 2 ].indexOf( "|" ) > 0 )
				{
					def werte = doc.field[ 2 ].tokenize( " | " );
					
					// Kundennummer
					if ( werte[ 0 ].length() > 0 )
					{
						doc.field[ 2 ] = werte[ 0 ];
					}
					
					// Kundenname
					if ( werte[ 1 ].length() > 0 )
					{
						doc.field[ 1 ] = werte[ 1 ];
					}
					
					// Land
					if ( werte[ 2 ].length() > 0 )
					{
						doc.field[ 4 ] = werte[ 2 ];
					}
				}
			}
		}
		
		// Validierung Kundennummer ENDE
		////////////////////////////////////////
		
		////////////////////////////////////////
		// Validierung Artikelnummer calb/d.velop 2025.12.08 START
		
		if ( isRepoIdInDocType( d3, docType.id(), 119 ) )
		{
			if ( doc.field[ 12 ] )
			{
				if ( doc.field[ 12 ].indexOf( "|" ) > 0 )
				{
					def werte = doc.field[ 12 ].tokenize( " | " );
					
					// Artikelnummer
					if ( werte[ 0 ].length() > 0 )
					{
						doc.field[ 12 ] = werte[ 0 ];
					}
					
					// Artikelname
					if ( werte[ 1 ].length() > 0 )
					{
						doc.field[ 13 ] = werte[ 1 ];
					}
					
				}
			}
		}
		
		// Validierung Artikelnummer ENDE
		////////////////////////////////////////
		
		////////////////////////////////////////
		// Validierung Mandant calb/d.velop 2025.12.08 START
		
		if ( isRepoIdInDocType( d3, docType.id(), 122 ) )
		{
			if ( doc.field[ 15 ] )
			{
				if ( doc.field[ 15 ].indexOf( "|" ) > 0 )
				{
					def werte = doc.field[ 15 ].tokenize( " | " );
					
					// Artikelnummer
					if ( werte[ 0 ].length() > 0 )
					{
						doc.field[ 15 ] = werte[ 0 ];
					}
					
					// Artikelname
					if ( werte[ 1 ].length() > 0 )
					{
						doc.field[ 19 ] = werte[ 1 ];
					}
					
				}
			}
		}
		
		// Validierung Mandant ENDE
		////////////////////////////////////////
		
		////////////////////////////////////////
		// Validierung Vertreter calb/d.velop 2025.12.08 START
		
		if ( isRepoIdInDocType( d3, docType.id(), 136 ) )
		{
			if ( doc.field[ 32 ] )
			{
				if ( doc.field[ 32 ].indexOf( "|" ) > 0 )
				{
					def werte = doc.field[ 32 ].tokenize( " | " );
					
					// Artikelnummer
					if ( werte[ 0 ].length() > 0 )
					{
						doc.field[ 32 ] = werte[ 0 ];
					}
					
					// Artikelname
					if ( werte[ 1 ].length() > 0 )
					{
						doc.field[ 35 ] = werte[ 1 ];
					}
				}
			}
		}
		
		// Validierung Vertreter ENDE
		////////////////////////////////////////
		
		
		d3.log.info("ENDE | hookInsertEntry10 ");
		return 0;
	}
	
	
	
	
	
	
	
	
	private boolean isRepoIdInDocType( D3Interface d3, String docTypeShort, int repoId )
	{
		boolean isRepoIdInDocType = false;
		try
		{
			Document docTemp = d3.archive.newDocument();
			docTemp.setType( docTypeShort );
			DocumentType docType = docTemp.getType();
			if ( docTemp != null && docType != null )
			{
				for ( int i = 1; i <= 89; i++ )
				{
					try
					{
						DocumentTypeAttribute docTypeAttrib = docTemp.getType().getField().getAt( i );
						if ( docTypeAttrib != null )
						{
							RepositoryField repoField = docTypeAttrib.getRepositoryField();
							if ( repoField != null )
							{
								String currRepoId = repoField.getId();
								if ( currRepoId != null )
								{
									int currRepoIdInt = Integer.valueOf( currRepoId );
									if ( currRepoIdInt == repoId )
									{
										isRepoIdInDocType = true;
										break;
									}
								}
							}
						}
					}
					catch ( Exception eField )
					{
						//nix
					}
				}
			}
		}
		catch ( Exception e )
		{
			// wieder nix
		}
		
		//d3.log.error( "Nachricht: " + isRepoIdInDocType );
		
		return isRepoIdInDocType;
	}

	
	
}