package com.quyunshuo.module.home.utils;

import android.content.Context;
import android.util.Log;

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

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CommodityManage {

    private static volatile CommodityManage singleton;

    private CommodityManage() {
    }

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

    public void init(Context context, String CommodityName) {
        String assetsJson = AssetManager.INSTANCE.getInstance().readJsonFromAssets(context, CommodityName);
        // Log.d("backinfo", "assetsJson:" + assetsJson);
        Boolean productData = SpUtils.INSTANCE.contains("PRODUCT_DATA");
        if (assetsJson.isEmpty()) {//代表app没有更新
            if (!productData) {//代表没有存入过
                String productList = AssetManager.INSTANCE.getInstance().readJsonFromAssets(context, "version_1.json");
                //    Log.d("backinfo", "productList:" + productList);
                SpUtils.INSTANCE.put("PRODUCT_DATA", productList);
            }

        } else {//代表app已经更新
            if (!productData) {//代表没有存入过
                SpUtils.INSTANCE.put("PRODUCT_DATA", assetsJson);
            } else {
                String loction = SpUtils.INSTANCE.getString("PRODUCT_DATA", "");

                // Log.d("backinfo", "loction:" + loction);
                Gson gson = new Gson();
                List<ProductDataBean> list = gson.fromJson(loction, new TypeToken<List<ProductDataBean>>() {
                }.getType());
                List<ProductDataBean> newlist = gson.fromJson(assetsJson, new TypeToken<List<ProductDataBean>>() {
                }.getType());
                boolean isnewProduct = isNewLists(list, newlist);
                if (isnewProduct) {
                    Log.d("backinfo", "有新的商品");
                    List<ProductDataBean> newProduct = mergeLists(list, newlist);
                    SpUtils.INSTANCE.put("PRODUCT_DATA", gson.toJson(newProduct));
                } else {
                    Log.d("backinfo", "没有新的商品");
                }

            }
        }


    }


    private boolean isNewLists(List<ProductDataBean> list1, List<ProductDataBean> list2) {
        // 创建新列表避免修改原始数据
        List<ProductDataBean> mergedList = new ArrayList<>(list1);
        Set<Integer> existingIds = new HashSet<>();

        // 收集第一个列表的所有 productId
        for (ProductDataBean bean : list1) {
            existingIds.add(bean.getProductId());
        }

        // 遍历第二个列表，添加不重复的项
        for (ProductDataBean bean : list2) {
            if (!existingIds.contains(bean.getProductId())) {
                return true;
            }
        }

        return false;
    }

    private List<ProductDataBean> mergeLists(List<ProductDataBean> list1, List<ProductDataBean> list2) {
        // 创建新列表避免修改原始数据
        List<ProductDataBean> mergedList = new ArrayList<>(list1);
        Set<Integer> existingIds = new HashSet<>();

        // 收集第一个列表的所有 productId
        for (ProductDataBean bean : list1) {
            existingIds.add(bean.getProductId());
        }

        // 遍历第二个列表，添加不重复的项
        for (ProductDataBean bean : list2) {
            if (!existingIds.contains(bean.getProductId())) {
                double price = findMaxPrice(list1);
                bean.setPrice(price);
                mergedList.add(bean);
                existingIds.add(bean.getProductId()); // 避免重复添加
            }
        }

        return mergedList;
    }

    private double findMaxPrice(List<ProductDataBean> productList) {
        double maxPrice = 0;


        for (int i = 0; i < productList.size(); i++) {

            double currentPrice = productList.get(i).getPrice();

            if (currentPrice > maxPrice) {
                maxPrice = currentPrice;
            }
        }


        // 处理所有价格都是负数的情况
        return maxPrice;
    }
}