package com.renrenliao.im.widget.view;

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

import androidx.constraintlayout.widget.ConstraintLayout;

import com.renrenliao.im.R;
import com.renrenliao.im.utils.LogUtils;

/**
 * *@ClassName CustomToolBar
 * *@Description
 * *@Author gan
 * *@Date 2022/5/23 12:48
 **/
public class CustomToolBar extends ConstraintLayout {

  //  private Context mContext;
    ConstraintLayout custom_toolbar;
    ImageView iv_title_left;
    TextView tv_title_left;
    ImageView iv_left_close;
    TextView tv_title_center;
    ImageView iv_title_search;
    ImageView iv_title_right;
    Button btn_title_right;
    ImageView iv_right_more;
    TextView tv_title_right;

    private int bkg;
    private boolean is_iv_title_left = true;
    private String str_tv_title_left;
    private boolean is_iv_left_close;
    private String str_tv_title_center;
    private boolean is_iv_title_right;
    private int ic_iv_title_right;
    private boolean is_btn_title_right;
    private boolean is_iv_title_search;
    private boolean is_iv_right_more;
    private String str_tv_title_right;

    public CustomToolBar(Context context) {
        this(context, null);
    }

    public CustomToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
      //  mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.layout_common_bar, this, true);
        custom_toolbar = view.findViewById(R.id.custom_toolbar);
        iv_title_left = view.findViewById(R.id.iv_title_left);//是否显示  默认显示
        tv_title_left = view.findViewById(R.id.tv_title_left);//显示类容
        iv_left_close = findViewById(R.id.iv_left_close);  //是否显示   默认不显示
        tv_title_center = view.findViewById(R.id.tv_title_center); //显示类容
        iv_title_search = view.findViewById(R.id.iv_title_search); //是否显示   默认不显示
        iv_title_right = view.findViewById(R.id.iv_title_right); //是否显示  默认不显示   显示类容
        btn_title_right = view.findViewById(R.id.btn_title_right);  //是否显示 默认不显示
        iv_right_more = view.findViewById(R.id.iv_right_more);  //是否显示  默认不显示
        tv_title_right = view.findViewById(R.id.tv_title_right);//显示类容


        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomToolBar);
        bkg = ta.getResourceId(R.styleable.CustomToolBar_bkg,0);
        is_iv_title_left = ta.getBoolean(R.styleable.CustomToolBar_is_iv_left, true);
        str_tv_title_left = ta.getString(R.styleable.CustomToolBar_str_tv_title_left);
        is_iv_left_close = ta.getBoolean(R.styleable.CustomToolBar_is_iv_left_close,false);
        str_tv_title_center = ta.getString(R.styleable.CustomToolBar_str_tv_title_center);
        is_iv_title_right = ta.getBoolean(R.styleable.CustomToolBar_is_iv_title_right,false);
        ic_iv_title_right = ta.getResourceId(R.styleable.CustomToolBar_ic_iv_title_right,0);
        is_btn_title_right = ta.getBoolean(R.styleable.CustomToolBar_is_btn_title_right,false);
        is_iv_title_search = ta.getBoolean(R.styleable.CustomToolBar_is_iv_title_search,false);
        is_iv_right_more = ta.getBoolean(R.styleable.CustomToolBar_is_iv_right_more,false);
        str_tv_title_right = ta.getString(R.styleable.CustomToolBar_str_tv_title_right);

        ta.recycle();
        init();
    }

    private void init(){

        if (bkg!=0){
            custom_toolbar.setBackgroundResource(bkg);
        }
        iv_title_left.setVisibility(is_iv_title_left==true?View.VISIBLE:View.GONE);

        if (null!=str_tv_title_left){
            tv_title_left.setText(str_tv_title_left);
        }

        iv_left_close.setVisibility(is_iv_left_close==true?View.VISIBLE:View.GONE);

        if (null!=str_tv_title_center){
            tv_title_center.setText(str_tv_title_center);
        }

        iv_title_right.setVisibility(is_iv_title_right==true?View.VISIBLE:View.GONE);
        if (ic_iv_title_right!=0){
            iv_title_right.setImageResource(ic_iv_title_right);
        }

        iv_title_search.setVisibility(is_iv_title_search==true?View.VISIBLE:View.GONE);

        btn_title_right.setVisibility(is_btn_title_right==true?View.VISIBLE:View.GONE);

        iv_right_more.setVisibility(is_iv_right_more==true?View.VISIBLE:View.GONE);

        if (str_tv_title_right!=null){
            tv_title_right.setText(str_tv_title_right);
        }
    }

    public void setTvLeft(String str){
        tv_title_left.setText(str);
    }

    public void setOnBackListener(OnBackListener l ){
        iv_title_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnBackListener = l;
                mOnBackListener.onBackClick();
            }
        });
    }

    public OnBackListener mOnBackListener;
//    public void setOnBackListener( OnBackListener l) {
//        mOnBackListener = l;
//    }
    public interface OnBackListener {
        void onBackClick();
    }
}
