using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine;
using static UnityEngine.Rendering.VirtualTexturing.Debugging;

public class Pipe : MonoBehaviour
{
    private Rigidbody2D rb;
    private Vector2 offset;
    private bool isDragging = false;
    public Transform pivot;
    public GameObject handle;
    public GameObject suckPoint;
    public GameObject mouth;
    public Collider2D getTrigger;

    public float maxDragDistance = 1f; // 最大拖拽距离

    public GameManager gm;

    public float suckRadius;

    public bool isInWater;
    public bool firstTime = true;

    void Start()
    {
        // 获取刚体组件
        rb = GetComponent<Rigidbody2D>();
    }

    void OnMouseDown()
    {

        // 记录鼠标点击时的偏移量
        offset = (Vector2)transform.position - (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
        isDragging = true;
        Cursor.visible = false;
        handle.transform.position = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
        handle.SetActive(true);
    }

    void OnMouseDrag()
    {
        if (isDragging)
        {
            // 获取鼠标在世界空间中的位置
            Vector2 mousePosition = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset;

            // 计算物体与鼠标之间的距离
            float distance = Vector2.Distance(transform.position, mousePosition);

            // 如果距离超过最大拖拽距离，限制鼠标位置在最大拖拽距离范围内
            if (distance > maxDragDistance)
            {
                // 计算物体与鼠标的方向向量，并将鼠标位置限制在最大距离内
                Vector2 direction2 = (mousePosition - (Vector2)transform.position).normalized;
                mousePosition = (Vector2)transform.position + direction2 * maxDragDistance;
            }

            // 移动物体到限制后的鼠标位置
            rb.MovePosition(mousePosition);




            Vector3 direction = pivot.position - transform.position;

            // 计算旋转角度
            float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

            // 让物体旋转到目标方向
            Quaternion targetRotation = Quaternion.Euler(new Vector3(0, 0, angle));
            rb.MoveRotation(targetRotation);
            //transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 7 * Time.deltaTime);
        }




    }

    void OnMouseUp()
    {
        // 当鼠标松开时，停止拖动
        isDragging = false;
        handle.SetActive(false);
        Cursor.visible = true;


    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        rb.velocity = Vector2.zero;  // 停止物体的移动
        rb.angularVelocity = 0;
        // 当物体碰到墙壁时停止所有物理动作
    }


    internal void InWater()
    {
        if (firstTime)
        {
            gm.PipeFirstInWater();
            firstTime = false;
        }
        this.isInWater = true;
        SpriteRenderer rd = GetComponent<SpriteRenderer>();
        rd.sortingOrder = 6;
    }

    internal void OutWater()
    {
        this.isInWater = false;
        SpriteRenderer rd = GetComponent<SpriteRenderer>();
        rd.sortingOrder = 11;
    }

    internal void ShowMouth(bool value)
    {
        this.mouth.gameObject.SetActive(value);
    }
}
