using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

public class KitchenManager : MonoBehaviour
{
    [SerializeField] private Button cookingButton;
    [SerializeField] private Button adButton;
    [SerializeField] private KitchenView kitchenView;
    [SerializeField] private BulkOrderIndicatorView orderIndicatorView;
    // Start is called before the first frame update
    void Start()
    {
        // レシピの表示
        cookingButton.OnClickAsObservable().Subscribe(_ =>
        {
            TransitionManager.Instance.LoadSceneAdditive(GameScenes.Recipe);
        });
        
        var gameData = GameDataManager.GameData;
        CoinManager.Instance.ChangeCoin(gameData.Coin);
        HeartMeter.Instance.Initialize();
        HeartMeter.Instance.SetHeart(gameData.Heart);

        kitchenView.Initialize();
        adButton.gameObject.SetActive(false);
        
        // 動画視聴可能かどうかはシーンロード時に確認
        // kitchenView.SetNormalAnimation();
#if UNITY_EDITOR || DEVELOPMENT_BUILD
        adButton.gameObject.SetActive(true);
        kitchenView.SetTiredAnimation();
        adButton.OnClickAsObservable().Subscribe(_ =>
        {
            // 動画視聴POP
            adButton.gameObject.SetActive(false);
            kitchenView.SetNormalAnimation();
        }).AddTo(this);
#endif
        
        // 大量注文
        // 受け取っていない場合アニメーション
        orderIndicatorView.SetIndicate(false, false);
        orderIndicatorView.TappedPhone.ThrottleFirst(TimeSpan.FromSeconds(1f)).Subscribe(_ =>
        {
            TransitionManager.Instance.LoadSceneAdditive(GameScenes.BulkOrder);
        }).AddTo(this);
        
        SetData();

#if UNITY_EDITOR
        Market.StockFlavorLog();
#endif
    }

    private void SetData()
    {
        var gameData = GameDataManager.GameData;
        // 所持レシピ
        if (gameData.MyRecipes == null || gameData.MyRecipes.Length == 0)
        {
            gameData.MyRecipes = new[]
            {
                1,
            };
        }

        // 所持素材
        if (gameData.Material == null || gameData.Material.Count == 0)
        {
            gameData.Material = Enumerable.Range(1, 12).Select(x => new MaterialData(x, 0)).ToList();
        }
        
        // 保存タンク
        if (gameData.StorageTanks == null || gameData.StorageTanks.Count == 0)
        {
            gameData.StorageTanks = new List<StorageTank>
            {
                new StorageTank
                {
                    Id = 1,
                    Capacity = 50,
                    FlavorId = -1,
                }
            };
        }
        GameDataManager.SaveGameData();
    }
}
