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

public class MainGameDebugView : MonoBehaviour
{
    [SerializeField] private Button showDebugButton;
    [SerializeField] private Button closeDebugButton;
    [SerializeField] private Button gameResetButton;
    [SerializeField] private Button backMainButton;
    [SerializeField] private GameObject view;

    [SerializeField] private Button pattern1;
    [SerializeField] private Button pattern2;

    [SerializeField] private MainGameDebugParameter[] panDataParameters;
    [SerializeField] private PopcornGameManager gameManager;
    [SerializeField] private ThermalControl thermalControl;

    private readonly PanData debugPanData = new PanData();

    private void Awake()
    {
#if DEVELOPMENT_BUILD || UNITY_EDITOR
        view.transform.SetLocalScale(0f);
        view.SetActive(true);
        Observable.NextFrame().Subscribe(_ =>
        {
            view.transform.SetLocalScale(1f);
            view.SetActive(false);
        });
#else
        gameObject.SetActive(false);
        return;
#endif
    }
    // Start is called before the first frame update

    void Start()
    {
        Initialize();
    }

    [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")]
    private void Initialize()
    {
        showDebugButton.OnClickAsObservable().Subscribe(_ =>
        {
            Time.timeScale = 0f;
            view.SetActive(true);
        }).AddTo(this);
        closeDebugButton.OnClickAsObservable().Subscribe(_ =>
        {
            Time.timeScale = 1f;
            view.SetActive(false);
        }).AddTo(this);
        gameResetButton.OnClickAsObservable().Subscribe(_ =>
        {
            Time.timeScale = 1f;
            view.SetActive(false);
            TransitionManager.Instance.UnloadScene(GameScenes.CookingResults);
            gameManager.ResetGame(debugPanData);
        }).AddTo(this);
        backMainButton.OnClickAsObservable().Subscribe(_ =>
        {
            Time.timeScale = 1f;
            TransitionManager.Instance.LoadScene(GameScenes.Main);
        }).AddTo(this);
        // pattern1.OnClickAsObservable().Subscribe(_ =>
        // {
        //     SetPreset(preset1);
        // }).AddTo(this);
        // pattern2.OnClickAsObservable().Subscribe(_ =>
        // {
        //     SetPreset(preset2);
        // }).AddTo(this);
        
        // panData取得
        var originalPanData = LocalCacheManager.Load<PanData>(PopcornGameManager.PanDataTag);
        debugPanData.id = originalPanData.id;
        debugPanData.rarity = originalPanData.rarity;
        debugPanData.prefabName = originalPanData.prefabName;
        SetPanDataToParam(originalPanData);
        panDataParameters.Select(param => param.OnValueChange).CombineLatest()
            .ThrottleFrame(1)
            // .Debug("debugParameter")
            .Subscribe(x =>
            {
                SetParamToPanData(x);
                thermalControl.SetPanData(debugPanData);
            }).AddTo(this);
    }

    private void SetParamToPanData(IList<float> parameters)
    {
        debugPanData.panFactor = parameters[0];
        debugPanData.deadZone = parameters[1];
        debugPanData.baseIncreaseSpeed = parameters[2];
        debugPanData.coldIncreaseSpeed = parameters[3];
        debugPanData.hotIncreaseSpeed = parameters[4];
        debugPanData.maxDecreaseSpeed = parameters[5];
        debugPanData.yellowSize = parameters[6];
        debugPanData.randomRange = parameters[7];
    }

    private void SetPanDataToParam(PanData panData)
    {
        panDataParameters[0].SetValue(panData.panFactor);
        panDataParameters[1].SetValue(panData.deadZone);
        panDataParameters[2].SetValue(panData.baseIncreaseSpeed);
        panDataParameters[3].SetValue(panData.coldIncreaseSpeed);
        panDataParameters[4].SetValue(panData.hotIncreaseSpeed);
        panDataParameters[5].SetValue(panData.maxDecreaseSpeed);
        panDataParameters[6].SetValue(panData.yellowSize);
        panDataParameters[7].SetValue(panData.randomRange);
    }

    private float[] preset1 = 
    {
        0.002f,
        0.009f,
        0.18f,
        0.18f,
        0.18f,
        0.5f,
        0.35f,
        0.65f,
        50f,
        10f,
        .1f,
        2f,
        12.5f,
        1.5f,
        1f,
        45f,
        20f,
    };

    private float[] preset2 =
    {
        0.003f,
        0.009f,
        0.5f,
        0.5f,
        0.18f,
        0.5f,
        0.45f,
        0.55f,
        50f,
        20f,
        .1f,
        2f,
        12.5f,
        1.5f,
        1f,
        45f,
        20f,
    };

    void SetPreset(float[] preset)
    {
        var parameters = view.GetComponentsInChildren<MainGameDebugParameter>();
        for (int i = 0; i < parameters.Length; i++)
        {
            parameters[i].SetValue(preset[i]);
        }
        
    }
}
