/*
 * Copyright (C) 2017 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 android.support.design.card;

import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.support.annotation.RestrictTo;
import android.support.v4.view.ViewCompat;

/** @hide */
@RestrictTo(LIBRARY_GROUP)
class MaterialCardViewHelper {

  private static final int DEFAULT_STROKE_VALUE = -1;
  private final MaterialCardView materialCardView;

  private int strokeColor;
  private int strokeWidth;

  public MaterialCardViewHelper(MaterialCardView card) {
    materialCardView = card;
  }

  public void loadFromAttributes(TypedArray attributes) {
    strokeColor =
        attributes.getColor(R.styleable.MaterialCardView_strokeColor, DEFAULT_STROKE_VALUE);
    strokeWidth = attributes.getDimensionPixelSize(R.styleable.MaterialCardView_strokeWidth, 0);
    ViewCompat.setBackground(materialCardView, createBackgroundDrawable());
    adjustContentPadding(strokeWidth);
  }

  /**
   * Creates a drawable background for the card in order to handle a stroke outline.
   *
   * @return drawable representing background for a card.
   */
  private Drawable createBackgroundDrawable() {
    GradientDrawable bgDrawable = new GradientDrawable();
    bgDrawable.setCornerRadius(materialCardView.getRadius());
    // In order to set a stroke, a size and color both need to be set. We default to a zero-width
    // width size, but won't set a default color. This prevents drawing a stroke that blends in with
    // the card but that could affect card spacing.
    if (strokeColor != DEFAULT_STROKE_VALUE) {
      bgDrawable.setStroke(strokeWidth, strokeColor);
    }

    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
      bgDrawable.setColor(materialCardView.getCardBackgroundColor());
    } else {
      // For API < 21, use the default background color from the colorStateList that's provided by
      // {@code getCardBackgroundColor}; this class is not currently attempting to handle states.
      bgDrawable.setColor(materialCardView.getCardBackgroundColor().getDefaultColor());
    }

    return bgDrawable;
  }

  /**
   * Guarantee at least enough content padding to account for the stroke width.
   *
   * @param strokeWidth width of the stroke on the card
   */
  private void adjustContentPadding(int strokeWidth) {
    int contentPaddingLeft = materialCardView.getContentPaddingLeft() + strokeWidth;
    int contentPaddingTop = materialCardView.getContentPaddingTop() + strokeWidth;
    int contentPaddingRight = materialCardView.getContentPaddingRight() + strokeWidth;
    int contentPaddingBottom = materialCardView.getContentPaddingBottom() + strokeWidth;
    materialCardView.setContentPadding(
        contentPaddingLeft, contentPaddingTop, contentPaddingRight, contentPaddingBottom);
  }
}

@@ -878,152 +878,37 @@
 oid.
-os.Build.VERSION;%0Aimport android.os.Build.VERSION_CODES;%0Aimport android.support.annotation.RestrictTo;%0Aimport android.support.v4.view.ViewCompat
+support.annotation.RestrictTo
 ;%0A%0A/
@@ -1504,33 +1504,8 @@
     
-ViewCompat.setBackground(
 mate
@@ -1520,20 +1520,33 @@
 View
-, 
+.setForeground(
 create
-Back
+Fore
 grou
@@ -1585,27 +1585,16 @@
 Padding(
-strokeWidth
 );%0A  %7D%0A%0A
@@ -1623,20 +1623,20 @@
 rawable 
-back
+fore
 ground f
@@ -1724,20 +1724,20 @@
 senting 
-back
+fore
 ground f
@@ -1778,20 +1778,20 @@
 e create
-Back
+Fore
 groundDr
@@ -1822,17 +1822,17 @@
 rawable 
-b
+f
 gDrawabl
@@ -1859,25 +1859,25 @@
 able();%0A    
-b
+f
 gDrawable.se
@@ -1917,24 +1917,25 @@
 tRadius());%0A
+%0A
     // In or
@@ -2218,33 +2218,33 @@
 _VALUE) %7B%0A      
-b
+f
 gDrawable.setStr
@@ -2289,444 +2289,16 @@
     
-if (VERSION.SDK_INT %3E= VERSION_CODES.LOLLIPOP) %7B%0A      bgDrawable.setColor(materialCardView.getCardBackgroundColor());%0A    %7D else %7B%0A      // For API %3C 21, use the default background color from the colorStateList that's provided by%0A      // %7B@code getCardBackgroundColor%7D; this class is not currently attempting to handle states.%0A      bgDrawable.setColor(materialCardView.getCardBackgroundColor().getDefaultColor());%0A    %7D%0A%0A    return b
+return f
 gDra
@@ -2314,21 +2314,16 @@
 %7D%0A%0A  /**
-%0A   *
  Guarant
@@ -2393,72 +2393,8 @@
 dth.
-%0A   *%0A   * @param strokeWidth width of the stroke on the card%0A  
  */%0A
@@ -2429,31 +2429,16 @@
 Padding(
-int strokeWidth
 ) %7B%0A    

Update MaterialCardView to use foreground instead of background for stroke