package com.mogo.skin.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

import com.mogo.skin.SkinViewSupport;
import com.mogo.skin.utils.SkinResources;

/**
 * donghongyu
 */
public class SkinImageView extends AppCompatImageView implements SkinViewSupport {
    // 背景图
    private int backgroundResId;
    // 图标
    private int imageResId;

    public SkinImageView(Context context) {
        this(context, null);
    }

    public SkinImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    @Override
    public void setBackgroundResource(int resId) {
        backgroundResId = resId;
        Drawable backgroundRes = SkinResources.getInstance().getDrawable(backgroundResId);
        super.setBackgroundDrawable(backgroundRes);
    }

    @Override
    public void setImageResource(int resId) {
        imageResId = resId;
        Drawable imageRes = SkinResources.getInstance().getDrawable(imageResId);
        super.setImageDrawable(imageRes);
    }

    @Override
    public void applySkin() {
        if (backgroundResId != 0) {
            Drawable backgroundRes = SkinResources.getInstance().getDrawable(backgroundResId);
            setBackgroundDrawable(backgroundRes);
        }
        if (imageResId != 0) {
            Drawable imageRes = SkinResources.getInstance().getDrawable(imageResId);
            setImageDrawable(imageRes);
        }
    }
}