package com.khmer9.lotto.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.khmer9.lotto.R;

public class RectangleView  extends ConstraintLayout {
    ImageView imageViewLoto;
    TextView tvValue;
    ConstraintLayout bgView;

    public RectangleView(@NonNull Context context) {
        super(context);
        init();

    }

    public RectangleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        setStyle(attrs);

    }

    public RectangleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        setStyle(attrs);
    }

    public RectangleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
        setStyle(attrs);
    }
    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(
                R.layout.view_rectangle, this, true);
        bgView = view.findViewById(R.id.iv_bg);
        imageViewLoto = view.findViewById(R.id.iv_logo) ;
        tvValue = view.findViewById(R.id.tv_value);

    }

    private void setStyle(AttributeSet attrs){
        TypedArray a = getContext().getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.RectangleView,
                0, 0);

        try {
             Drawable bgDrawable = a.getDrawable(R.styleable.RectangleView_bgImage);
            int bigSmall_resID = a.getResourceId(R.styleable.RectangleView_bigSmallImage, R.drawable.btn_rectangle_blue);
            String textRange = a.getString(R.styleable.RectangleView_rangeNumber);
            bgView.setBackground(bgDrawable);
            imageViewLoto.setImageResource(bigSmall_resID);
            tvValue.setText(textRange);

        } finally {
            a.recycle();
        }
    }

}
