package com.naz.sdkdemo.weight;

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.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.naz.sdkdemo.R;
import com.naz.sdkdemo.utils.MyUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

/**
 * Description:
 * Author: ydd
 * Date:2020/12/7
 */
public class TitleBar extends FrameLayout {
    private TextView tvTitle;
    private Toolbar toolbar;
    private TextView tvLeft;
    private TextView tvRight;
    private TextView tvRightSecondary;
    private ImageView imgRight;
    private View splitView;
    private TextView tvBack;
    public TitleBar(Context context) {
        this(context, null);
    }

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

    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (getId() == View.NO_ID) {
            setId(R.id.title_view);
        }
        // 最后一个参数 是否将xml上面的布局添加到当前View里面去
        LayoutInflater.from(context).inflate(R.layout.titleview, this, true);
        tvTitle = findViewById(R.id.title_tv);
        toolbar = findViewById(R.id.toolbar);
        tvLeft = findViewById(R.id.tv_left);
        tvRight = findViewById(R.id.tv_right);
        tvRightSecondary = findViewById(R.id.tv_right_two);
        imgRight = findViewById(R.id.right_img);
        splitView = findViewById(R.id.titlebar_split);
        tvBack = findViewById(R.id.tv_back);
        if (null != attrs) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
            tvTitle.setText(ta.getString(R.styleable.TitleBar_tbTitle));
            boolean enable = ta.getBoolean(R.styleable.TitleBar_tbEnableBack, true);
            if (!enable) {
                hiddenNavigationIcon();
            }
            boolean enableSplit = ta.getBoolean(R.styleable.TitleBar_tbEnableSplit, false);
            setSplitVisible(enableSplit);
            ta.recycle();
        }
        post(new Runnable() {
            @Override
            public void run() {
                boolean hasNavIcon = toolbar.getNavigationIcon() != null;
                ViewGroup.LayoutParams lp = tvLeft.getLayoutParams();
                if (lp instanceof Toolbar.LayoutParams && !hasNavIcon) {
                    Toolbar.LayoutParams tlp = (Toolbar.LayoutParams)lp;
                    tlp.leftMargin = MyUtils.dp2px(context, 10);
                    tvLeft.setLayoutParams(tlp);
                }
            }
        });
    }

    public Toolbar getToolBarView() {
        return toolbar;
    }

    public void setBackListener(View.OnClickListener listener) {
        tvBack.setOnClickListener(listener);
    }

    public void hiddenBack() {
        tvBack.setVisibility(View.GONE);
    }

    public void hiddenNavigationIcon() {
        toolbar.setNavigationIcon(null);
        ViewGroup.LayoutParams lp = tvLeft.getLayoutParams();
        if (lp instanceof Toolbar.LayoutParams) {
            Toolbar.LayoutParams tlp = (Toolbar.LayoutParams)lp;
            tlp.leftMargin = MyUtils.dp2px(getContext(), 10);
            tvLeft.setLayoutParams(tlp);
        }
    }

    public void setNavigationIcon(int resId) {
        toolbar.setNavigationIcon(resId);
        ViewGroup.LayoutParams lp = tvLeft.getLayoutParams();
        if (lp instanceof Toolbar.LayoutParams) {
            Toolbar.LayoutParams tlp = (Toolbar.LayoutParams)lp;
            tlp.leftMargin = MyUtils.dp2px(getContext(), 0);
            tvLeft.setLayoutParams(tlp);
        }
    }

    public TextView getLeftView() {
        return tvLeft;
    }

    public TextView getRightSecondaryView() {
        return tvRightSecondary;
    }

    public TextView getRightView() {
        return tvRight;
    }

    public void setLeftText(String text, View.OnClickListener listener) {
        setLeftText(text, 0, listener);
    }

    public void setLeftText(String text, int bgResId, View.OnClickListener listener) {
        tvLeft.setText(text);
        if (bgResId > 0) {
            tvLeft.setBackgroundResource(bgResId);
        } else if (bgResId < 0) {
            tvLeft.setBackground(null);
        }
        tvLeft.setOnClickListener(listener);
        tvLeft.setVisibility(View.VISIBLE);
    }

    public TextView getCenterView() {
        return tvTitle;
    }

    public void setTitle(String text) {
        tvTitle.setText(text);
    }

    public void setTitle(int textResId) {
        tvTitle.setText(textResId);
    }

    public void setTitleIcon(Drawable drawable){
        tvTitle.setCompoundDrawables(null, null, drawable, null);
        tvTitle.setCompoundDrawablePadding(5);
    }

    public void setRightImg(int image, View.OnClickListener listener) {
        imgRight.setVisibility(VISIBLE);
        imgRight.setImageResource(image);
        imgRight.setOnClickListener(listener);
    }

    public void setRightTextSecondary(String str, int color) {
        tvRightSecondary.setVisibility(VISIBLE);
        tvRightSecondary.setText(str);
        tvRightSecondary.setTextColor(color);
    }

    public void setRightText(String str, View.OnClickListener listener) {
        setRightText(str, 0, listener);
    }

    public void setRightText(String str, int bgResId, View.OnClickListener listener) {
        tvRight.setVisibility(VISIBLE);
        tvRight.setText(str);
        if (bgResId > 0) {
            tvRight.setBackgroundResource(bgResId);
        } else if (bgResId < 0) {
            tvRight.setBackground(null);
        }
        tvRight.setOnClickListener(listener);
    }

    public void setRightTextSecondary(String str, View.OnClickListener listener) {
        setRightTextSecondary(str, 0, listener);
    }

    public void setRightTextSecondary(String str, int bgResId, View.OnClickListener listener) {
        tvRightSecondary.setVisibility(VISIBLE);
        tvRightSecondary.setText(str);
        if (bgResId > 0) {
            tvRightSecondary.setBackgroundResource(bgResId);
        }
        tvRightSecondary.setOnClickListener(listener);
    }

    public void setSplitVisible(boolean visible) {
        splitView.setVisibility(visible ? View.VISIBLE : View.GONE);
    }
}