/*
 * 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.graphics.drawable;

import com.android.internal.R;
import com.android.launcher3.icons.MonochromeIconFactory_Accessor;
import com.android.layoutlib.bridge.android.BridgeContext;
import com.android.layoutlib.bridge.impl.RenderAction;
import com.android.tools.layoutlib.annotations.LayoutlibDelegate;

import android.content.res.Resources;
import android.graphics.Canvas;

public class AdaptiveIconDrawable_Delegate {
    public static String sPath;

    /**
     * Delegate that replaces a call to Resources.getString in
     * the constructor of AdaptiveIconDrawable.
     * This allows to pass a non-default value for the mask for adaptive icons.
     */
    @SuppressWarnings("unused")
    public static String getResourceString(Resources res, int resId) {
        if (resId == R.string.config_icon_mask) {
            return sPath;
        }
        return res.getString(resId);
    }

    @LayoutlibDelegate
    public static void draw(AdaptiveIconDrawable thisDrawable, Canvas canvas) {
        BridgeContext context = RenderAction.getCurrentContext();
        if (context.useThemedIcon()) {
            Drawable mono = thisDrawable.getMonochrome();
            if (mono == null && !context.forceMonochromeIcon()) {
                thisDrawable.draw_Original(canvas);
                return;
            }
            int[] colors = getColors();
            if (mono != null) {
                mono.mutate();
                mono.setTint(colors[1]);
            } else {
                mono = MonochromeIconFactory_Accessor.getMonochromeIcon(thisDrawable, colors[1]);
            }
            AdaptiveIconDrawable themedIcon =
                    new AdaptiveIconDrawable(new ColorDrawable(colors[0]), mono);
            themedIcon.onBoundsChange(thisDrawable.getBounds());
            themedIcon.draw_Original(canvas);
        } else {
            thisDrawable.draw_Original(canvas);
        }
    }

    // Adapted from com.android.launcher3.icons.ThemedIconDrawable
    private static int[] getColors() {
        Resources resources = Resources.getSystem();
        int[] colors = new int[2];
        if (resources.getConfiguration().isNightModeActive()) {
            colors[0] = resources.getColor(android.R.color.system_accent2_800, null);
            colors[1] = resources.getColor(android.R.color.system_accent1_200, null);
        } else {
            colors[0] = resources.getColor(android.R.color.system_accent1_100, null);
            colors[1] = resources.getColor(android.R.color.system_accent1_700, null);
        }
        return colors;
    }
}
