﻿using System;
using System.Collections.Generic;
using MiniJSON;
using Stove.StoveSDK;
using UnityEngine;
using UnityEngine.UI;

#if UNITY_ANDROID
        #if MARKET_HUAWEI
        using Stove.StoveSDK.IAP.Huawei;

        #elif MARKET_ONESTORE
        using Stove.StoveSDK.IAP.OneStore;
        
        #else
        using Stove.StoveSDK.IAP.Google;

        #endif
#endif

public class FetchProductDialog : MonoBehaviour
{
    public List<IAPProduct> Products;
    public GameObject buttonPrefab;
    public GameObject panel;

    public static void Show(List<IAPProduct> products)
    {
        Present(products);
    }

    public static void Present(List<IAPProduct> products)
    {
        GameObject fetchProductDialogPanel = GameObject.Find("FetchProductDialogPanel");

        Image image = fetchProductDialogPanel.GetComponent<Image>();
        image.enabled = true;

        FetchProductDialog fetchProductDialog = fetchProductDialogPanel.GetComponent<FetchProductDialog>();
        fetchProductDialog.panel.SetActive(true);
        fetchProductDialog.Products = products;

        GameObject content = fetchProductDialogPanel.transform.Find("Panel").Find("Scroll View").Find("Viewport").Find("Content").gameObject;
        foreach (Transform child in content.transform)
        {
            GameObject.Destroy(child.gameObject);
        }

        int posY = (int)content.transform.position.y;
        posY -= 150;

        foreach(IAPProduct product in products)
        {
            Debug.Log("-------" + product);

            GameObject button = (GameObject)Instantiate(fetchProductDialog.buttonPrefab);
            button.transform.SetParent(content.transform, false);
            button.transform.position = new Vector3(button.transform.position.x, posY, button.transform.position.z);

            Button tempButton = button.GetComponent<Button>();
            tempButton.onClick.AddListener(() => fetchProductDialog.ProductClick(product));

            string productState;
            if (product.State == IAPProduct.ProductState.Available)
            {
                productState = "Available";
            }
            else if (product.State == IAPProduct.ProductState.Purchased)
            {
                productState = "Purchased";
            }
            else if (product.State == IAPProduct.ProductState.Waiting)
            {
                productState = "Waiting";
            }
            else
            {
                productState = "Disabled";
            }

            button.GetComponentInChildren<Text>().text = "[" + product.ProductIdentifier + "]" + product.LocalizedTitle + "(" + productState + ")";
            button.GetComponentInChildren<Text>().fontSize = 40;

            posY -= 150;
        }
    }

    public static void Dismiss()
    {
        GameObject fetchProductDialogPanel = GameObject.Find("FetchProductDialogPanel");

        Image image = fetchProductDialogPanel.GetComponent<Image>();
        image.enabled = false;

        FetchProductDialog fetchProductDialog = fetchProductDialogPanel.GetComponent<FetchProductDialog>();
        fetchProductDialog.panel.SetActive(false);
    }

    public void Cancel()
    {
        Dismiss();
    }

    public void ProductClick(IAPProduct product)
    {
        Debug.Log("------- [Stove-Sample] IAP.StartPurchase -------");

        IAP.StartPurchase(product, (Result result) =>
        {
            Debug.Log("------- [Stove-Sample] IAP.StartPurchase : " + result + " -------");

            if (result.IsSuccessful)
            {
                SSTools.ShowMessage("IAP.StartPurchase Success", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
            else
            {
                SSTools.ShowMessage("IAP.StartPurchase Fail", SSTools.Position.bottom, SSTools.Time.oneSecond);
            }
        });
    }
}
