package com.kfzs.duanduan.utils.dlg;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.sheep.jiuyan.samllsheep.R;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  TitleBarUtils</p>
 * @ <p>Description:通用标题栏处理工具</p>
 * @ date:  2017/11/10 10:30
 * @ QQ:    315096953
 */

public class TitleBarUtils {


    private static TitleBarUtils mTitleBarUtils;

    public synchronized static TitleBarUtils getInstance() {
        if (mTitleBarUtils == null) {
            mTitleBarUtils = new TitleBarUtils();
        }
        return mTitleBarUtils;
    }


    /**
     * 设置标题
     *
     * @param activity
     * @param title
     * @return
     */
    public TitleBarUtils setTitle(Activity activity, String title) {
        TextView titleView = activity.findViewById(R.id.txt_baseactivity_title);
        titleView.setVisibility(View.VISIBLE);
        titleView.setText(title == null ? "" : title);
        return mTitleBarUtils;
    }

    /**
     * 设置Title文本的颜色
     *
     * @param activity
     * @param color    颜色值
     * @return
     */
    public TitleBarUtils setTitleColor(Activity activity, int color) {
        TextView titleView = activity.findViewById(R.id.txt_baseactivity_title);
        titleView.setTextColor(color);
        return mTitleBarUtils;
    }


    /**
     * 设置点击Title时结束activity
     *
     * @param activity
     * @return
     */
    public TitleBarUtils setTitleFinish(final Activity activity) {
        activity.findViewById(R.id.img_baseactivity_title)
                .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.finish();
            }
        });
        return mTitleBarUtils;
    }


    /**
     * 设置最左边的Title监听
     *
     * @param activity
     * @param drawableID      如果为0显示透明，如果写-1表示资源不变。-2为Gone，其它为显示该值资源
     * @param onClickListener 如果为null,不改监听
     */
    public void setTitleListen(Activity activity, int drawableID, View.OnClickListener onClickListener) {
        TextView titleView = activity.findViewById(R.id.txt_baseactivity_title);
        ImageView imageView= activity.findViewById(R.id.img_baseactivity_title);
        if (drawableID == 0) {
            imageView.setVisibility(View.INVISIBLE);
        } else if (drawableID == -2) {
            imageView.setVisibility(View.GONE);
            return;
        } else if (drawableID != -1) {
            imageView.setImageResource(drawableID);
        }

        if (onClickListener != null) {
            imageView.setOnClickListener(onClickListener);
        }
    }


    /**
     * 设置最右边的普通按钮状态
     *
     * @param activity
     * @param btnText              设置文本
     * @param backgroundColorResId 设置背景,0为透明，-1不变，-2为Gone,其它为ResID
     * @param onClickListener      点击时的操作。
     */
    public void setRightBotton(Activity activity, String btnText, int backgroundColorResId,
                               View.OnClickListener onClickListener) {
        Button button = activity.findViewById(R.id.btn_baseactivity_right);
        ImageButton imageButton = activity.findViewById(R.id.ibtn_baseactivity_right);
        button.setVisibility(View.VISIBLE);
        imageButton.setVisibility(View.GONE);
        if (backgroundColorResId == 0) {
            button.setBackgroundColor(0x00000000);
        } else if (backgroundColorResId == -2) {
            button.setVisibility(View.GONE);
            return;
        } else if (backgroundColorResId != -1) {
            button.setBackgroundColor(backgroundColorResId);
        }

        if (btnText != null) {
            button.setText(btnText);
        }
        //执行回调
        if (onClickListener != null) {
            button.setOnClickListener(onClickListener);
        }
    }


    /**
     * 设置最右边的普通按钮状态
     *
     * @param activity
     * @param backgroundImgResId 设置背景的ResID,为零透明，-1不变,-2为Gone，其它是资源
     * @param onClickListener    点击时的操作。
     */
    public void setRightImgBotton(Activity activity, int backgroundImgResId, View.OnClickListener onClickListener) {
        Button button = activity.findViewById(R.id.btn_baseactivity_right);
        ImageButton imageButton = activity.findViewById(R.id.ibtn_baseactivity_right);

        button.setVisibility(View.GONE);
        imageButton.setVisibility(View.VISIBLE);
        if (backgroundImgResId == 0) {
            imageButton.setImageDrawable(new ColorDrawable(0x00000000));
        } else if (backgroundImgResId == -2) {
            imageButton.setVisibility(View.GONE);
            return;
        } else if (backgroundImgResId != -1) {
            imageButton.setImageResource(backgroundImgResId);
        }
        if (onClickListener != null) {
            imageButton.setOnClickListener(onClickListener);
        }
    }

    /**
     * 设置右边按钮显示状态
     *
     * @param activity
     * @param visibility
     */
    public void setRightBtnVisibility(Activity activity, int visibility) {
        activity.findViewById(R.id.btn_baseactivity_right).setVisibility(visibility);
    }

    /**
     * 设置右边图片按钮显示状态
     *
     * @param activity
     * @param visibility
     */
    public void setRightImgBtnVisibility(Activity activity, int visibility) {
        activity.findViewById(R.id.ibtn_baseactivity_right).setVisibility(visibility);
    }

    /**
     * 获取右边按钮实例
     *
     * @param activity
     * @return
     */
    public Button getRightBtn(Activity activity) {
        return activity.findViewById(R.id.btn_baseactivity_right);
    }

    /**
     * 获取右边图片按钮实例
     *
     * @param activity
     * @return
     */
    public ImageButton getRightImgBtn(Activity activity) {
        return activity.findViewById(R.id.ibtn_baseactivity_right);
    }


    /**
     * 隐藏标题栏所有组件，GONE或VISIBLE
     */
    public void setShowOrHide(Activity activity, boolean isShow) {
        activity.findViewById(R.id.layout_navigationBar)
                .setVisibility(isShow ? View.VISIBLE : View.GONE);
    }
}
