/*
 * COPYRIGHT (c) 2018 All rights reserved by HANWHA LIFE.
 */
package kr.co.zumo.app.lifeplus.supervisor;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.content.res.ResourcesCompat;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import kr.co.zumo.app.R;

/**
 * 앱에서 사용할 폰트 설정
 */

/**
 * TypefaceUtil
 * <pre>
 * </pre>
 *
 * @author 민효동
 * @version 1.0
 * @history 민효동   [2018. 10. 8.]   [최초 작성]
 * @since 2018. 10. 8.
 */

public class TypefaceUtil {
  private static final String REGULAR_FONT_ASSET_NAME = "font/droid_sans.ttf"; // in assets folder
  private static final String MEDIUM_FONT_ASSET_NAME = "font/bmyeonsung.otf"; // in assets folder

  //  private static final String REGULAR_FONT_FILE = "/system/fonts/droid_sans.ttf"; // 사용 가능하지만 없는 유저도 있을 수 있어서 asset 에 임베딩

  private static final String FONT_DEFAULT = "DEFAULT";
  private static final String FONT_DEFAULT_BOLD = "DEFAULT_BOLD";
  private static final String FONT_SERIF = "SERIF";
  private static final String FONT_SANS_SERIF = "SANS_SERIF";
  private static final String FONT_MONOSPACE = "MONOSPACE";

  public void init(Context context) {
    try {
//      Typeface regular = createFont(context, REGULAR_FONT_ASSET_NAME);
//      Typeface medium = createFont(context, MEDIUM_FONT_ASSET_NAME);
      Typeface regular = ResourcesCompat.getFont(context, R.font.droid_sans);
//      Typeface medium = ResourcesCompat.getFont(context, R.font.bmyeonsung);

      Map<String, Typeface> fonts = new HashMap<>();
      fonts.put(context.getString(R.string.lifeplus_font_sans_serif), regular);
      fonts.put(context.getString(R.string.lifeplus_font_serif), regular);
      overrideFonts(fonts);

      overrideFont(FONT_DEFAULT, regular);
      overrideFont(FONT_SERIF, regular);
      overrideFont(FONT_SANS_SERIF, regular);
      overrideFont(FONT_MONOSPACE, regular);
      overrideFont(FONT_DEFAULT_BOLD, regular);
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  /**
   * assets 폴더의 font 를 가져온다.
   *
   * @param context Context
   * @param path assets 을 제외한 경로
   * @return
   */
  private Typeface createFont(Context context, String path) {
    return Typeface.createFromAsset(context.getAssets(), path);
  }

  /**
   * font 파일을 가져온다.
   *
   * @param path
   * @return
   */
  private Typeface createFromFile(String path) {
    return Typeface.createFromFile(path);
  }

  private void overrideFonts(Map<String, Typeface> typefaces) {
    try {
      final Field field = Typeface.class.getDeclaredField("sSystemFontMap");
      field.setAccessible(true);

      Map<?, ?> tempMap = (Map<?, ?>) field.get(null);
      Map<String, Typeface> oldFonts = new HashMap<>();

      for (Map.Entry<?, ?> entry : tempMap.entrySet()) {
//        Log.w("APP# TypefaceUtil | overrideFonts", "|" + entry.getKey());
        oldFonts.put((String) entry.getKey(), (Typeface) entry.getValue());
      }

      oldFonts.putAll(typefaces);
      field.set(null, oldFonts);
      field.setAccessible(false);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void overrideFont(String fontName, Typeface typeface) {
    try {
      final Field field = Typeface.class.getDeclaredField(fontName);
      field.setAccessible(true);
      field.set(null, typeface);
      field.setAccessible(false);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
