/*
 * Copyright 2016-2018 Michael Bel
 *
 * 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 org.michaelbel.bottomsheet;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import org.michaelbel.bottomsheetdialog.Utils;

/**
 * Date: 17.02.2018
 * Time: 00:30
 *
 * @author Michael Bel
 */

@SuppressWarnings("all")
public class BottomSheetCell extends FrameLayout {

    private Paint paint;
    private int cellHeight;
    private boolean divider;

    private TextView textView;
    private ImageView iconView;

    public BottomSheetCell(Context context) {
        super(context);

        if (paint == null) {
            paint = new Paint();
            paint.setStrokeWidth(1);
            paint.setColor(0x1F000000);
        }

        cellHeight = Utils.dp(context, 48);

        iconView = new ImageView(context);
        iconView.setScaleType(ImageView.ScaleType.CENTER);

        LayoutParams params1 = new LayoutParams(
                Utils.dp(context, 24),
                Utils.dp(context, 24)
        );
        params1.gravity = Gravity.START | Gravity.CENTER_VERTICAL;
        params1.leftMargin = Utils.dp(context, 16);
        params1.rightMargin = Utils.dp(context, 16);

        iconView.setLayoutParams(params1);
        addView(iconView);

        textView = new TextView(context);
        textView.setLines(1);
        textView.setMaxLines(1);
        textView.setSingleLine(true);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);

        LayoutParams params2 = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        params2.gravity = Gravity.START | Gravity.CENTER_VERTICAL;
        params2.leftMargin = Utils.dp(context, 16);
        params2.rightMargin = Utils.dp(context, 16);

        textView.setLayoutParams(params2);
        addView(textView);
    }

    public BottomSheetCell setIcon(@DrawableRes int resId, @ColorInt int color) {
        if (resId != 0) {
            Drawable res = ContextCompat.getDrawable(getContext(), resId);
            if (res != null) {
                res.clearColorFilter();
                res.mutate().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
            }

            iconView.setImageDrawable(res);

            LayoutParams params2 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params2.gravity = Gravity.START | Gravity.CENTER_VERTICAL;
            params2.leftMargin = Utils.dp(getContext(), 72);
            params2.rightMargin = Utils.dp(getContext(), 16);
            textView.setLayoutParams(params2);
        } else {
            LayoutParams params2 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params2.gravity = Gravity.START | Gravity.CENTER_VERTICAL;
            params2.leftMargin = Utils.dp(getContext(), 16);
            params2.rightMargin = Utils.dp(getContext(), 16);
            textView.setLayoutParams(params2);
        }

        return this;
    }

    public BottomSheetCell setText(@NonNull CharSequence text, @ColorInt int color) {
        textView.setText(text);
        textView.setTextColor(color);
        return this;
    }

    public BottomSheetCell setHeight(int height) {
        cellHeight = height;
        return this;
    }

    public BottomSheetCell setDivider(boolean divider) {
        this.divider = divider;
        setWillNotDraw(!divider);
        return this;
    }

    public BottomSheetCell setDividerColor(boolean theme) {
        paint.setColor(!theme ? 0x1F000000 : 0x1FFFFFFF);
        setWillNotDraw(false);
        return this;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = MeasureSpec.makeMeasureSpec(widthMeasureSpec, MeasureSpec.EXACTLY);
        int height = cellHeight + (divider ? 1 : 0);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (divider) {
            canvas.drawLine(getPaddingLeft(), getHeight() - 1, getWidth() - getPaddingRight(), getHeight() - 1, paint);
        }
    }
}
@@ -1216,56 +1216,8 @@
 w;%0A%0A
-import org.michaelbel.bottomsheetdialog.Utils;%0A%0A
 /**%0A
@@ -1229,14 +1229,20 @@
 te: 
-17.02.
+Sat, 17 Feb 
 2018
@@ -1256,16 +1256,20 @@
 e: 00:30
+ MSK
 %0A *%0A * @
@@ -1933,33 +1933,16 @@
 tParams(
-%0A                
 Utils.dp
@@ -1955,32 +1955,16 @@
 xt, 24),
-%0A               
  Utils.d
@@ -1977,25 +1977,16 @@
 ext, 24)
-%0A        
 );%0A     
@@ -2536,33 +2536,16 @@
 tParams(
-%0A                
 ViewGrou
@@ -2572,32 +2572,16 @@
 CONTENT,
-%0A               
  ViewGro
@@ -2608,25 +2608,16 @@
 _CONTENT
-%0A        
 );%0A     
@@ -4817,17 +4817,16 @@
 eSpec);%0A
-%0A
         
@@ -4958,17 +4958,16 @@
 1 : 0);%0A
-%0A
         

Update BottomSheetCell class