/*******************************************************************************
 * Copyright 2012-present Pixate, Inc.
 * 
 * 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.pixate.freestyle.styling.virtualAdapters;

import java.util.List;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.os.Build;

import com.pixate.freestyle.PixateFreestyle;
import com.pixate.freestyle.styling.PXRuleSet;
import com.pixate.freestyle.styling.stylers.PXStylerContext;
import com.pixate.freestyle.util.PXLog;

/**
 * Virtual adapter for {@link ActionBar} home icon.
 * 
 * <pre>
 * <code>
 *   - icon
 * </code>
 * </pre>
 * 
 * For example, setting the icon for an {@link ActionBar} is done like this:
 * 
 * <pre>
 * action-bar icon {
 *   background-image: url(home.svg);
 * }
 * </pre>
 * 
 * @author Shalom Gibly
 */
public class PXVirtualActionBarIconAdapter extends PXVirtualChildAdapter {

    private static String ELEMENT_NAME = "icon";
    private static PXVirtualActionBarIconAdapter instance;

    /**
     * Returns a singleton instance of this class.
     * 
     * @return An instance of {@link PXVirtualActionBarIconAdapter}
     */
    public static PXVirtualActionBarIconAdapter getInstance() {
        synchronized (PXVirtualActionBarIconAdapter.class) {
            if (instance == null) {
                instance = new PXVirtualActionBarIconAdapter();
            }
        }
        return instance;
    }

    /*
     * (non-Javadoc)
     * @see
     * com.pixate.freestyle.styling.adapters.PXViewStyleAdapter#getElementName
     * (java.lang.Object)
     */
    @Override
    public String getElementName(Object object) {
        return ELEMENT_NAME;
    }

    @Override
    public boolean updateStyle(List<PXRuleSet> ruleSets, List<PXStylerContext> contexts) {
        if (!super.updateStyle(ruleSets, contexts)) {
            return false;
        }

        PXStylerContext context = contexts.get(0);
        ActionBar actionBar = (ActionBar) context.getStyleable();

        if (PixateFreestyle.ICS_OR_BETTER) {
            setIconICS(context, actionBar);
        } else {
            // TODO - add a compatible call for earlier API
            if (PXLog.isLogging()) {
                PXLog.w(getClass().getSimpleName(),
                        "Unable to set ActionBar icon. API version < 14.");
            }
        }
        return true;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private void setIconICS(PXStylerContext context, ActionBar actionBar) {
        actionBar.setIcon(context.getBackgroundImage());
    }
}
