package com.tenlionsoft.baselib.core.widget.views;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.tenlionsoft.baselib.R;

import io.reactivex.rxjava3.internal.operators.maybe.MaybeDoOnLifecycle;
import razerdp.basepopup.BasePopupWindow;

/**
 * 作者: adam
 * 日期: 2021/9/30 - 2:58 下午
 * 邮箱: itgaojian@163.com
 * 描述: 标题栏操作弹窗
 */
public class ControlPopup extends BasePopupWindow {
    public static final int TYPE_CREATE_SEARCH = 18;//新建或搜索
    public static final int TYPE_EDIT_DEL = 19;//编辑或删除
    private OnItemClick mOnItemClick;
    private View mContentView;
    private final TextView mTvCreate;
    private final TextView mTvSearch;
    private final TextView mTvDel;
    private final TextView mTvEdit;

    public ControlPopup(Context context, int type) {
        super(context);
        mContentView = createPopupById(R.layout.popup_control_layout);

        mTvCreate = mContentView.findViewById(R.id.tv_create);
        mTvSearch = mContentView.findViewById(R.id.tv_search);
        mTvDel = mContentView.findViewById(R.id.tv_del);
        mTvEdit = mContentView.findViewById(R.id.tv_edit);
        View line1 = mContentView.findViewById(R.id.split_line_1);
        View line2 = mContentView.findViewById(R.id.split_line_2);
        View line3 = mContentView.findViewById(R.id.split_line_3);
        if (type == TYPE_CREATE_SEARCH) {
            //新建和搜索
            line2.setVisibility(View.GONE);
            line3.setVisibility(View.GONE);
            mTvDel.setVisibility(View.GONE);
            mTvEdit.setVisibility(View.GONE);
        } else if (type == TYPE_EDIT_DEL) {
            //删除和编辑
            line1.setVisibility(View.GONE);
            line2.setVisibility(View.GONE);
            mTvCreate.setVisibility(View.GONE);
            mTvSearch.setVisibility(View.GONE);
        }
        setContentView(mContentView);
        setPopupGravity(Gravity.BOTTOM | Gravity.CENTER);
        if (mOnItemClick != null) {
            setViewClickListener(v -> {
                int id = v.getId();
                if (id == R.id.tv_search) {
                    mOnItemClick.onSearch();
                } else if (id == R.id.tv_create) {
                    mOnItemClick.onCreate();
                } else if (id == R.id.tv_edit) {
                    mOnItemClick.onEdit();
                } else if (id == R.id.tv_del) {
                    mOnItemClick.onDel();
                }
            }, mTvCreate, mTvSearch, mTvEdit, mTvDel);
        }
    }


    public void addOnItemClick(OnItemClick listener) {
        this.mOnItemClick = listener;
    }

    public interface OnItemClick {
        void onEdit();

        void onDel();

        void onCreate();

        void onSearch();
    }
}
