package com.quyunshuo.module.home.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils;
import com.quyunshuo.androidbaseframemvvm.base.utils.commons.StringUtils;
import com.quyunshuo.androidbaseframemvvm.common.bean.ProductDataBean;

import java.util.ArrayList;
import java.util.List;

public class OrderManage {

    private static volatile OrderManage singleton;

    private OrderManage() {
    }

    public static OrderManage getInstance() {
        if (singleton == null) {
            synchronized (OrderManage.class) {
                if (singleton == null) {
                    singleton = new OrderManage();
                }
            }
        }
        return singleton;
    }

    public List<ProductDataBean> getOrderList() {
        String loction = SpUtils.INSTANCE.getString("PRODUCT_DATA", "");
        List<ProductDataBean> list = new ArrayList<>();
        Gson gson = new Gson();
        if (StringUtils.isNotNullOrEmpty(loction)) {
            list = gson.fromJson(loction, new TypeToken<List<ProductDataBean>>() {
            }.getType());
        }

        return list;
    }

    public List<ProductDataBean> getProductsByNo(String targetProductNo) {
        List<ProductDataBean> result = new ArrayList<>();
        for (ProductDataBean bean : getOrderList()) {
            // 处理可能的 null 值（假设 getProductNo() 返回 String）
            if (bean.getProductNo() != null && bean.getProductNo().equals(targetProductNo)) {
                result.add(bean);
            }

        }
        return result;
    }
}