using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Xsolla.Catalog;
using Xsolla.Core;

namespace NeoriginPCSDK
{
    public class NeoriginPlatformPcServer : NeoriginPlatformBaseServer
    {
        public override void GetPayItems(NeoriginSDK.GetPayItemsCallBack callBack = null)
        {
            base.GetPayItems(callBack);
            XsollaCatalog.GetPaginatedItems((storeItems) =>
            {
                if (storeItems != null && storeItems.items != null && storeItems.items.Length > 0)
                {
                    List<NeoriginSDKCallbackDataPayItem> payItemsList = new List<NeoriginSDKCallbackDataPayItem>();
                    Debug.Log($"NeoriginPlatformServer xsolla GetPayItems success:{storeItems.items.Length} items");
                    StoreItem[] items = storeItems.items;

                    foreach (var item in items)
                    {
                        if (item != null)
                        {
                            string sku = item.sku ?? "";
                            string name = item.name ?? "";
                            string type = item.type ?? "";
                            string description = item.description ?? "";

                            if (string.IsNullOrEmpty(sku))
                            {
                                Debug.LogWarning($"NeoriginPlatformServer xsolla Store item sku is null");
                                continue;
                            }
                            string amount = "";
                            string currency = "";

                            if (item.price != null)
                            {
                                amount = item.price.amount ?? "";
                                currency = item.price.currency ?? "";
                            }
                            if (string.IsNullOrEmpty(amount) || string.IsNullOrEmpty(currency))
                            {
                                Debug.LogWarning($"NeoriginPlatformServer xsolla Store item amount&currency is null");
                                continue;
                            }
                            NeoriginSDKCallbackDataPayItem payItem = new NeoriginSDKCallbackDataPayItem();
                            payItem.product_id = sku;
                            payItem.price = float.Parse(amount);
                            payItem.symbol = currency;
                            payItem.title = name;
                            payItem.localizedDescription = description;
                            payItem.countryCode = "";
                            payItem.priceCurrencyCode = currency;
                            payItemsList.Add(payItem);

                            Debug.Log($"NeoriginPlatformServer xsolla Store item: sku:{sku},name:{name},price:{amount},currency:{currency},type:{type},description:{description}");
                        }
                    }
                    if (payItemsList.Count == 0)
                    {
                        Debug.Log($"NeoriginPlatformServer xsolla GetPayItems no valid items");
                        callBack?.Invoke(NeoriginSDKStatusCode.PayErrorItems, "NeoriginPlatformServer xsolla GetPayItems no valid items");
                        return;
                    }

                    callBack?.Invoke(NeoriginSDKStatusCode.Success, "Success", payItemsList.ToArray());
                }
                else
                {
                    Debug.Log($"NeoriginPlatformServer xsolla GetPayItems no items");
                    callBack?.Invoke(NeoriginSDKStatusCode.PayNoItems, "NeoriginPlatformServer xsolla GetPayItems no items");
                }
            }, (error) =>
            {
                Debug.Log($"NeoriginPlatformServer GetPayItems xsolla Error message: {error.errorMessage},errorCode:{error.errorCode},statusCode:{error.statusCode}");
                callBack?.Invoke(code: NeoriginSDKStatusCode.InternalError, msg: $"GetPayItems xsolla Error message: {error.errorMessage},errorCode:{error.errorCode},statusCode:{error.statusCode}");
                return;
            }, 50, 0);
        }
    }
}
