package peergos.shared.user.fs;

import jsinterop.annotations.*;
import peergos.shared.storage.ContentAddressedStorage;
import peergos.shared.util.*;

import java.io.*;
import java.time.*;
import java.util.Optional;

@JsType
public class FileProperties {
    public static final FileProperties EMPTY = new FileProperties("", 0, LocalDateTime.MIN, false, Optional.empty());

    public final String name;
    @JsIgnore
    public final long size;
    public final LocalDateTime modified;
    public final boolean isHidden;
    public final Optional<byte[]> thumbnail;

    public FileProperties(String name, int sizeHi, int sizeLo, LocalDateTime modified, boolean isHidden, Optional<byte[]> thumbnail) {
        this.name = name;
        this.size = sizeLo | ((sizeHi | 0L) << 32);
        this.modified = modified;
        this.isHidden = isHidden;
        this.thumbnail = thumbnail;
    }

    @JsIgnore
    public FileProperties(String name, long size, LocalDateTime modified, boolean isHidden, Optional<byte[]> thumbnail) {
        this(name, (int)(size >> 32), (int) size, modified, isHidden, thumbnail);
    }

    public int sizeLow() {
        return (int) size;
    }

    public int sizeHigh() {
        return (int) (size >> 32);
    }

    public byte[] serialize() {
        DataSink dout = new DataSink();
        dout.writeString(name);
        dout.writeLong(size);
        dout.writeDouble(modified.toEpochSecond(ZoneOffset.UTC));
        dout.writeBoolean(isHidden);
        if (!thumbnail.isPresent())
            dout.writeInt(0);
        else {
            dout.writeArray(thumbnail.get());
        }
        return dout.toByteArray();
    }

    public static FileProperties deserialize(byte[] raw) throws IOException {
        DataSource din = new DataSource(raw);
        String name = din.readString();
        long size = din.readLong();
        double modified = din.readDouble();
        boolean isHidden = din.readBoolean();
        int length = din.readInt();
        Optional<byte[]> thumbnail = length == 0 ?
                Optional.empty() :
                Optional.of(Serialize.deserializeByteArray(length, din, length));

        return new FileProperties(name, size, LocalDateTime.ofEpochSecond((int)modified, 0, ZoneOffset.UTC), isHidden, thumbnail);
    }

    @JsIgnore
    public FileProperties withSize(long newSize) {
        return new FileProperties(name, newSize, modified, isHidden, thumbnail);
    }

    public FileProperties withModified(LocalDateTime modified) {
        return new FileProperties(name, size, modified, isHidden, thumbnail);
    }

    @Override
    public String toString() {
        return "FileProperties{" +
                "name='" + name + '\'' +
                ", size=" + size +
                ", modified=" + modified +
                ", isHidden=" + isHidden +
                ", thumbnail=" + thumbnail +
                '}';
    }
}
@@ -1393,22 +1393,20 @@
 ut.write
-Double
+Long
 (modifie
@@ -1867,14 +1867,12 @@
     
-double
+long
  mod
@@ -1891,14 +1891,12 @@
 read
-Double
+Long
 ();%0A
@@ -2220,21 +2220,16 @@
 hSecond(
-(int)
 modified

fix random errors with modification time (don't need to resort to double hack because of JS anymore)