package com.cooleshow.base.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.cooleshow.base.R;
import com.cooleshow.base.utils.SizeUtils;
import com.luck.picture.lib.tools.ScreenUtils;

/**
 * @author dengqw
 * @time 2022/6/30 10:36
 * @describe：
 **/
public class ComTitleBar extends FrameLayout {
    private String mTitleText, mRightText;
    private boolean mRightMoreImgVisible, mLeftImgVisible, mRightImgVisible, mRightTextVisible;
    private int mLeftImgSrc, mRightImgSrc, mRightMoreImgSrc;
    private int mTitleTextSize;
    public ImageView mLeftImg, mRightImg, mRightMoreImg;
    public TextView mTitle, mRightTextView;

    public ComTitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.title_style);
        mTitleText = array.getString(R.styleable.title_style_title_text);
        mTitleTextSize = array.getInteger(R.styleable.title_style_title_text_size, 18);
        mRightText = array.getString(R.styleable.title_style_right_text);
        mRightTextVisible = array.getBoolean(R.styleable.title_style_right_visible, false);
        mRightImgVisible = array.getBoolean(R.styleable.title_style_right_img_visible, false);
        mRightImgSrc = array.getResourceId(R.styleable.title_style_right_img_src, 0);
        mLeftImgSrc = array.getResourceId(R.styleable.title_style_left_img_src, R.drawable.icon_back_black);
        mLeftImgVisible = array.getBoolean(R.styleable.title_style_left_img_visible, true);
        mRightMoreImgSrc = array.getResourceId(R.styleable.title_style_right_more_img_src, 0);
        mRightMoreImgVisible = array.getBoolean(R.styleable.title_style_right_more_img_visible, false);
        array.recycle();
        initView();
    }

    public ComTitleBar(@NonNull Context context) {
        this(context, null);
    }

    public ComTitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    private void initView() {
        View titleView = View.inflate(getContext(), R.layout.common_title_layout, this);
        ConstraintLayout rootView = titleView.findViewById(R.id.toolbar);
        mTitle = titleView.findViewById(R.id.title);
        mRightTextView = titleView.findViewById(R.id.tv_right_text);
        mLeftImg = titleView.findViewById(R.id.iv_back);
        mRightImg = titleView.findViewById(R.id.tv_right);
        mRightMoreImg = titleView.findViewById(R.id.tv_right2);

        int statusBarHeight = ScreenUtils.getStatusBarHeight(getContext());
        ViewGroup.LayoutParams lp = rootView.getLayoutParams();
        lp.height = statusBarHeight + ScreenUtils.dip2px(getContext(), 44);
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        rootView.setLayoutParams(lp);
        rootView.setPadding(SizeUtils.dp2px(10),statusBarHeight,SizeUtils.dp2px(10),0);

        setTitleText(mTitleText);
        setLeftImgSrc(mLeftImgSrc);
        setRightImgSrc(mRightImgSrc);
        setRightText(mRightText);
        setRightMoreImgSrc(mRightMoreImgSrc);
    }

    private void setTitleTextSize(int titleTextSize) {
        mTitleTextSize = titleTextSize;
        if (mTitle != null) {
            mTitle.setTextSize(titleTextSize);
        }
    }

    //左边按钮
    private void setLeftImgSrc(int leftImgId) {
        mLeftImgSrc = leftImgId;
        if (mLeftImgSrc != 0) {
            mLeftImg.setImageResource(mLeftImgSrc);
            mLeftImgVisible = true;
        }
        mLeftImg.setVisibility(mLeftImgVisible ? VISIBLE : GONE);
    }

    //右边按钮
    private void setRightImgSrc(int rightImgId) {
        mRightImgSrc = rightImgId;
        if (mRightImgSrc != 0) {
            mRightImg.setImageResource(mRightImgSrc);
            mRightImgVisible = true;
        }
        mRightImg.setVisibility(mRightImgVisible ? VISIBLE : GONE);

    }


    private void setRightMoreImgSrc(int rightMoreImgId) {
        mRightMoreImgSrc = rightMoreImgId;
        if (mRightMoreImgSrc != 0) {
            mRightMoreImg.setImageResource(mRightMoreImgSrc);
            mRightMoreImgVisible = true;
        }
        mRightMoreImg.setVisibility(mRightMoreImgVisible ? VISIBLE : GONE);
    }

    public void setRightText(String rightText) {
        mRightText = rightText;
        mRightTextView.setText(mRightText);
        mRightTextView.setVisibility(VISIBLE);
    }

    //标题
    public void setTitleText(String title) {
        mTitleText = title;
        mTitle.setText(mTitleText);
    }

    public void setLeftClickListener(View.OnClickListener listener) {
        mLeftImg.setOnClickListener(listener);
    }

    public void setRightClickListener(View.OnClickListener listener) {
        mRightImg.setOnClickListener(listener);
    }

    public void setRightMoreClickListener(View.OnClickListener listener) {
        mRightMoreImg.setOnClickListener(listener);
    }
}
