package com.zswc.creativity.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.core.graphics.ColorUtils;
import androidx.core.view.ViewCompat;

import com.hwangjr.rxbus.RxBus;
import com.ysnows.base.view.BView;
import com.ysnows.base.view.BaseView;

import org.jetbrains.annotations.Nullable;

/**
 * Created by shihuiyun
 * on 2020/9/11
 */
public class BaseLinearLayout extends LinearLayout implements BaseView {
    public BaseLinearLayout(Context context) {
        super(context);
    }
    public BaseLinearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public BaseLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }
    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        Drawable background = getBackground();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setBackground(new RippleDrawable(ColorStateList.valueOf(Color.parseColor("#9B9B9B")), background, new ColorDrawable(Color.parseColor("#8AF9EE"))));
        } else {
            if (background instanceof ColorDrawable) {
                int color = ((ColorDrawable) background).getColor();
                ColorDrawable newColorDrawable = new ColorDrawable(ColorUtils.setAlphaComponent(color, 160));
                StateListDrawable stateListDrawable = new StateListDrawable();
                stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, newColorDrawable);
                stateListDrawable.addState(new int[]{}, background);
                ViewCompat.setBackground(this, stateListDrawable);
                setClickable(true);
            } else if (background instanceof BitmapDrawable) {
                Bitmap bitmap = ((BitmapDrawable) background).getBitmap();
                BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
                bitmapDrawable.setAlpha(160);
                StateListDrawable stateListDrawable = new StateListDrawable();
                stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, bitmapDrawable);
                stateListDrawable.addState(new int[]{}, background);
                ViewCompat.setBackground(this, stateListDrawable);
                setClickable(true);
            }
        }
        register();
    }



    @Nullable
    @Override
    public Context context() {
        return null;
    }


    @Override
    public void toast(@Nullable String msg) {

    }

    @Override
    public void toast(int msgID) {

    }

    @Nullable
    @Override
    public BView view() {
        Context context = getContext();
        if (context instanceof BView) {
            return ((BView) context);
        }
        return null;
    }

    @Override
    public boolean isRxbus() {
        return false;
    }

    @Override
    public void register() {
        if (isRxbus()) {
            RxBus.get().register(this);
        }
    }

    @Override
    public void unRegister() {
        if (isRxbus()) {
            RxBus.get().unregister(this);
        }
    }
}
