using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using MyGame.Scripts;
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 Button bulkOrderResetButton;
    [SerializeField] private Button shopLevelUpResetButton;
    [SerializeField] private Button dailyMissionResetButton;
    [SerializeField] private Button normalMissionResetButton;
    [SerializeField] private Toggle alwaysVideoFailToLoadToggle;
    [SerializeField] private Toggle fastGrowingToggle;
    [SerializeField] private Toggle skipTutorialToggle;
    [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()
    {
        var gameData = GameDataManager.GameData;
        Market.Instance.IsPause.Value = true;
        
        if (!LocalCacheManager.Exists(DebugPrevSceneTag))
        {
            TransitionManager.Instance.LoadScene(GameScenes.Main);
        }
        SoundManager.Instance.StopBGM();
        var prevScene = LocalCacheManager.Load<GameScenes>(DebugPrevSceneTag);
        resetButton.OnClickAsObservable().Subscribe(_ =>
        {
            GameDataManager.ResetData();
            TutorialManager.Instance.ResetTutorial();
            Market.Instance.SetGameData(GameDataManager.GameData);
            Market.Instance.ReShuffle(GameDataManager.GameData.ShopStock);
            Market.Instance.CheckStock(GameDataManager.GameData.ShopStock);
            if (UsayaStorageManager.LoadOrDefault(UsayaStorageFilename.Settings_Data, "DebugTutorialEnable", false))
            {
                GameDataManager.GameData.ViewedShopLevel = 1;
                GameDataManager.GameData.FinishTutorial();
            }
            TransitionManager.Instance.LoadScene(GameScenes.DebugOption);
        }).AddTo(this);
        
        backButton.OnClickAsObservable().Subscribe(_ =>
        {
            GameDataManager.SaveGameData();
            Market.Instance.IsPause.Value = false;
            TransitionManager.Instance.LoadScene(prevScene);
        });

        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, Amount = 5}};
            // レシピ
            gameData.MyRecipes = new[] {Const.DefaultRecipeId};
            // フライパン
            gameData.Pans = new[] {Const.DefaultPanId};
        }).AddTo(this);
        
        refillManyProductButton.OnClickAsObservable().Subscribe(_ =>
        {
            // gameData.StorageTanks.Clear();
            // CookingResult.AddStock(new ProductData(){volume = 5, id = 1}, ProductRarity.Normal);
            // CookingResult.AddStock(new ProductData(){volume = 5, id = 1}, ProductRarity.Yellow);
            // CookingResult.AddStock(new ProductData(){volume = 5, id = 1}, ProductRarity.Copper);
            // CookingResult.AddStock(new ProductData(){volume = 5, id = 1}, ProductRarity.Silver);
            // CookingResult.AddStock(new ProductData(){volume = 5, id = 1}, ProductRarity.Gold);
            // CookingResult.AddStock(new ProductData(){volume = 5, id = 1}, ProductRarity.Rainbow);
            // CookingResult.AddStock(new ProductData(){volume = 20, id = 2}, ProductRarity.Silver);
            if (gameData.StorageTanks.Count < 2)
            {
                gameData.StorageTanks.Add(new StorageTank(){Capacity = Const.TankCapacity});
            }
            var flavor = gameData.StorageTanks[0].FlavorId;
            if (flavor == -1)
            {
                flavor = 1;
            }
            CookingResult.AddStock(new ProductData(){volume = 5, id = flavor}, ProductRarity.Normal);
            CookingResult.AddStock(new ProductData(){volume = 5, id = flavor}, ProductRarity.Yellow);
            CookingResult.AddStock(new ProductData(){volume = 5, id = flavor}, ProductRarity.Copper);
            CookingResult.AddStock(new ProductData(){volume = 5, id = flavor}, ProductRarity.Silver);
            CookingResult.AddStock(new ProductData(){volume = 10, id = 1}, ProductRarity.Copper);
            CookingResult.AddStock(new ProductData(){volume = 10, id = 1}, ProductRarity.Silver);
        }).AddTo(this);
        
        refillLittleProductButton.OnClickAsObservable().Subscribe(_ =>
        {
            gameData.StorageTanks.ForEach(x => x.ClearStock());
            // CookingResult.AddStock(new ProductData(){volume = 5, id = 1}, ProductRarity.Yellow);
            CookingResult.AddStock(new ProductData(){volume = 3, id = 2}, ProductRarity.Copper);
        }).AddTo(this);

        bulkOrderResetButton.OnClickAsObservable().Subscribe(_ =>
        {
            gameData.AchievedOrderLevel = 0;
            gameData.OrderIdInProgress = 0;
            gameData.CancelOrderId = 0;
            gameData.CompletedProductList.Clear();
        }).AddTo(this);

        shopLevelUpResetButton.OnClickAsObservable().Subscribe(_ =>
        {
            gameData.ViewedShopLevel = 0;
        }).AddTo(this);

        dailyMissionResetButton.OnClickAsObservable().Subscribe(_ =>
        {
            gameData.lastDailyMissionSetTime = DateTime.MinValue.ToBinary();
        }).AddTo(this);
        
        normalMissionResetButton.OnClickAsObservable().Subscribe(_ =>
        {
            gameData.AchievedMission.Clear();
            gameData.ResetNormalTotalCount();
        }).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)
        // };

        alwaysVideoFailToLoadToggle.isOn = UsayaStorageManager.LoadOrDefault(UsayaStorageFilename.Settings_Data, "DebugAlwaysVideoFailToLoad", false);
        alwaysVideoFailToLoadToggle.OnValueChangedAsObservable().Subscribe(x =>
        {
            UsayaStorageManager.Save(UsayaStorageFilename.Settings_Data, "DebugAlwaysVideoFailToLoad", x);
        }).AddTo(this);
        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);
        skipTutorialToggle.isOn = UsayaStorageManager.LoadOrDefault(UsayaStorageFilename.Settings_Data, "DebugTutorialEnable", false);
        skipTutorialToggle.OnValueChangedAsObservable().Subscribe(x =>
        {
            UsayaStorageManager.Save(UsayaStorageFilename.Settings_Data, "DebugTutorialEnable", x);
        }).AddTo(this);
    }
#endif
}
