package epson.print.storage.gdrivev3;


import com.epson.iprint.storage.StorageServiceClient;
import com.google.api.services.drive.model.File;

import java.util.HashSet;

public class GoogleDriveFile implements OnlineFile {
    private static HashSet<String> sGoogleDocumentsTypeSet;
    private final File mDriveFile;

    public GoogleDriveFile(@NonNull File file) {
        mDriveFile = file;
    }

    public String getName() {
        return mDriveFile.getName();
    }

    public boolean isFolder() {
        return GoogleDriveMimeType.MIME_TYPE_GOOGLE_FOLDER.equals(mDriveFile.getMimeType());
    }

    @Nullable
    public String getId() {
        return mDriveFile.getId();
    }

    public String getMimeType() {
        return mDriveFile.getMimeType();
    }

    public boolean isDisplayFile() {
        if (localGetTrashed()) {
            return false;
        }
        if (!isFolder() && !isIprintFile()) {
            return false;
        }
        return true;
    }

    private boolean isIprintFile() {
        if (localGetTrashed()) {
            return false;
        }
        prepareFileTypeSet();
        if (sGoogleDocumentsTypeSet.contains(getMimeType())) {
            return true;
        }
        return StorageServiceClient.isPrintableFilename(getName());
    }

    public boolean isGoogleDocuments() {
        prepareFileTypeSet();
        return sGoogleDocumentsTypeSet.contains(getMimeType());
    }

    private boolean localGetTrashed() {
        Boolean trashed = mDriveFile.getTrashed();
        return trashed != null && trashed.booleanValue();
    }

    private static synchronized void prepareFileTypeSet() {
        synchronized (GoogleDriveFile.class) {
            if (sGoogleDocumentsTypeSet == null) {
                sGoogleDocumentsTypeSet = new HashSet<>();
                sGoogleDocumentsTypeSet.add(GoogleDriveMimeType.MIME_TYPE_GOOGLE_DOC);
                sGoogleDocumentsTypeSet.add(GoogleDriveMimeType.MIME_TYPE_GOOGLE_SHEETS);
                sGoogleDocumentsTypeSet.add(GoogleDriveMimeType.MIME_TYPE_GOOGLE_DRAWING);
                sGoogleDocumentsTypeSet.add(GoogleDriveMimeType.MIME_TYPE_GOOGLE_SLIDES);
            }
        }
    }
}
