package macampcorp.macamp.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by ptitcois on 06/06/17.
 */
public class FontChangeCrawler
{
    private Typeface typeface;

    public FontChangeCrawler(Typeface typeface)
    {
        this.typeface = typeface;
    }

    public FontChangeCrawler(AssetManager assets, String assetsFontFileName)
    {
        typeface = Typeface.createFromAsset(assets, assetsFontFileName);
    }

    public  void replaceFonts(ViewGroup viewTree)
    {
        View child;
        for(int i = 0; i < viewTree.getChildCount(); ++i)
        {
            child = viewTree.getChildAt(i);
            if(child instanceof ViewGroup)
            {
                // recursive call
                replaceFonts((ViewGroup)child);
            }
            else if(child instanceof TextView)
            {
                // base case
                ((TextView) child).setTypeface(typeface);
            }
        }
    }

    public  void replaceFontsOnView(View child)
    {
        if(child instanceof TextView)
        {
            ((TextView) child).setTypeface(typeface);
        }

    }

    public static void setFont(Activity app, String font)
    {
        FontChangeCrawler fontChanger = new FontChangeCrawler(app.getAssets(), font);
        fontChanger.replaceFonts((ViewGroup)app.findViewById(android.R.id.content));
    }

    public static void setFont(Activity app)
    {
        FontChangeCrawler fontChanger = new FontChangeCrawler(app.getAssets(), "font.ttf");
        if(fontChanger!=null) fontChanger.replaceFonts((ViewGroup)app.findViewById(android.R.id.content));
    }

    public static void setFont(ViewGroup v, Context app)
    {
        FontChangeCrawler fontChanger = new FontChangeCrawler(app.getAssets(), "font.ttf");
        fontChanger.replaceFonts(v);
    }

    public static void setFont(ViewGroup v, Context app, String font)
    {
        FontChangeCrawler fontChanger = new FontChangeCrawler(app.getAssets(), font);
        fontChanger.replaceFonts(v);
    }



    public static void setFont(View v, Context app)
    {
        FontChangeCrawler fontChanger = new FontChangeCrawler(app.getAssets(), "font.ttf");
        fontChanger.replaceFontsOnView(v);
    }

    public static void setFont(View v, Context app, String font)
    {
        FontChangeCrawler fontChanger = new FontChangeCrawler(app.getAssets(), font);
        fontChanger.replaceFontsOnView(v);
    }




}