package org.aohe.core.twain;

import com.sun.jna.Pointer;
import com.sun.jna.win32.StdCallLibrary;
import org.aohe.libs.Win32Twain;

public class TwainCallback implements StdCallLibrary.StdCallCallback {

    public int callback(Win32Twain.TW_IDENTITY orign, Win32Twain.TW_IDENTITY dest, int dg, short dat, short msg, Pointer data) {
//        System.out.println("MSG!!!!!!!!!!!!!!!!!!!!!!!!!!!" + msg);
        try {
            TwainIdentity orig = new TwainIdentity(Twain.sourceManager, orign);
            TwainSource source = Twain.sourceManager.getSource();

            if (orig.getId() != source.getId()) {
                return Twain.TWRC_FAILURE;
            }
            System.out.println(String.format("CALLBACK ----------------------> DG:%d, DAT:%d, MSG:%d", dg, dat, msg));
            return source.callback(dg, dat, msg, data);
        } catch (Throwable e) {
            Twain.signalException(e.getMessage());
            e.printStackTrace();
            return Twain.TWRC_FAILURE;
        }
    }
}
