package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import kr.co.hanwha.hifive.R;

/**
 * Created by sanghoonkwon on 2018. 1. 23..
 */

public class FontEditText extends android.support.v7.widget.AppCompatEditText {
    public FontEditText(Context context) {
        super(context);
        initialize(context);
    }

    public FontEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public FontEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs, defStyleAttr);
    }

    private void initialize(Context context) {
        try {
//            Typeface font = Typeface.createFromAsset(context.getAssets(), "NanumBarunGothic.otf.mp3");
            Typeface font = Typeface.create(getTypeface(), Typeface.NORMAL);
            if (font != null) {
                this.setTypeface(font);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initialize(Context context, @Nullable AttributeSet attrs, int defStyle) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyle, 0);
        setTypeface(context, typedArray);
    }

    private void initialize(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.FontTextView);
        setTypeface(context, typedArray);
    }

    private void setTypeface(Context context, TypedArray typedArray) {
        String textStyle = typedArray.getString(R.styleable.FontTextView_textStyle);
        Typeface typeface;
        if (textStyle != null && textStyle.equalsIgnoreCase("bold")) {
//            typeface = Typeface.createFromAsset(context.getAssets(), "NanumBarunGothicBold.otf.mp3");
            typeface = Typeface.create(getTypeface(), Typeface.BOLD);
        } else {
//            typeface = Typeface.createFromAsset(context.getAssets(), "NanumBarunGothic.otf.mp3");
            typeface = Typeface.create(getTypeface(), Typeface.NORMAL);
        }
        setTypeface(typeface);

        typedArray.recycle();
    }
}