package com.example.cloudmusic.textNewUI;

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.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.cloudmusic.R;

public class Jive extends LinearLayout {

    private Button leftBtn;
    private Button rightBtn;
    private TextView titleTv;
    private String titleText;
    private float titleTextSize;
    private int titleTextColor;
    private String leftText;
    private float leftTextSize;
    private int leftTextColor;
    private Drawable leftBackground;
    private String rightText;
    private float rightTextSize;
    private int rightTextColor;
    private Drawable rightBackground;

    public Jive(Context context) {
        super(context);
    }

    public Jive(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context.obtainStyledAttributes(attrs,R.styleable.TopBar));
        View view = LayoutInflater.from(context).inflate(R.layout.text2, this);
        leftBtn = view.findViewById(R.id.left_btn);
        rightBtn = view.findViewById(R.id.right_btn);
        titleTv = view.findViewById(R.id.title_tv);

        titleTv.setText(titleText);
        titleTv.setTextSize(titleTextSize);
        titleTv.setTextColor(titleTextColor);
        //设置左边按钮属性
        leftBtn.setText(leftText);
        leftBtn.setTextSize(leftTextSize);
        leftBtn.setTextColor(leftTextColor);
        leftBtn.setBackgroundDrawable(leftBackground);
        //设置右边按钮属性
        rightBtn.setText(rightText);
        rightBtn.setTextSize(rightTextSize);
        rightBtn.setTextColor(rightTextColor);
        rightBtn.setBackgroundDrawable(rightBackground);
        //左右按钮点击事件
        leftBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                onTopBarClickListener.leftClick();
            }
        });
        rightBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                onTopBarClickListener.rightClick();
            }
        });
    }

    onTopBarClickListener onTopBarClickListener = null;

    public void setOnTopBarClickListener(onTopBarClickListener onTopBarClickListener) {
        this.onTopBarClickListener = onTopBarClickListener;
    }

    private void initAttrs(TypedArray ta) {
        titleText = ta.getString(R.styleable.TopBar_titleText);
        titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize, 25);
        titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);

        leftText = ta.getString(R.styleable.TopBar_leftText);
        leftTextSize = ta.getDimension(R.styleable.TopBar_leftTextSize, 16);
        leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);
        leftBackground = ta.getDrawable(R.styleable.TopBar_leftBackground);

        rightText = ta.getString(R.styleable.TopBar_rightText);
        rightTextSize = ta.getDimension(R.styleable.TopBar_rightTextSize, 16);
        rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);
        rightBackground = ta.getDrawable(R.styleable.TopBar_rightBackground);

        ta.recycle();
    }

    public interface onTopBarClickListener{
        void leftClick();
        void rightClick();
    }
}
