package stego.StegoCaller;

import StegoGUI.JPanel_AddCarrierImagePath_OR_UploadDestination;
import StegoGUI.StegoFileSelection;
import java.io.File;
import java.util.ArrayList;
import stego.Drivers;
import stego.StegoCreator.StegoEmbedder;

// Referenced classes of package stego.StegoCaller:
//            Caller_Embedder_JAR

public class Caller_StegoEmbedder
{

    String strMyClassName;

    public Caller_StegoEmbedder(StegoFileSelection par, File encryptionJAR, boolean encryptSourceData, int encryptionKeySize, File fleGeneratedEncryptionKey, boolean importExternal_JAR, File fleExternal_JAR, 
            boolean useSourceFile, boolean useSourceText, File fleSourceFile, String strSourceText, String strBatchIdentifier, ArrayList alCarrierImages, File carrierImageSaveDirectory)
    {
        strMyClassName = "Caller_StegoEmbedder";
        try
        {
            if(useSourceFile && (fleSourceFile == null || !fleSourceFile.exists() || !fleSourceFile.isFile()) || useSourceText && (strSourceText == null || strSourceText.trim().equals("")) || alCarrierImages == null || alCarrierImages.size() < 1 || strBatchIdentifier == null || strBatchIdentifier.trim().equals("") || strBatchIdentifier.length() != 4)
            {
                Drivers.jop_Error("Invalid data received in Caller_StegoEmbedder.java", "Cannot continue");
                throw new Exception("Data Validation failed.  Recommend checking each data type received to find the " +
"culprit"
);
            }
            if(useSourceFile)
            {
                callStegoEmbedder_SourceFile(par, encryptionJAR, encryptSourceData, encryptionKeySize, fleGeneratedEncryptionKey, importExternal_JAR, fleExternal_JAR, useSourceFile, fleSourceFile, false, strSourceText, strBatchIdentifier, alCarrierImages, carrierImageSaveDirectory);
            } else
            if(useSourceText)
            {
                if(strSourceText == null || strSourceText.trim().equals(""))
                {
                    Drivers.jop_Error("Invalid data received in Caller_StegoEmbedder.java", "Cannot continue");
                    throw new Exception("Data Validation failed.  Recommend checking each data type received to find the " +
"culprit"
);
                }
                callStegoEmbedder_SourceFile(par, encryptionJAR, encryptSourceData, encryptionKeySize, fleGeneratedEncryptionKey, importExternal_JAR, fleExternal_JAR, false, fleSourceFile, useSourceText, strSourceText, strBatchIdentifier, alCarrierImages, carrierImageSaveDirectory);
            } else
            {
                Drivers.jop_Error("Invalid data received in Caller_StegoEmbedder.java", "Cannot continue");
            }
        }
        catch(Exception e)
        {
            Drivers.eop("Caller_StegoEmbedder Constructor", strSourceText, e, e.getMessage(), true);
        }
    }

    private boolean callStegoEmbedder_SourceFile(StegoFileSelection parent, File fleEncryptionJAR, boolean encryptSource, int encryptionKeySize, File fleEncryptionKey, boolean importExternalJAR, File fleExternalJAR, 
            boolean useSourceFle, File fleSource, boolean useSourceTxt, String strSourceText, String strBatchID, ArrayList alCarrierNodes, File fleCarrierImageSaveDirectory)
    {
        try
        {
            StegoEmbedder stegowriter = null;
            Caller_Embedder_JAR caller_ExternalJAR_Embed = null;
            int offset = 0;
            JPanel_AddCarrierImagePath_OR_UploadDestination currNode = null;
            int messageFileExtensionType = Drivers.getFileExtensionType(fleSource);
            String strMessageFileExtensionIndexType = Drivers.formatStringWithLeadingZeros((new StringBuilder()).append(messageFileExtensionType).toString(), 2);
            String strNumCarrierFilesTot = Drivers.formatStringWithLeadingZeros((new StringBuilder()).append(alCarrierNodes.size()).toString(), 2);
            byte arrSourceMsgToEmbed[] = (byte[])null;
            byte arrCurrIndexNum[] = (byte[])null;
            byte arrBatchNum[] = strBatchID.getBytes();
            byte arrNumCarrierFilesTot[] = strNumCarrierFilesTot.getBytes();
            byte arrMessageFileIndexExtensionType[] = strMessageFileExtensionIndexType.getBytes();
            byte arrCompleteSourceMsgToEmbed[] = (byte[])null;
            byte arrSourceDateFileExtension[] = (byte[])null;
            if(useSourceFle)
            {
                arrCompleteSourceMsgToEmbed = Drivers.readImage_Fully(fleSource);
                arrSourceDateFileExtension = Drivers.getFileExtension_ByteArray(fleSource, false);
            } else
            if(useSourceTxt)
            {
                arrCompleteSourceMsgToEmbed = strSourceText.getBytes();
                arrSourceDateFileExtension = Drivers.getFileExtension_ByteArray(fleSource, true);
            }
            for(int i = 0; i < alCarrierNodes.size(); i++)
            {
                currNode = (JPanel_AddCarrierImagePath_OR_UploadDestination)alCarrierNodes.get(i);
                String strCurrIndex = Drivers.formatStringWithLeadingZeros((new StringBuilder()).append(i).toString(), 2);
                arrCurrIndexNum = strCurrIndex.getBytes();
                arrSourceMsgToEmbed = new byte[currNode.getMyEmbedContentinCarrierFileSize(false)];
                for(int j = 0; j < currNode.getMyEmbedContentinCarrierFileSize(false); j++)
                {
                    arrSourceMsgToEmbed[j] = arrCompleteSourceMsgToEmbed[offset++];
                }

                try
                {
                    if(importExternalJAR)
                    {
                        caller_ExternalJAR_Embed = new Caller_Embedder_JAR(fleExternalJAR, arrSourceMsgToEmbed, arrBatchNum, arrCurrIndexNum, arrNumCarrierFilesTot, arrSourceDateFileExtension, currNode.getMyCarrierFilePath(), fleCarrierImageSaveDirectory);
                    } else
                    {
                        stegowriter = new StegoEmbedder(fleEncryptionJAR, encryptSource, encryptionKeySize, fleEncryptionKey, arrSourceMsgToEmbed, currNode.getMyEmbedContentSize(false), arrBatchNum, arrCurrIndexNum, arrNumCarrierFilesTot, arrMessageFileIndexExtensionType, arrSourceDateFileExtension, currNode.getMyCarrierFilePath(), fleCarrierImageSaveDirectory);
                    }
                }
                catch(Exception e)
                {
                    stegowriter = new StegoEmbedder(fleEncryptionJAR, encryptSource, encryptionKeySize, fleEncryptionKey, arrSourceMsgToEmbed, currNode.getMyEmbedContentSize(false), arrBatchNum, arrCurrIndexNum, arrNumCarrierFilesTot, arrMessageFileIndexExtensionType, arrSourceDateFileExtension, currNode.getMyCarrierFilePath(), fleCarrierImageSaveDirectory);
                }
            }

        }
        catch(Exception e)
        {
            Drivers.eop("callStegoEmbedder_SourceFile", strMyClassName, e, e.getLocalizedMessage(), true);
            return false;
        }
        return true;
    }
}
