using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class DCMotorModule : ModuleBase
{
    private static DCMotorControlPanel motorControlPanel;
    public Transform motorShaft;

    public float rotationSpeed = 90f;
    private float targetPosition = 0f;
    private Coroutine followCoroutine;

    public void Start()
    {
        if (motorControlPanel == null)
        {
            motorControlPanel = FindObjectOfType<DCMotorControlPanel>(true);
        }
    }

    public override void OnSelect()
    {
        if (followCoroutine != null)
            StopCoroutine(followCoroutine);

        followCoroutine = StartCoroutine(FollowPositionForFrames());
        motorControlPanel.Initialize(this);
    }

    private IEnumerator FollowPositionForFrames()
    {
        RectTransform panelRect = motorControlPanel.GetComponent<RectTransform>();

        float duration = 0.5f;
        float timer = 0f;

        while (timer < duration)
        {
            Vector3 worldPos = transform.position;
            Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos) + new Vector3(150f, 0f, 0f);
            panelRect.position = screenPos;

            timer += Time.deltaTime;
            yield return null;
        }
    }

    public override void DeSelect()
    {
        motorControlPanel.HidePanel();
    }

    public void Rotate(float degrees, int direction)
    {
        float rotation = degrees * direction;
        targetPosition += rotation;
        this.SendToControlLibrary("DC", targetPosition);
        StartCoroutine(RotateOverTime(rotation));
    }

    private IEnumerator RotateOverTime(float rotation)
    {
        float currentRotationDegrees = 0f;

        while (Mathf.Abs(currentRotationDegrees) < Mathf.Abs(rotation))
        {
            float step = rotationSpeed * Time.deltaTime;
            if (Mathf.Abs(currentRotationDegrees + step) > Mathf.Abs(rotation))
            {
                step = Mathf.Abs(rotation) - Mathf.Abs(currentRotationDegrees);
            }

            motorShaft.Rotate(Vector3.up, step * Mathf.Sign(rotation));
            currentRotationDegrees += step * Mathf.Sign(rotation);

            yield return null;
        }
    }
}
