package uk.gov.register.views;

import com.fasterxml.jackson.databind.node.TextNode;
import com.google.common.collect.ImmutableMap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import uk.gov.register.core.RegisterData;
import uk.gov.register.resources.RequestContext;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Optional;

import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class HomePageViewTest {

    @Mock
    RequestContext mockRequestContext;

    @Test
    public void getRegisterText_rendersRegisterTextAsMarkdown() throws Exception {
        String registerText = "foo *bar* [baz](/quux)";
        RegisterData registerData = new RegisterData(ImmutableMap.of(
                "register", new TextNode("widget"),
                "phase", new TextNode("alpha"),
                "text", new TextNode(registerText)
        ));
        HomePageView homePageView = new HomePageView(null, null, mockRequestContext, 1, 2, null, () -> "test.register.gov.uk", registerData, Optional.<RegisterProof>empty());

        String result = homePageView.getRegisterText();

        assertThat(result, equalTo("<p>foo <em>bar</em> <a href=\"/quux\">baz</a></p>\n"));
    }

    @Test
    public void getLastUpdatedTime_formatsTheLocalDateTimeToUKDateTimeFormat() {
        Instant instant = LocalDateTime.of(2015, 9, 11, 13, 17, 59, 543).toInstant(ZoneOffset.UTC);

        HomePageView homePageView = new HomePageView(null, null, mockRequestContext, 1, 2, instant, () -> "test.register.gov.uk", null, Optional.<RegisterProof>empty());

        assertThat(homePageView.getLastUpdatedTime(), equalTo("11 September 2015"));
    }

    @Test
    public void getLinkToRegisterRegister_returnsTheLinkOfRegisterRegister(){
        Instant instant = LocalDateTime.of(2015, 9, 11, 13, 17, 59, 543).toInstant(ZoneOffset.UTC);

        when(mockRequestContext.getScheme()).thenReturn("https");

        RegisterData registerData = new RegisterData(ImmutableMap.of(
                "register", new TextNode("school")
        ));
        HomePageView homePageView = new HomePageView(null, null, mockRequestContext, 1, 2, instant, () -> "test.register.gov.uk", registerData, Optional.<RegisterProof>empty());

        assertThat(homePageView.getLinkToRegisterRegister(), equalTo("https://register.test.register.gov.uk/record/school"));
    }

    @Test
    public void getRegisterProof_returnsPrettyPrintedJson() throws Exception {
        RegisterProof regProof = new RegisterProof("e869291e3017a7b1dd6b16af0b556d75378bef59486f1a7f53586b3ca86aed09");

        HomePageView homePageView = new HomePageView(null, null, mockRequestContext, 1, 2, null, () -> "test.register.gov.uk", null, Optional.of(regProof));

        String result = homePageView.getRegisterJson();

        assertThat(result, equalTo("{\n  \"proof-identifier\" : \"merkle:sha-256\",\n  \"root-hash\" : \"e869291e3017a7b1dd6b16af0b556d75378bef59486f1a7f53586b3ca86aed09\"\n}"));
    }

    @Test
    public void getRegisterProof_returnsEmptyStringForNoProof() throws Exception {
        HomePageView homePageView = new HomePageView(null, null, mockRequestContext, 1, 2, null, () -> "test.register.gov.uk", null, Optional.<RegisterProof>empty());
        assertThat(homePageView.getRegisterJson(), isEmptyString());
    }

}

@@ -253,16 +253,54 @@
 Runner;%0A
+import uk.gov.register.core.Register;%0A
 import u
@@ -333,16 +333,16 @@
 erData;%0A
-
 import u
@@ -1319,34 +1319,30 @@
 gisterData, 
-Optional.%3C
+create
 RegisterProo
@@ -1334,38 +1334,32 @@
 ateRegisterProof
-%3Eempty
 ());%0A%0A        St
@@ -1828,34 +1828,30 @@
 .uk%22, null, 
-Optional.%3C
+create
 RegisterProo
@@ -1843,38 +1843,32 @@
 ateRegisterProof
-%3Eempty
 ());%0A%0A        as
@@ -2482,26 +2482,22 @@
 erData, 
-Optional.%3C
+create
 Register
@@ -2497,30 +2497,24 @@
 egisterProof
-%3Eempty
 ());%0A%0A      
@@ -2987,20 +2987,8 @@
 ll, 
-Optional.of(
 regP
@@ -2992,17 +2992,16 @@
 egProof)
-)
 ;%0A%0A     
@@ -3246,330 +3246,152 @@
     
-@Test%0A    public void getRegisterProof_returnsEmptyStringForNoProof() throws Exception %7B%0A        HomePageView homePageView = new HomePageView(null, null, mockRequestContext, 1, 2, null, () -%3E %22test.register.gov.uk%22, null, Optional.%3CRegisterProof%3Eempty());%0A        assertThat(homePageView.getRegisterJson(), isEmptyString()
+private RegisterProof createRegisterProof()%7B%0A        return new RegisterProof(%22e869291e3017a7b1dd6b16af0b556d75378bef59486f1a7f53586b3ca86aed09%22
 );%0A 

Fix HomePageView tests