/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * 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.pyamsoft.pydroid.util;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.TypedValue;

public final class StringUtil {

  private static final int INDEX_ATTR = 0;

  private StringUtil() {

  }

  @NonNull public static String xor(final @NonNull String s, final long r) {
    final char[] chars = s.toCharArray();
    for (int i = 0; i < chars.length; ++i) {
      chars[i] = (char) (chars[i] ^ r);
    }
    return String.valueOf(chars);
  }

  public static String formatResString(final @NonNull Resources resources, final int stringId,
      final @NonNull Object... args) {
    final String format = resources.getString(stringId);
    return String.format(format, args);
  }

  public static String formatString(final @NonNull String fmt, final @NonNull Object... args) {
    return String.format(fmt, args);
  }

  /**
   * Takes an array of strings and creates a SpannableStringBuilder out of them If the array is
   * null or empty, returns null
   */
  public static SpannableStringBuilder createBuilder(final @NonNull String... strs) {
    final int size = strs.length;
    if (size > 0) {
      final SpannableStringBuilder strb = new SpannableStringBuilder(strs[0]);
      for (int i = 1; i < size; ++i) {
        if (strs[i] != null) {
          strb.append(strs[i]);
        }
      }
      return strb;
    } else {
      return null;
    }
  }

  /**
   * Applies multiple Spans to a Spannable string Use passed in builder as the out parameter
   */
  public static void multiSpannable(final @NonNull Spannable out, final int start, final int stop,
      final @NonNull Object... spans) {
    for (final Object span : spans) {
      out.setSpan(span, start, stop, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    }
  }

  public static void colorSpan(final @NonNull Spannable out, final int start, final int stop,
      final int color) {
    out.setSpan(new ForegroundColorSpan(color), start, stop, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  }

  public static void boldSpan(final @NonNull Spannable out, final int start, final int stop) {
    out.setSpan(new StyleSpan(Typeface.BOLD), start, stop, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  }

  public static void sizeSpan(final @NonNull Spannable out, final int start, final int stop,
      final int size) {
    out.setSpan(new AbsoluteSizeSpan(size), start, stop, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  }

  public static TypedArray getAttributeFromAppearance(final @NonNull Context context,
      final int textAppearance, final int attr) {
    final TypedValue typedValue = new TypedValue();
    context.getTheme().resolveAttribute(textAppearance, typedValue, true);
    return context.obtainStyledAttributes(typedValue.data, new int[] { attr });
  }

  public static int getTextSizeFromAppearance(final @NonNull Context context,
      final int textAppearance) {
    final TypedArray a =
        getAttributeFromAppearance(context, textAppearance, android.R.attr.textSize);
    final int textSize = a.getDimensionPixelSize(INDEX_ATTR, -1);
    a.recycle();
    return textSize;
  }

  public static int getTextColorFromAppearance(final @NonNull Context context,
      final int textAppearance) {
    final TypedArray a =
        getAttributeFromAppearance(context, textAppearance, android.R.attr.textColor);
    final int textSize = a.getColor(INDEX_ATTR, -1);
    a.recycle();
    return textSize;
  }
}

@@ -777,16 +777,63 @@
 peface;%0A
+import android.support.annotation.CheckResult;%0A
 import a
@@ -867,16 +867,16 @@
 onNull;%0A
-
 import a
@@ -1213,16 +1213,29 @@
 %0A  %7D%0A%0A  
+@CheckResult 
 @NonNull
@@ -1465,32 +1465,56 @@
 Of(chars);%0A  %7D%0A%0A
+  @CheckResult @NonNull%0A
   public static 
@@ -1725,32 +1725,56 @@
 at, args);%0A  %7D%0A%0A
+  @CheckResult @NonNull%0A
   public static 
@@ -2030,32 +2030,54 @@
 ns null%0A   */%0A  
+@CheckResult @NonNull 
 public static Sp
@@ -2111,16 +2111,23 @@
 Builder(
+%0A      
 final @N
@@ -2446,19 +2446,82 @@
     
-return null
+throw new IllegalArgumentException(%22Cannot format empty array of strings%22)
 ;%0A  
@@ -3522,32 +3522,56 @@
 NCLUSIVE);%0A  %7D%0A%0A
+  @CheckResult @NonNull%0A
   public static 
@@ -3896,32 +3896,45 @@
 attr %7D);%0A  %7D%0A%0A  
+@CheckResult 
 public static in
@@ -4243,26 +4243,39 @@
 tSize;%0A  %7D%0A%0A
-
   
+@CheckResult 
 public stati

Check result of StringUtil functions