/*
 * Copyright (c) 2016 Stichting Yona Foundation
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package nu.yona.app.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import java.util.HashMap;

import nu.yona.app.R;


public class YonaFontUtils {

    public static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android";
    private static HashMap<String, Typeface> fontCache = new HashMap<>();

    public static void applyCustomFont(TextView customFontTextView, Context context, AttributeSet attrs) {
        TypedArray attributeArray = context.obtainStyledAttributes(
                attrs, R.styleable.YonaFontTextView);

        String fontName = attributeArray.getString(R.styleable.YonaFontTextView_font);

        // check if a special textStyle was used (e.g. extra bold)
        int textStyle = attributeArray.getInt(R.styleable.YonaFontTextView_textStyle, 0);

        Typeface customFont = selectTypeface(context, textStyle);
        customFontTextView.setTypeface(customFont);

        attributeArray.recycle();
    }

    private static Typeface selectTypeface(Context context, int textStyle) {
            /*
            information about the TextView textStyle:
            http://developer.android.com/reference/android/R.styleable.html#TextView_textStyle
            */
        switch (textStyle) {
            case 12: // bold
                return getTypeface("roboto-bold.ttf", context);

            case 10: // extra light, equals @integer/font_style_extra_light
                return getTypeface("roboto-light.ttf", context);

            case 11:
                return getTypeface("roboto-medium.ttf", context);

            case 13: // regular
            default:
                return getTypeface("roboto-Regular.ttf", context);
        }
    }

    public static Typeface getTypeface(String fontname, Context context) {
        Typeface typeface = fontCache.get(fontname);

        if (typeface == null) {
            try {
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + fontname);
            } catch (Exception e) {
                return null;
            }

            fontCache.put(fontname, typeface);
        }

        return typeface;
    }
}
@@ -541,16 +541,237 @@
 tils %7B%0A%0A
+    //font Style TypeFace%0A    private static final int ROBOTO_LIGHT = 10;%0A    private static final int ROBOTO_MEDIUM = 11;%0A    private static final int ROBOTO_BOLD = 12;%0A    private static final int ROBOTO_NORMAL = 13;%0A%0A%0A
     publ
@@ -1873,104 +1873,20 @@
 ase 
-12: // bold%0A                return getTypeface(%22roboto-bold.ttf%22, context);%0A%0A            case 10
+ROBOTO_LIGHT
 : //
@@ -2021,18 +2021,29 @@
    case 
-11
+ROBOTO_MEDIUM
 :%0A      
@@ -2120,18 +2120,132 @@
    case 
-13
+ROBOTO_BOLD: // bold%0A                return getTypeface(%22roboto-bold.ttf%22, context);%0A%0A            case ROBOTO_NORMAL
 : // reg

update PR as per comment