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.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.sxyz.rfid.R;
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 TipsDialog extends Dialog {

    private Context mContext;
    private Display display;

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

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

    protected TipsDialog(@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(int viewId, String msg) {
        View v = LayoutInflater.from(mContext).inflate(viewId, 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.5)));
        //设置弹窗内容
        TextView txt_msg = v.findViewById(R.id.txt_msg);
        txt_msg.setText(msg);

        //窗口位置
        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);
    }
}
