package com.kickstarter.libs.utils;

import android.content.SharedPreferences;

import com.kickstarter.KSRobolectricTestCase;
import com.kickstarter.libs.utils.extensions.ProjectExt;
import com.kickstarter.mock.factories.ProjectFactory;
import com.kickstarter.libs.MockSharedPreferences;
import com.kickstarter.libs.RefTag;
import com.kickstarter.models.Project;
import com.kickstarter.models.Urls;
import com.kickstarter.models.Web;

import org.junit.Test;

import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;

public final class RefTagUtilsTest extends KSRobolectricTestCase {
  final static SharedPreferences sharedPreferences = new MockSharedPreferences();

  @Test
  public void testCookieNameForProject() {
    final Project project = ProjectFactory.project();

    assertEquals("ref_" + String.valueOf(project.id()), RefTagUtils.cookieNameForProject(project));
  }

  @Test
  public void testCookieValueForRefTag() {
    final RefTag refTag = RefTag.from("test");

    assertEquals("test%3F" + SystemUtils.secondsSinceEpoch(), RefTagUtils.cookieValueForRefTag(refTag));
  }

  @Test
  public void testStoredCookieRefTagForProject() {
    final CookieManager cookieManager = new CookieManager();
    final CookieStore cookieStore = cookieManager.getCookieStore();
    final Project project = ProjectFactory.project();
    final RefTag refTag = RefTag.recommended();

    // set the cookie and retrieve the ref tag
    cookieStore.add(null, new HttpCookie("ref_" + project.id(), refTag.tag() + "%3F" + SystemUtils.secondsSinceEpoch()));
    final RefTag retrievedRefTag = RefTagUtils.storedCookieRefTagForProject(project, cookieManager, sharedPreferences);

    assertNotNull(retrievedRefTag);
    assertEquals(refTag, retrievedRefTag);
  }

  @Test
  public void testBuildCookieForRefTagAndProject_WithWellFormedUrl() {
    final Project project = ProjectFactory.project();
    final RefTag refTag = RefTag.category();
    final HttpCookie cookie = RefTagUtils.buildCookieWithRefTagAndProject(refTag, project);

    assertNotNull(cookie);
    assertEquals(ProjectExt.timeInSecondsUntilDeadline(project), cookie.getMaxAge());
    assertEquals("www.kickstarter.com", cookie.getDomain());
  }

  @Test
  public void testBuildCookieForRefTagAndProject_WithMalformedUrl() {
    final Web webUrls = ProjectFactory.project().urls().web().toBuilder().project("such:\\bad^<data").build();
    final Urls urls = ProjectFactory.project().urls().toBuilder().web(webUrls).build();
    final Project project = ProjectFactory.project().toBuilder().urls(urls).build();

    final RefTag refTag = RefTag.category();
    final HttpCookie cookie = RefTagUtils.buildCookieWithRefTagAndProject(refTag, project);

    assertNull(cookie);
  }

  @Test
  public void testFindRefTagCookieForProject_WhenCookieExists() {
    final CookieManager cookieManager = new CookieManager();
    final CookieStore cookieStore = cookieManager.getCookieStore();
    final Project project = ProjectFactory.project();
    final RefTag refTag = RefTag.recommended();

    // set and retrieve the cookie
    cookieStore.add(null, new HttpCookie("ref_" + project.id(), refTag.tag() + "%3F" + SystemUtils.secondsSinceEpoch()));
    final HttpCookie cookie = RefTagUtils.findRefTagCookieForProject(project, cookieManager, sharedPreferences);

    assertNotNull(cookie);
    assertEquals(RefTagUtils.cookieNameForProject(project), cookie.getName());
    assertEquals(RefTagUtils.cookieValueForRefTag(refTag), cookie.getValue());
  }

  @Test
  public void testFindRefTagCookieForProject_WhenCookieDoesNotExist() {
    final CookieManager cookieManager = new CookieManager();
    final Project project = ProjectFactory.project();

    // retrieve the cookie
    final HttpCookie cookie = RefTagUtils.findRefTagCookieForProject(project, cookieManager, sharedPreferences);

    assertNull(cookie);
  }
}

@@ -428,16 +428,82 @@
 s.Web;%0A%0A
+import org.joda.time.DateTime;%0Aimport org.joda.time.DateTimeZone;%0A
 import o
@@ -1965,33 +1965,123 @@
 actory.project()
-;
+.toBuilder().deadline(new DateTime(DateTimeZone.UTC).plusDays(10).plusSeconds(1)).build();%0A
 %0A    final RefTa

Fix flaky date time test in RefTagUtils (#1561)