package com.sxyz.rfid.widgets;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sxyz.rfid.R;
import com.sxyz.rfid.presenter.model.OutboundFlowingWaterModel;

import java.util.List;

/**
 * @Description: 出库
 * @Author: chunyuan.wu
 * @CreateDate: 2025/12/31 16:39
 * @Version: 1.0
 */
public class OutBoundPopupWindow extends Dialog {

    private Context mContext;

    public OutBoundPopupWindow(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public OutBoundPopupWindow(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
    }

    protected OutBoundPopupWindow(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mContext = context;
    }

    /**
     * 基本列表弹窗
     *
     * @param datas          内容
     * @param dialogListener 位置回调
     */
    public void init(List<OutboundFlowingWaterModel.RowsBean> datas, final DialogListener dialogListener) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.popup_outbound, null);
        this.setContentView(v);
        this.setCancelable(true);
        RecyclerView rv = v.findViewById(R.id.rv);
        DialogAdapter adapter = new DialogAdapter(R.layout.item_pop, datas);
        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        //窗口位置
        setWindow(getWindow());
    }

    /***
     * 带标题弹窗
     * @param datas   内容
     * @param title    标题
     * @param dialogListener  位置回调
     */

    public void init(List<OutboundFlowingWaterModel.RowsBean> datas, String title, final DialogListener dialogListener) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.popup_outbound, null);
        this.setContentView(v);
        this.setCancelable(true);
        //列表
        RecyclerView rv = v.findViewById(R.id.rv);
        DialogAdapter adapter = new DialogAdapter(R.layout.item_pop, datas);
        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        //标题
//        TextView tv = v.findViewById(R.id.tv_title);
//        tv.setText(title);
        //窗口位置
        setWindow(getWindow());
    }

    /***
     * 自定义标题
     * @param datas
     * @param viewId
     * @param dialogListener
     */

    public void init(List<OutboundFlowingWaterModel.RowsBean> datas, int viewId, final DialogListener dialogListener) {
        LinearLayout v = new LinearLayout(mContext);
        v.setOrientation(LinearLayout.VERTICAL);
        //自定义标题
        View titleView = LayoutInflater.from(mContext).inflate(viewId, null);
        LinearLayout.LayoutParams header_params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        v.addView(titleView, header_params);
        //列表
        RecyclerView rv = new RecyclerView(mContext);
        DialogAdapter adapter = new DialogAdapter(R.layout.item_pop, datas);
        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setId(R.id.rv);
        LinearLayout.LayoutParams rv_params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        v.addView(rv, rv_params);
        this.setContentView(v);
        this.setCancelable(true);
        //窗口位置
        setWindow(getWindow());
    }


    /**
     * 设置窗口位置
     */

    private void setWindow(Window wd) {
        if (wd == null) {
            return;
        }
        WindowManager.LayoutParams params = wd.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wd.setAttributes(params);
        wd.setGravity(Gravity.BOTTOM);
    }

    /**
     * 设置动画
     */
    public void setAnim(int styleId) {
        Window w = getWindow();
        if (w == null) {
            return;
        }
        w.setWindowAnimations(styleId);
    }

    /**
     * 设置位置
     */
    public void setPosition(int position) {
        Window w = getWindow();
        if (w == null) {
            return;
        }
        w.setGravity(position);
    }
}

class DialogListener {
    public void onItemClick(int pos) {
    }
}

class DialogAdapter extends BaseQuickAdapter<OutboundFlowingWaterModel.RowsBean, BaseViewHolder> {

    public DialogAdapter(int layoutResId, List<OutboundFlowingWaterModel.RowsBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, OutboundFlowingWaterModel.RowsBean bean) {
//        helper.setText(R.id.tv_item, item);
    }
}
