package com.zkjc.common.view;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zkjc.common.R;
import com.zkjc.common.utils.ScreenUtils;


public class CommonTitleBar extends RelativeLayout implements View.OnClickListener {

    private Context mContext;

    private LinearLayout back_return_ll;
    private RelativeLayout rl_top;
    private ImageView mLeftImageView;
    private TextView mLeftTv;

    private TextView mTitleTv;

    public TextView mRightTv;
    private ImageView mRightImageView;
    private LinearLayout mRightLayout;
    private TextView mRightLayoutTv;
    private ImageView mRightLayoutIv;
    public CommonTitleBar(Context context) {
        this(context, null);
    }

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

    public CommonTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;

        // 1. 代码动态添加
        // addView();

        // 2.直接加载写好的布局   ui_common_titlebar   this : parent
        inflate(context, R.layout.ui_common_titlebar, this);
        // LayoutInflater.from(context).inflate(R.layout.ui_common_titlebar,this);
        // View.inflate(context,R.layout.ui_common_titlebar,null);

        // 3. 开始控制头部标题  ， 返回关闭页面
        // 3.1 返回关闭页面  设置点击事件  findViewById
        // activity -->  findViewById   view.findViewById    布局在哪里  this
        back_return_ll = findViewById(R.id.back_return_ll);
        back_return_ll.setOnClickListener(this);
        rl_top=findViewById(R.id.top);

        // 3.2 开始控制头部标题  findViewByID  setText  动态代码设置
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        //左侧
        mLeftImageView = findViewById(R.id.comtop_return);
        mLeftTv = findViewById(R.id.return_tv);
        //右侧
        mRightTv = (TextView) findViewById(R.id.right_tv);
        mRightImageView = findViewById(R.id.right_iv);
        mRightLayout = findViewById(R.id.right_ll);
        mRightLayoutIv = findViewById(R.id.right_ll_iv);
        mRightLayoutTv = findViewById(R.id.right_ll_tv);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back_return_ll) {// 关闭页面  任何地方上下文都可以转成Activity
            ((Activity) mContext).finish();
        }
    }

    /**
     * 设置头部标题
     * @param title
     */
    public void setTitle(String title){
        mTitleTv.setText(title);
    }
    public void setTitleBackgroundColor(){
        rl_top.setBackgroundColor(getResources().getColor(R.color.title_color2));
    }
    public void setmLeftImageView(int resId) {
        if(resId <= 0) {
            this.mLeftImageView.setVisibility(View.GONE);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) this.mLeftTv.getLayoutParams();
            int left = ScreenUtils.dip2px(mContext, mContext.getResources().getDimensionPixelOffset(R.dimen.ui_common_titlebar_left));
            layoutParams.setMargins(left, 0, 0 ,0);
            this.mLeftTv.setLayoutParams(layoutParams);
        }
        else
            this.mLeftImageView.setImageResource(resId);
    }

    public void setmLeftTv(String mLeftTv) {
        this.mLeftTv.setVisibility(View.VISIBLE);
        this.mLeftTv.setText(mLeftTv);
    }
    public void setRightLayout(int resId, String text) {
        mRightLayout.setVisibility(View.VISIBLE);
        mRightLayoutIv.setImageResource(resId);
        mRightLayoutTv.setText(text);
    }
    public void setRightLayout(int layoutId) {
        mRightLayout.removeAllViews();
        mRightLayout.addView(LayoutInflater.from(getContext()).inflate(layoutId, null));
        mRightLayout.setVisibility(View.VISIBLE);
    }
    public void setmRightImageView(int resId, int left, int top, int right, int bottom) {
        mRightImageView.setVisibility(View.VISIBLE);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) this.mRightImageView.getLayoutParams();
        layoutParams.setMargins(left, top, right ,bottom);
        mRightImageView.setLayoutParams(layoutParams);
        mRightImageView.setImageResource(resId);
    }

    public void setmRightImageView(int resId) {
        mRightImageView.setVisibility(View.VISIBLE);
        mRightImageView.setImageResource(resId);
    }

    public void setRightImageViewGone() {
        mRightImageView.setVisibility(View.GONE);
    }

    public void setRightTextViewGone() {
        mRightTv.setVisibility(View.GONE);
    }

    /**
     * 设置右边文字
     */
    public void setRight(String right){
        mRightTv.setVisibility(View.VISIBLE);
        mRightTv.setText(right);
    }

    public String getRightText() {
        if(TextUtils.isEmpty(mRightTv.getText()))
            return "";
        return mRightTv.getText().toString();
    }

    /**
     * 设置左边的点击回调监听
     */
    public void setLeftOnClick(OnClickListener listener){
        back_return_ll.setOnClickListener(listener);
    }

    /**
     * 设置右边的点击回调监听
     */
    public void setRightOnClick(OnClickListener listener){
        mRightImageView.setOnClickListener(listener);
        mRightTv.setOnClickListener(listener);
        mRightLayout.setOnClickListener(listener);
    }

}
