package org.aohe.transfer;


import org.aohe.core.twain.Twain;
import org.aohe.core.twain.TwainSource;
import org.aohe.exceptions.TwainException;
import org.aohe.utils.TwainUtils;

import java.io.File;


public class TwainFileTransfer extends TwainTransfer {

    protected File file;

    public TwainFileTransfer(TwainSource source) {
        super(source);
        file = null;
    }

    protected int getImageFileFormat() {
        return source.getImageFileFormat();
    }

    public void setFile(File f) {
        file = f;
    }

    public File getFile() {
        if (file == null) {
            String ext = Twain.ImageFileFormatExts[getImageFileFormat()];
            try {
                File dir = new File(System.getProperty("user.home"), "mmsc/tmp");
                dir.mkdirs();
                file = File.createTempFile("mmsctwain", ext, dir);
            } catch (Exception e) {
                file = new File("c:\\mmsctwain." + ext);
            }
        }
        return file;
    }

    @Override
    public void initiate() throws TwainException {
        super.initiate();

        String file = getFile().getPath();
        int iff = getImageFileFormat();

        byte[] setup = new byte[260];
        TwainUtils.setString(setup, 0, file);
        TwainUtils.setINT16(setup, 256, iff);
        TwainUtils.setINT16(setup, 258, 0);

        source.call(Twain.DG_CONTROL, Twain.DAT_SETUPFILEXFER, Twain.MSG_SET, setup);
        source.call(Twain.DG_IMAGE, Twain.DAT_IMAGEFILEXFER, Twain.MSG_GET, null);
    }

    @Override
    public void finish() throws TwainException {
        Twain.transferFileImage(file);
    }

    @Override
    public void cancel() throws TwainException {
        if ((file != null) && file.exists()) {
            file.delete();
        }
    }

    @Override
    public void cleanup() throws TwainException {
        setFile(null);
    }

}
