package com.github.satoshun.truth.android.api.webkit;

import android.net.http.SslCertificate;
import android.webkit.WebView;

import com.github.satoshun.truth.android.api.widget.AbsoluteLayoutSubject;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.SubjectFactory;
import com.google.common.truth.TestVerb;
import com.google.common.truth.Truth;

public abstract class WebViewSubject<S extends WebViewSubject<S, T>, T extends WebView> extends AbsoluteLayoutSubject<S, T> {

  public static final WebViewSubjectFactory FACTORY = new WebViewSubjectFactory();

  private static final TestVerb ASSERT = Truth.assert_();

  public WebViewSubject(FailureStrategy failureStrategy, T actual) {
    super(failureStrategy, actual);
  }

  public S canGoBack() {
    isNotNull();
    ASSERT.withFailureMessage("can go back")
            .that(actual().canGoBack())
            .isTrue();
    return myself();
  }

  public S canNotGoBack() {
    isNotNull();
    ASSERT.withFailureMessage("can not go back")
            .that(actual().canGoBack())
            .isFalse();
    return myself();
  }

  public S canGoForward() {
    isNotNull();
    if (!actual().canGoForward()) {
      fail("can go forward", actual().canGoForward());
    }
    return myself();
  }

  public S canNotGoForward() {
    isNotNull();
    if (actual().canGoForward()) {
      fail("can not go forward", actual().canGoForward());
    }
    return myself();
  }

  public S sameAsCertificate(SslCertificate certificate) {
    isNotNull();
    if (!certificate.equals(actual().getCertificate())) {
      fail("same as certificate", actual().getCertificate());
    }
    return myself();
  }

  public S sameAsOriginalUrl(String url) {
    isNotNull();
    if (url == null) {
      if (actual().getOriginalUrl() != null) {
        fail("same as originalUrl", actual().getOriginalUrl());
      }
      return myself();
    }
    Truth.assertThat(url).isEqualTo("");
    if (!url.equals(actual().getOriginalUrl())) {
      fail("same as originalUrl", actual().getOriginalUrl());
    }
    return myself();
  }

  private static class WebViewSubjectImpl extends WebViewSubject<WebViewSubjectImpl, WebView> {
    WebViewSubjectImpl(FailureStrategy failureStrategy, WebView actual) {
      super(failureStrategy, actual);
    }
  }

  private static class WebViewSubjectFactory extends SubjectFactory<WebViewSubjectImpl, WebView> {
    @Override
    public WebViewSubjectImpl getSubject(FailureStrategy fs, WebView that) {
      return new WebViewSubjectImpl(fs, that);
    }
  }
}

@@ -1163,69 +1163,70 @@
     
-if (!actual().canGoForward()) %7B%0A      fail(%22can go forward%22, 
+ASSERT.withFailureMessage(%22can go forward%22)%0A            .that(
 actu
@@ -1237,39 +1237,55 @@
 .canGoForward())
-;
 %0A    
-%7D
+        .isTrue();
 %0A    return myse
@@ -1351,49 +1351,33 @@
     
-if (actual().canGoForward()) %7B%0A      fail
+ASSERT.withFailureMessage
 (%22ca
@@ -1393,18 +1393,36 @@
 forward%22
-, 
+)%0A            .that(
 actual()
@@ -1437,23 +1437,40 @@
 rward())
-;
 %0A    
-%7D
+        .isFalse();
 %0A    ret
@@ -1572,31 +1572,110 @@
     
-if (!certificate.e
+ASSERT.withFailureMessage(%22same as certificate%22)%0A            .that(certificate)%0A            .isE
 qual
-s
+To
 (act
@@ -1701,397 +1701,834 @@
 e())
-) %7B%0A      fail(%22same as certificate%22, actual().getCertificate());%0A    %7D%0A    return myself();%0A  %7D%0A%0A  public S sameAsOriginalUrl(String url) %7B%0A    isNotNull();%0A    if (url == null) %7B%0A      if (actual().getOriginalUrl() != null) %7B%0A        fail(%22same as originalUrl%22, actual().getOriginalUrl());%0A      %7D%0A      return myself();%0A    %7D%0A    Truth.assertThat(url).isEqualTo(%22%22);%0A    if (!url.e
+;%0A    return myself();%0A  %7D%0A%0A  public S sameAsContentHeight(int height) %7B%0A    isNotNull();%0A    ASSERT.withFailureMessage(%22same as content height%22)%0A            .that(height)%0A            .isEqualTo(actual().getContentHeight());%0A    return myself();%0A  %7D%0A%0A  public S sameAsOriginalUrl(String url) %7B%0A    isNotNull();%0A    ASSERT.withFailureMessage(%22same as original url%22)%0A            .that(url)%0A            .isEqualTo(actual().getOriginalUrl());%0A    return myself();%0A  %7D%0A%0A  public S sameAsProgress(int progress) %7B%0A    isNotNull();%0A    ASSERT.withFailureMessage(%22same as progress%22)%0A            .that(progress)%0A            .isEqualTo(actual().getProgress());%0A    return myself();%0A  %7D%0A%0A  public S sameAsTitle(String title) %7B%0A    isNotNull();%0A    ASSERT.withFailureMessage(%22same as title%22)%0A            .that(title)%0A            .isE
 qual
-s
+To
 (act
@@ -2540,60 +2540,187 @@
 .get
-OriginalUrl())) %7B%0A      fail(%22same as originalUrl%22, 
+Title());%0A    return myself();%0A  %7D%0A%0A  public S sameAsUrl(String url) %7B%0A    isNotNull();%0A    ASSERT.withFailureMessage(%22same as url%22)%0A            .that(url)%0A            .isEqualTo(
 actu
@@ -2727,32 +2727,24 @@
 al().get
-Original
 Url());%0A
     %7D%0A  
@@ -2735,22 +2735,16 @@
 Url());%0A
-    %7D%0A
     retu

create WebViewSubject