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

import com.atlassian.bitbucket.internal.mirroring.SimpleMirroringRequest;
import com.atlassian.bitbucket.internal.mirroring.upstream.dao.AoMirroringRequest;
import com.atlassian.bitbucket.internal.mirroring.MirroringRequestState;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.user.TestApplicationUser;

import java.net.URI;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MirrorsTestHelper {
    public static final int DEFAULT_ID = 333;
    public static final String DEFAULT_MIRROR_ID = "my-bitbucket-mirror";
    public static final String DEFAULT_MIRROR_NAME = "My Bitbucket Mirror";
    public static final String DEFAULT_PRODUCT_TYPE = "Bitbucket";
    public static final String DEFAULT_PRODUCT_VERSION = "4.NEXT";
    public static final String DEFAULT_DESCRIPTOR_URL = "/descriptor";
    public static final String DEFAULT_MIRROR_BASE_URL = "http://bitbucket-eu.localhost:9999";

    public static final TestApplicationUser DEFAULT_USER = new TestApplicationUser("user", 1);

    public static SimpleMirroringRequest createSimpleMirroringRequest() {
        return createSimpleMirroringRequest(DEFAULT_MIRROR_ID);
    }

    public static SimpleMirroringRequest createSimpleMirroringRequest(String mirrorId) {
        return createSimpleMirroringRequest(mirrorId, DEFAULT_MIRROR_NAME);
    }

    public static SimpleMirroringRequest createSimpleMirroringRequest(String mirrorId, String mirrorName) {
        return new SimpleMirroringRequest.Builder()
                .id(DEFAULT_ID)
                .mirrorBaseUrl(DEFAULT_MIRROR_BASE_URL)
                .mirrorServer(mirrorId, mirrorName)
                .product(DEFAULT_PRODUCT_TYPE, DEFAULT_PRODUCT_VERSION)
                .addonDescriptor(URI.create(DEFAULT_DESCRIPTOR_URL)).build();
    }

    public static AoMirroringRequest createMockAoMirroringRequest(String mirrorId, MirroringRequestState state) {
        return createMockAoMirroringRequest(DEFAULT_ID, mirrorId, state, DEFAULT_USER);
    }

    public static AoMirroringRequest createMockAoMirroringRequest(int id, String mirrorId, MirroringRequestState state, ApplicationUser resolver) {
        AoMirroringRequest mock = mock(AoMirroringRequest.class);
        when(mock.getId()).thenReturn(id);
        when(mock.getBaseUrl()).thenReturn(DEFAULT_MIRROR_BASE_URL);
        when(mock.getMirrorId()).thenReturn(mirrorId);
        when(mock.getMirrorName()).thenReturn(DEFAULT_MIRROR_NAME);
        when(mock.getProductType()).thenReturn(DEFAULT_PRODUCT_TYPE);
        when(mock.getProductVersion()).thenReturn(DEFAULT_PRODUCT_VERSION);
        when(mock.getDescriptorUrl()).thenReturn(DEFAULT_DESCRIPTOR_URL);
        when(mock.getAddonDescriptorUri()).thenReturn(URI.create(DEFAULT_DESCRIPTOR_URL));
        when(mock.getState()).thenReturn(state);
        if (resolver != null) {
            when(mock.getResolverId()).thenReturn(resolver.getId());
        }
        return mock;
    }
}