package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import kr.co.hanwha.hifive.R;

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

public class FontTextView extends AppCompatTextView {
    Color mColor;

    public FontTextView(Context context) {
        super(context);
        initialize(context);
    }

    public FontTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context, attrs);
    }

    public FontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs, defStyleAttr);
    }

    private void initialize(Context context) {
        if (isInEditMode()) return;

        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) {
        if (isInEditMode()) return;

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyle, 0);
        setTypeface(context, typedArray);
    }

    private void initialize(Context context, @Nullable AttributeSet attrs) {
        if (isInEditMode()) return;

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.FontTextView);
        setTypeface(context, typedArray);
    }

    private void setTypeface(Context context, TypedArray typedArray) {
        if (isInEditMode()) return;

        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();
    }
    /*
<co.kr.hanwha.NGW.custom.CustomTextView
                xmlns:app="http://schemas.android.com/apk/res/com.konai.konamoney"
                android:id="@+id/walletMain_textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="3. 내 지갑"
                android:textColor="@drawable/temp_button_text_black"
                android:textSize="20sp"
                app:textStyle="bold" />
 */
}
