/*
 * Copyright (C) 2016 Phillip Hsu
 *
 * 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.philliphsu.bottomsheetpickers.time.grid;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;

import com.philliphsu.bottomsheetpickers.R;
import com.philliphsu.bottomsheetpickers.Utils;

public class MinutesGrid extends NumbersGrid {
    private static final String TAG = "MinutesGrid";

    private ImageButton mMinusButton;
    private ImageButton mPlusButton;

    public MinutesGrid(Context context) {
        super(context);
    }

    public MinutesGrid(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MinutesGrid(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mMinusButton = (ImageButton) findViewById(R.id.dec_min);
        mPlusButton = (ImageButton) findViewById(R.id.inc_min);
        // We're not doing method binding because we don't have IDs set on these buttons.
        mMinusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int value = getSelection() - 1;
                if (value < 0)
                    value = 59;
                setSelection(value);
                mSelectionListener.onNumberSelected(value);
            }
        });
        mPlusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int value = getSelection() + 1;
                if (value == 60)
                    value = 0;
                setSelection(value);
                mSelectionListener.onNumberSelected(value);
            }
        });
    }

    @Override
    public void setSelection(int value) {
        super.setSelection(value);
        if (value % 5 == 0) {
            // The new value is one of the predetermined minute values
            int positionOfValue = value / 5;
            setIndicator(getChildAt(positionOfValue));
        } else {
            clearIndicator();
        }
    }

    @Override
    void setTheme(Context context, boolean themeDark) {
        super.setTheme(context, themeDark);
        if (themeDark) {
            // Resources default to dark-themed color (#FFFFFF)
            // If vector fill color is transparent, programmatically tinting will not work.
            // Since dark-themed active icon color is fully opaque, use that color as the
            // base color and tint at runtime as needed.
            mMinusButton.setImageResource(R.drawable.ic_minus_circle_24dp);
            mPlusButton.setImageResource(R.drawable.ic_add_circle_24dp);
        } else {
            // Tint drawables
            final int colorActiveLight = ContextCompat.getColor(context, R.color.icon_color_active_light);
            mMinusButton.setImageDrawable(Utils.getTintedDrawable(
                    context, R.drawable.ic_minus_circle_24dp, colorActiveLight));
            mPlusButton.setImageDrawable(Utils.getTintedDrawable(
                    context, R.drawable.ic_add_circle_24dp, colorActiveLight));
        }
    }
}

@@ -680,16 +680,103 @@
 ontext;%0A
+import android.graphics.drawable.Drawable;%0Aimport android.support.annotation.ColorInt;%0A
 import a
@@ -910,16 +910,49 @@
 eButton;
+%0Aimport android.widget.ImageView;
 %0A%0Aimport
@@ -3040,16 +3040,17 @@
     if (
+!
 themeDar
@@ -3070,372 +3070,155 @@
     
-// Resources default to dark-themed color (#FFFFFF)%0A            // If vector fill color is transparent, programmatically tinting will not work.%0A            // Since dark-themed active icon color is fully opaque, use that color as the%0A            // base color and tint at runtime as needed.%0A            mMinusButton.setImageResource(R.drawable.ic_minus_circle_24dp
+final int colorActiveLight = ContextCompat.getColor(context, R.color.icon_color_active_light);%0A            applyTint(mMinusButton, colorActiveLight
 );%0A 
@@ -3220,32 +3220,42 @@
 t);%0A            
+applyTint(
 mPlusButton.setI
@@ -3253,55 +3253,26 @@
 tton
-.setImageResource(R.drawable.ic_add_circle_24dp
+, colorActiveLight
 );%0A 
@@ -3283,210 +3283,169 @@
    %7D
- else %7B
 %0A    
-   
+%7D%0A%0A
     
- 
 // T
-int drawables%0A            final int colorActiveLight = ContextCompat.getColor(context, R.color.icon_color_active_light);%0A            mMinusButton.setImageDrawable(Utils.getTinted
+ODO: Consider moving this to Utils.%0A    private static void applyTint(ImageView view, @ColorInt int color) %7B%0A        Drawable drawable = view.get
 Draw
@@ -3441,32 +3441,34 @@
 iew.getDrawable(
+);
 %0A               
@@ -3464,237 +3464,80 @@
     
-            context, R.drawable.ic_minus_circle_24dp, colorActiveLight));%0A            mPlusButton.setImageDrawable(Utils.getTintedDrawable(%0A                    context, R.drawable.ic_add_circle_24dp, colorActiveLight));%0A        %7D
+Utils.setTint(drawable, color);%0A        view.setImageDrawable(drawable);
 %0A   

Fix crashing of light grid time picker below API 21