package com.atlassian.bitbucket.internal.mirroring.upstream.rest;

import com.atlassian.bitbucket.mirroring.upstream.MirrorServer;
import com.atlassian.bitbucket.rest.RestMapEntity;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.util.RestPage;
import com.atlassian.bitbucket.validation.annotation.RequiredString;
import com.google.common.collect.ImmutableList;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import javax.validation.constraints.NotNull;
import java.util.Date;

import static com.atlassian.bitbucket.internal.mirroring.dao.AoConstants.DEFAULT_STRING_COLUMN_LENGTH;
import static com.atlassian.bitbucket.internal.mirroring.dao.AoConstants.LONG_STRING_COLUMN_LENGTH;

@JsonSerialize
@JsonSurrogate(MirrorServer.class)
public class RestMirrorServer extends RestMapEntity implements MirrorServer {

    public static final String ID = "id";
    public static final String BASE_URL = "baseUrl";
    public static final String ENABLED = "enabled";
    public static final String LAST_SEEN_DATE = "lastSeenDate";
    public static final String NAME = "name";
    public static final String PRODUCT_TYPE = "productType";
    public static final String PRODUCT_VERSION = "productVersion";

    // for REST documentation
    private RestMirrorServer(String id, String baseUrl, String name, String productType, String productVersion, Date lastSeenDate, boolean enabled) {
        put(ID, id);
        put(BASE_URL, baseUrl);
        put(NAME, name);
        put(PRODUCT_TYPE, productType);
        put(PRODUCT_VERSION, productVersion);
        put(LAST_SEEN_DATE, lastSeenDate);
        put(ENABLED, enabled);
    }

    @SuppressWarnings("UnusedDeclaration") // for Jackson
    public RestMirrorServer() {
    }

    public RestMirrorServer(MirrorServer mirror) {
        this(mirror.getId(), mirror.getBaseUrl(), mirror.getName(), mirror.getProductType(),
                mirror.getProductVersion(), mirror.getLastSeenDate(), mirror.isEnabled());
    }

    public static final RestMirrorServer EXAMPLE_ENABLED =
            new RestMirrorServer("B0F5-CS21-45C2-CCK3", "http://atlassian-vietnam.atlassian.com", "Mirror", "Bitbucket", "4.0.0", new Date(System.currentTimeMillis() - 632742L), true);
    public static final RestPage<RestMirrorServer> EXAMPLE_PAGE = new RestPage<>(0, 25, 2, true,
            ImmutableList.of(EXAMPLE_ENABLED,
                    new RestMirrorServer("B08E-RC7D-42H6-9SWU", "http://atlassian-vietnam.atlassian.com", "Mirror Vietnam", "Bitbucket", "4.0.0", new Date(System.currentTimeMillis() - 56921L), true)),
            null);

    @RequiredString(size = LONG_STRING_COLUMN_LENGTH)
    @Override
    public String getBaseUrl() {
        return getStringProperty(BASE_URL);
    }

    @RequiredString(size = DEFAULT_STRING_COLUMN_LENGTH)
    @Override
    public String getId() {
        return getStringProperty(ID);
    }

    @NotNull
    @Override
    public Date getLastSeenDate() {
        return new Date(getLongProperty(LAST_SEEN_DATE));
    }

    @RequiredString(size = DEFAULT_STRING_COLUMN_LENGTH)
    @Override
    public String getName() {
        return getStringProperty(NAME);
    }

    @RequiredString(size = DEFAULT_STRING_COLUMN_LENGTH)
    @Override
    public String getProductType() {
        return getStringProperty(PRODUCT_TYPE);
    }

    @RequiredString(size = DEFAULT_STRING_COLUMN_LENGTH)
    @Override
    public String getProductVersion() {
        return getStringProperty(PRODUCT_VERSION);
    }

    @Override
    public boolean isEnabled() {
        return getBoolProperty(ENABLED);
    }
}
