using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;

public class Cup : MonoBehaviour
{
    public Water waterLevel;//每次吸气，水面会降低

    public int sipTimes;//允许满吸多少口水,在关卡初始化时候设置

    public BubbleManager bubbleManager;
    public List<Bubble> bubbles;

    public GameManager gm;

    public int maxScore;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void WaterDown(float power)
    {
        if (power > 0)
        {
            float distance = 0;
            distance = (1f / sipTimes) * (power / 50f);
            waterLevel.MoveDown(distance);
        }
    }

    internal void DragBubble(float power)
    {
        
        foreach (var bubble in bubbles)
        {
            Vector2 distance = gm.pipe.suckPoint.transform.position - bubble.gameObject.transform.position;
            if (distance.magnitude <= gm.pipe.suckRadius)
            {
                Debug.Log(bubble.gameObject.name);
                bubble.ForceDrag(distance.normalized * power * gm.forceStrength);//power可以是负数
            }
        }

        WaterDown(power);
    }

    internal void RemoveBubble(GameObject gameObject)
    {
        this.bubbles.Remove(gameObject.GetComponent<Bubble>());

        Destroy(gameObject);
    }

    internal void SpawnBubble(int[] seq)
    {
        bubbleManager.SpawnBubble(seq);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "SuckPoint")
        {
            Pipe pipe = collision.gameObject.GetComponentInParent<Pipe>();
            pipe.InWater();

        }

    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.tag == "SuckPoint")
        {
            Pipe pipe = collision.gameObject.GetComponentInParent<Pipe>();
            pipe.OutWater();
        }
    }
}
