package com.unis.sxzcl.widget;

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

import com.unis.sxzcl.R;
import com.unis.sxzcl.databinding.BaseTitleBarBinding;
import com.xyxsbj.mylibrary.tool.StringTool;

/**
 * 包名: com.unis.sxzcl.widget
 * 类名: BaseTitleBar
 * 作用描述: 自定义标题栏
 * 创建人: mQ
 * 创建时间: 2020/11/23 18:08
 * 更新人:
 * 更新时间: 2020/11/23 18:08
 * 更新说明:
 * 版本号: 1.0
 */
public class BaseTitleBar extends RelativeLayout {

    private BaseTitleBarBinding mBinding;
    private Context mContext;


    public BaseTitleBar(Context context) {
        super(context);
        mContext = context;
        mBinding = BaseTitleBarBinding.inflate(LayoutInflater.from(context));
    }

    public BaseTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        mBinding = BaseTitleBarBinding.inflate(LayoutInflater.from(context), this, true);

        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.baseTitleBar);
        if (attributes != null) {
            String title = attributes.getString(R.styleable.baseTitleBar_barTitle);
            if (StringTool.notEmpty(title))
                mBinding.barTitle.setText(title);
            //背景色
            int barBackground = attributes.getResourceId(
                    R.styleable.baseTitleBar_barBackground, R.drawable.title_bar_background);
            mBinding.root.setBackgroundResource(barBackground);
            //左侧按钮显示
            boolean leftVisible = attributes.getBoolean(R.styleable.baseTitleBar_leftVisible, true);
            mBinding.barLeft.setVisibility(leftVisible ? VISIBLE : GONE);
            //左侧文字颜色
            int leftTextColor = attributes.getColor(
                    R.styleable.baseTitleBar_leftTextColor, Color.WHITE);
            mBinding.barLeft.setTextColor(leftTextColor);
            //左侧文字内容
            String leftText = attributes.getString(R.styleable.baseTitleBar_leftText);
            if (StringTool.notEmpty(leftText))
                mBinding.barLeft.setText(leftText);
            //左侧图标
            int leftDrawable = attributes.getResourceId(R.styleable.baseTitleBar_leftDrawable, -1);
            if (leftDrawable != -1)
                mBinding.barLeft.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, 0, 0, 0);

            //右侧按钮显示
            boolean rightVisible = attributes.getBoolean(R.styleable.baseTitleBar_rightVisible, false);
            mBinding.barRight.setVisibility(rightVisible ? VISIBLE : GONE);
            //右侧文字颜色
            int rightTextColor = attributes.getColor(
                    R.styleable.baseTitleBar_rightTextColor, Color.WHITE);
            mBinding.barRight.setTextColor(rightTextColor);
            //右侧文字内容
            String rightText = attributes.getString(R.styleable.baseTitleBar_rightText);
            if (StringTool.notEmpty(rightText))
                mBinding.barRight.setText(rightText);
            //右侧图标
            int rightDrawable = attributes.getResourceId(R.styleable.baseTitleBar_rightDrawable, -1);
            if (rightDrawable != -1)
                mBinding.barRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, rightDrawable, 0);

        }

        mBinding.vStatus.setHeight(getStatusHeight());
    }

    public int getStatusHeight() {
        //获取状态栏高度
        int height = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            height = getResources().getDimensionPixelSize(resourceId);
        }
//        LogTool.i("状态栏高度:" + height);
        return height;
    }

    /**
     * 获取左侧控件
     */
    public TextView getBarLeft() {
        return mBinding.barLeft;
    }

    /**
     * 获取右侧控件
     */
    public TextView getBarRight() {
        return mBinding.barRight;
    }

    /**
     * 获取标题控件
     */
    public TextView getBarTitle() {
        return mBinding.barTitle;
    }

    /**
     * 设置标题文本
     */
    public BaseTitleBar setTitle(String title) {
        mBinding.barTitle.setText(title);
        return this;
    }

    /**
     * 设置标题左侧图标
     */
    public BaseTitleBar setTitleLeftDrawable(Drawable drawable) {
        mBinding.barTitle.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        return this;
    }
    /**
     * 设置标题右侧图标
     */
    public BaseTitleBar setTitleRightDrawable(Drawable drawable) {
        mBinding.barTitle.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        return this;
    }

    /**
     * 左侧控件是否显示
     */
    public BaseTitleBar setLeftVisible(boolean visible) {
        mBinding.barLeft.setVisibility(visible ? VISIBLE : GONE);
        return this;
    }

    /**
     * 右侧控件是否显示
     */
    public BaseTitleBar setRightVisible(boolean visible) {
        mBinding.barRight.setVisibility(visible ? VISIBLE : GONE);
        return this;
    }

    /**
     * 设置左侧文本
     */
    public BaseTitleBar setLeftText(String leftText) {
        mBinding.barLeft.setText(leftText);
        return this;
    }

    /**
     * 设置左侧图标
     */
    public BaseTitleBar setLeftDrawable(Drawable drawable) {
        mBinding.barLeft.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        return this;
    }

    /**
     * 设置右侧文本
     */
    public BaseTitleBar setRightText(String rightText) {
        setRightVisible(true);
        mBinding.barRight.setText(rightText);
        return this;
    }

    /**
     * 设置标题控件点击监听
     */
    public BaseTitleBar setTitleClickListener(OnClickListener l) {
        mBinding.barTitle.setOnClickListener(l);
        return this;
    }

    /**
     * 设置左侧控件点击监听
     */
    public BaseTitleBar setLeftClickListener(OnClickListener l) {
        mBinding.barLeft.setOnClickListener(l);
        return this;
    }

    /**
     * 设置右侧控件点击监听
     */
    public BaseTitleBar setRightClickListener(OnClickListener l) {
        mBinding.barRight.setOnClickListener(l);
        return this;
    }
}
