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

public class DebugOptionManager : MonoBehaviour
{
    // シーン取得
    public static readonly string DebugPrevSceneTag = "DebugPrevScene";
    [SerializeField] private Button resetButton;
    [SerializeField] private Button backButton;
    [SerializeField] private Button resetFieldButton;
    [SerializeField] private Button resetMaterialButton;
    [SerializeField] private Button refillManyProductButton;
    [SerializeField] private Button refillLittleProductButton;
    [SerializeField] private Toggle fastGrowingToggle;
    [SerializeField] private Toggle manyWalkerToggle;
    [SerializeField] private InputField coinCount;
    [SerializeField] private InputField heartCount;
#if UNITY_EDITOR || DEVELOPMENT_BUILD
    // Start is called before the first frame update
    void Start()
    {
        if (!LocalCacheManager.Exists(DebugPrevSceneTag))
        {
            TransitionManager.Instance.LoadScene(GameScenes.Main);
        }
        var prevScene = LocalCacheManager.Load<GameScenes>(DebugPrevSceneTag);
        resetButton.OnClickAsObservable().Subscribe(_ =>
        {
            GameDataManager.ResetData();
            TransitionManager.Instance.LoadScene(GameScenes.DebugOption);
        }).AddTo(this);

        backButton.OnClickAsObservable().Subscribe(_ =>
        {
            GameDataManager.SaveGameData();
            TransitionManager.Instance.LoadScene(prevScene);
        });

        var gameData = GameDataManager.GameData;
        resetFieldButton.OnClickAsObservable().Subscribe(_ =>
        {
            gameData.PlantLineDataList = new List<PlantLineData>
            {
                new PlantLineData(PlantLineType.Center, CornFieldRank.Rank1),
            };
            gameData.SeedlingDataList = new List<SeedlingProgressData>();;
            gameData.MachineLevel = 1;
            gameData.CornSeed = 0;
        }).AddTo(this);

        resetMaterialButton.OnClickAsObservable().Subscribe(_ =>
        {
            // 所持素材
            gameData.Material = new List<MaterialData>{new MaterialData{Id = 2}};
            foreach (var material in gameData.Material)
            {
                material.Amount = 5;
            }
            // レシピ
            gameData.MyRecipes = new[] {1, 2, 13};
            // フライパン
            gameData.Pans = new[] {Const.DefaultPanId};
        }).AddTo(this);
        
        refillManyProductButton.OnClickAsObservable().Subscribe(_ =>
        {
            gameData.ShopStock.Clear();
            gameData.ShopStock.AddRange(Enumerable.Repeat(new ProductStockData()
            {
                FlavorId = 1, Rarity = ProductRarity.Normal
            }, 5));
            gameData.ShopStock.AddRange(Enumerable.Repeat(new ProductStockData()
            {
                FlavorId = 2, Rarity = ProductRarity.Yellow
            }, 5));
            gameData.ShopStock.AddRange(Enumerable.Repeat(new ProductStockData()
            {
                FlavorId = 3, Rarity = ProductRarity.Copper
            }, 5));
            gameData.ShopStock.AddRange(Enumerable.Repeat(new ProductStockData()
            {
                FlavorId = 4, Rarity = ProductRarity.Silver
            }, 5));
            // gameData.ShopStock.AddRange(Enumerable.Repeat(new ProductStockData()
            // {
            //     FlavorId = 5, Rarity = ProductRarity.Gold
            // }, 5));
            
            gameData.StorageTanks = new List<StorageTank>
            {
                // new StorageTank(){Id = 1, Capacity = 50, FlavorId = 1, Stocks = new List<ProductStockData>{new ProductStockData{Rarity = ProductRarity.Normal, Stock = 0}}},
                new StorageTank(){Id = 2, Capacity = 50, FlavorId = 2, Stocks = new List<ProductStockData>
                {
                    new ProductStockData{Rarity = ProductRarity.Normal, Stock = 1},
                    new ProductStockData{Rarity = ProductRarity.Yellow, Stock = 20},
                }},
                new StorageTank(){Id = 3, Capacity = 50, FlavorId = 1, Stocks = new List<ProductStockData>
                {
                    new ProductStockData{Rarity = ProductRarity.Normal, Stock = 1},
                    new ProductStockData{Rarity = ProductRarity.Yellow, Stock = 9},
                    new ProductStockData{Rarity = ProductRarity.Copper, Stock = 5},
                    new ProductStockData{Rarity = ProductRarity.Silver, Stock = 5},
                    new ProductStockData{Rarity = ProductRarity.Gold, Stock = 10},
                    new ProductStockData{Rarity = ProductRarity.Rainbow, Stock = 20},
                }},
                new StorageTank(){Id = 4, Capacity = 50, FlavorId = 1, Stocks = new List<ProductStockData>{new ProductStockData{Rarity = ProductRarity.Gold, Stock = 4}}},
            };
        }).AddTo(this);
        
        refillLittleProductButton.OnClickAsObservable().Subscribe(_ =>
        {
            gameData.StorageTanks.ForEach(x => x.ClearStock());
            gameData.ShopStock.Clear();
            gameData.ShopStock.AddRange(Enumerable.Repeat(new ProductStockData()
            {
                FlavorId = 1, Rarity = ProductRarity.Normal
            }, 5));
            gameData.ShopStock.AddRange(Enumerable.Repeat(new ProductStockData()
            {
                FlavorId = 2, Rarity = ProductRarity.Yellow
            }, 5));
            gameData.ShopStock.AddRange(Enumerable.Repeat(new ProductStockData()
            {
                FlavorId = 3, Rarity = ProductRarity.Copper
            }, 5));
            gameData.ShopStock.AddRange(Enumerable.Repeat(new ProductStockData()
            {
                FlavorId = 4, Rarity = ProductRarity.Silver
            }, 5));
            // gameData.ShopStock.AddRange(Enumerable.Repeat(new ProductStockData()
            // {
            //     FlavorId = 5, Rarity = ProductRarity.Gold
            // }, 5));
        }).AddTo(this);

        coinCount.text = $"{gameData.Coin}";
        coinCount.OnValueChangedAsObservable().Subscribe(x =>
        {
            gameData.Coin = Int32.Parse(x);
        }).AddTo(this);
        
        heartCount.text = $"{gameData.Heart}";
        heartCount.OnValueChangedAsObservable().Subscribe(x =>
        {
            gameData.Heart = Int32.Parse(x);
        }).AddTo(this);

        // debugfielddata
        // gameData.PlantLines = new List<PlantLineData>
        // {
        //     new PlantLineData(PlantLineType.Top, CornFieldRank.Rank2),
        //     new PlantLineData(PlantLineType.Center, CornFieldRank.Rank1),
        //     new PlantLineData(PlantLineType.Bottom, CornFieldRank.Rank3)
        // };

        fastGrowingToggle.isOn = UsayaStorageManager.LoadOrDefault(UsayaStorageFilename.Settings_Data, "DebugFastGrowing", false);
        fastGrowingToggle.OnValueChangedAsObservable().Subscribe(x =>
        {
            UsayaStorageManager.Save(UsayaStorageFilename.Settings_Data, "DebugFastGrowing", x);
        }).AddTo(this);
        manyWalkerToggle.isOn = UsayaStorageManager.LoadOrDefault(UsayaStorageFilename.Settings_Data, "DebugManyWalker", false);
        manyWalkerToggle.OnValueChangedAsObservable().Subscribe(x =>
        {
            UsayaStorageManager.Save(UsayaStorageFilename.Settings_Data, "DebugManyWalker", x);
        }).AddTo(this);
    }
#endif
}
