package com.sxyz.rfid.widgets;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
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.FrameLayout;
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 com.sxyz.rfid.presenter.model.OutboundFridModel;

import java.util.List;

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

    private Context mContext;
    private Display display;

    public OutBoundDialog(@NonNull Context context) {
        super(context);
        mContext = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public OutBoundDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    protected OutBoundDialog(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mContext = context;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    DialogAdapter adapter;

    /**
     * 基本列表弹窗
     *
     * @param datas          内容
     * @param dialogListener 位置回调
     */
    public void init(List<OutboundFridModel.RowsBean> datas, final DialogListener dialogListener) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.popup_outbound, null);
        this.setContentView(v);
        this.setCancelable(false);
        LinearLayout lLayout_bg = v.findViewById(R.id.lLayout_bg);
        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth() * 0.3), (int) (display.getHeight() * 0.9)));
        RecyclerView rv = v.findViewById(R.id.rv);
        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<OutboundFridModel.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);
        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<OutboundFridModel.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);
        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());
    }

    //添加数据
    public void addData(List<OutboundFridModel.RowsBean> datas) {
        adapter.addData(datas);
    }

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

    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 DialogAdapter extends BaseQuickAdapter<OutboundFridModel.RowsBean, BaseViewHolder> {

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

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