package com.epson.cameracopy.device;

import com.epson.cameracopy.alt.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class CameraFile implements Serializable {
    static final /* synthetic */ boolean $assertionsDisabled = false;
    private static final long serialVersionUID = 1;
    private File mDstFile;
    private final Date mFileCreateDate;
    private String mTempFileName;

    public CameraFile(Date date, String str) {
        mFileCreateDate = date;
        mTempFileName = str;
    }

    public String getFileName(String str) {
        File file = new File(str);
        if ((!file.exists() && !file.mkdirs()) || !file.isDirectory()) {
            return null;
        }
        String format = new SimpleDateFormat("yyyyMMddHHmm", Locale.US).format(mFileCreateDate);
        for (int i = 0; i < 100; i++) {
            File file2 = new File(file, String.format(Locale.US, "%s_%02d.jpg", new Object[]{format, Integer.valueOf(i)}));
            if (!file2.exists()) {
                return file2.toString();
            }
        }
        return null;
    }

    public boolean moveTo(String str) {
        String fileName;
        if (mTempFileName == null || (fileName = getFileName(str)) == null) {
            return false;
        }
        File file = new File(mTempFileName);
        File file2 = new File(fileName);
        if (file.renameTo(file2)) {
            mDstFile = file2;
            mTempFileName = null;
            return true;
        }
        try {
            FileUtils.copy(file, file2);
            file.delete();
            mDstFile = file2;
            mTempFileName = null;
            return true;
        } catch (IOException unused) {
            return false;
        }
    }

    public boolean isMovable() {
        return mTempFileName != null;
    }

    public String getValidFileName() {
        String str = mTempFileName;
        if (str != null) {
            return str;
        }
        File file = mDstFile;
        if (file != null) {
            return file.toString();
        }
        return null;
    }

    public boolean isMoved() {
        return mDstFile != null;
    }

    public void deleteTemporaryFile() {
        String str = mTempFileName;
        if (str != null) {
            File file = new File(str);
            if (file.exists()) {
                file.delete();
            }
            mTempFileName = null;
        }
    }
}
