// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.client.account;

import com.google.gerrit.reviewdb.client.AccountDiffPreference;
import com.google.gerrit.reviewdb.client.AccountDiffPreference.Whitespace;
import com.google.gwt.core.client.JavaScriptObject;

public class DiffPreferences extends JavaScriptObject {
  public static DiffPreferences create(AccountDiffPreference in) {
    DiffPreferences p = createObject().cast();
    if (in == null) {
      in = AccountDiffPreference.createDefault(null);
    }
    p.ignoreWhitespace(in.getIgnoreWhitespace());
    p.tabSize(in.getTabSize());
    p.context(in.getContext());
    p.intralineDifference(in.isIntralineDifference());
    p.showLineEndings(in.isShowLineEndings());
    p.showTabs(in.isShowTabs());
    p.showWhitespaceErrors(in.isShowWhitespaceErrors());
    p.syntaxHighlighting(in.isSyntaxHighlighting());
    p.hideTopMenu(in.isHideTopMenu());
    p.expandAllComments(in.isExpandAllComments());
    return p;
  }

  public final void copyTo(AccountDiffPreference p) {
    p.setIgnoreWhitespace(ignoreWhitespace());
    p.setTabSize(tabSize());
    p.setContext((short)context());
    p.setIntralineDifference(intralineDifference());
    p.setShowLineEndings(showLineEndings());
    p.setShowTabs(showTabs());
    p.setShowWhitespaceErrors(showWhitespaceErrors());
    p.setSyntaxHighlighting(syntaxHighlighting());
    p.setHideTopMenu(hideTopMenu());
    p.setExpandAllComments(expandAllComments());
  }

  public final void ignoreWhitespace(Whitespace i) {
    setIgnoreWhitespaceRaw(i.toString());
  }
  private final native void setIgnoreWhitespaceRaw(String i) /*-{ this.ignore_whitespace = i }-*/;

  public final native void tabSize(int t) /*-{ this.tab_size = t }-*/;
  public final native void context(int c) /*-{ this.context = c }-*/;
  public final native void intralineDifference(boolean i) /*-{ this.intraline_difference = i }-*/;
  public final native void showLineEndings(boolean s) /*-{ this.show_line_endings = s }-*/;
  public final native void showTabs(boolean s) /*-{ this.show_tabs = s }-*/;
  public final native void showWhitespaceErrors(boolean s) /*-{ this.show_whitespace_errors = s }-*/;
  public final native void syntaxHighlighting(boolean s) /*-{ this.syntax_highlighting = s }-*/;
  public final native void hideTopMenu(boolean s) /*-{ this.hide_top_menu = s }-*/;
  public final native void expandAllComments(boolean e) /*-{ this.expand_all_comments = e }-*/;

  public final Whitespace ignoreWhitespace() {
    String s = ignoreWhitespaceRaw();
    return s != null ? Whitespace.valueOf(s) : Whitespace.IGNORE_NONE;
  }
  private final native String ignoreWhitespaceRaw() /*-{ return this.ignore_whitespace }-*/;

  public final int tabSize() {return get("tab_size", 8); }
  public final int context() {return get("context", 10); }
  public final boolean intralineDifference() { return get("intraline_difference", true); }
  public final boolean showLineEndings() { return get("show_line_endings", true); }
  public final boolean showTabs() { return get("show_tabs", true); }
  public final boolean showWhitespaceErrors() { return get("show_whitespace_errors", true); }
  public final boolean syntaxHighlighting() { return get("syntax_highlighting", true); }
  public final native boolean hideTopMenu() /*-{ return this.hide_top_menu || false }-*/;
  public final native boolean expandAllComments() /*-{ return this.expand_all_comments || false }-*/;

  private final native int get(String n, int d)
  /*-{ return this.hasOwnProperty(n) ? this[n] : d }-*/;

  private final native boolean get(String n, boolean d)
  /*-{ return this.hasOwnProperty(n) ? this[n] : d }-*/;

  protected DiffPreferences() {
  }
}

@@ -3416,32 +3416,39 @@
 %0A  public final 
+native 
 boolean intralin
@@ -3461,16 +3461,19 @@
 rence() 
+/*-
 %7B return
@@ -3473,21 +3473,21 @@
  return 
-get(%22
+this.
 intralin
@@ -3498,27 +3498,31 @@
 fference
-%22, true); %7D
+ %7C%7C false %7D-*/;
 %0A  publi
@@ -3521,32 +3521,39 @@
 %0A  public final 
+native 
 boolean showLine
@@ -3558,24 +3558,27 @@
 neEndings() 
+/*-
 %7B return get
@@ -3570,29 +3570,29 @@
 /*-%7B return 
-get(%22
+this.
 show_line_en
@@ -3596,27 +3596,31 @@
 _endings
-%22, true); %7D
+ %7C%7C false %7D-*/;
 %0A  publi
@@ -3619,32 +3619,39 @@
 %0A  public final 
+native 
 boolean showTabs
@@ -3649,24 +3649,27 @@
  showTabs() 
+/*-
 %7B return get
@@ -3661,29 +3661,29 @@
 /*-%7B return 
-get(%22
+this.
 show_tabs%22, 
@@ -3679,27 +3679,31 @@
 how_tabs
-%22, true); %7D
+ %7C%7C false %7D-*/;
 %0A  publi
@@ -3702,32 +3702,39 @@
 %0A  public final 
+native 
 boolean showWhit
@@ -3748,16 +3748,19 @@
 rrors() 
+/*-
 %7B return
@@ -3760,21 +3760,21 @@
  return 
-get(%22
+this.
 show_whi
@@ -3787,27 +3787,31 @@
 e_errors
-%22, true); %7D
+ %7C%7C false %7D-*/;
 %0A  publi
@@ -3818,16 +3818,23 @@
 c final 
+native 
 boolean 
@@ -3854,16 +3854,19 @@
 hting() 
+/*-
 %7B return
@@ -3866,21 +3866,21 @@
  return 
-get(%22
+this.
 syntax_h
@@ -3894,19 +3894,23 @@
 ting
-%22, true); %7D
+ %7C%7C false %7D-*/;
 %0A  p
@@ -4091,32 +4091,32 @@
 %7C%7C false %7D-*/;%0A%0A
+
   private final 
@@ -4209,122 +4209,8 @@
 /;%0A%0A
-  private final native boolean get(String n, boolean d)%0A  /*-%7B return this.hasOwnProperty(n) ? this%5Bn%5D : d %7D-*/;%0A%0A
   pr

Fix saving intraline highlight diff preference