using Darkmatter.Core;
using Darkmatter.Domain;
using UnityEngine;
using VContainer;

public class PlatformManager : MonoBehaviour,IPlatformManager
{
    [Inject] private IPool<Platform> pool;
    [Inject] private IInputReader inputReader;

    private float yPos = 0f;

    [SerializeField] private float inputScale = 360f;
    float rotAmount = 0;
    public bool hasFirstPlatformBuilt = false; 


    private void Start()
    {
        ShowPlatforms();
    }

    private void OnEnable()
    {
        inputReader.OnDragValueChanged += HandleDrag;
    }

    private void OnDisable()
    {
        inputReader.OnDragValueChanged -= HandleDrag;
    }

    private void LateUpdate()
    {
#if UNITY_EDITOR || UNITY_STANDALONE
        if (!inputReader.isMouseButtonPressed) return;
#endif

        // Apply rotation
        transform.Rotate(Vector3.up * rotAmount, Space.World);
    }

    private void HandleDrag(Vector2 drag)
    {
        rotAmount = -drag.x/Screen.width * inputScale;
    }

    private void ShowPlatforms()
    {
        foreach (Platform platform in pool.All)
        {
            if (!hasFirstPlatformBuilt)
            {
                platform.SetPlatformRule(new FirstPlatformRule());
                hasFirstPlatformBuilt=true;
            }
            else
            {
                platform.SetPlatformRule(new OtherPlatformRule());
            }
            platform.gameObject.SetActive(true);
            platform.transform.position = new Vector3(0, yPos, 0);
            yPos--;
        }
    }

    public void BuildNewPlatform()
    {
        Platform platform = pool.GetFromPool();
        platform.SetPlatformRule(new OtherPlatformRule());
        platform.gameObject.SetActive(true);
        platform.transform.position = new Vector3(0, yPos, 0);
        yPos--;
    }
}


