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

[RequireComponent(typeof(EventTrigger))]
public class TouchPanel : BasePanel
{
    //相机
    public Camera cam;

    //当前水果 和当前水果的位置
    private GameObject NowFruit;
    private Vector3 NowFruitPos;

    //手指开始拖动的时候 水果X轴和手指X轴的初始偏移量
    private float OffsetX;

    //是否正在拉动
    private bool IsDraging = false;

    //屏幕左右边界的世界坐标
    private float LeftScreenBound;
    private float RightScreenBound;

    //将要生成的水果集合
    private Queue<int> InstantFruitList = new Queue<int>();

    public override void OnInit(object data = null)
    {
        cam = UIMgr.Getinstance().MainCamera;
        #region 添加拖动事件
        EventTrigger trigger = GetComponent<EventTrigger>();

        EventTrigger.Entry StartDrag = new EventTrigger.Entry();
        StartDrag.eventID = EventTriggerType.BeginDrag;
        StartDrag.callback.AddListener(OnBeginDrag);

        EventTrigger.Entry Draging = new EventTrigger.Entry();
        Draging.eventID = EventTriggerType.Drag;
        Draging.callback.AddListener(OnDraging);

        EventTrigger.Entry EndDrag = new EventTrigger.Entry();
        EndDrag.eventID = EventTriggerType.EndDrag;
        EndDrag.callback.AddListener(OnEngDrag);

        trigger.triggers.Add(StartDrag);
        trigger.triggers.Add(Draging);
        trigger.triggers.Add(EndDrag);

        #endregion

        //初始化水果
        SetInstantiateFruit();

        LeftScreenBound = cam.ScreenToWorldPoint(Vector2.zero).x;
        RightScreenBound = cam.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x;
    }

    public override void OnRefresh(object data = null)
    {

    }

    public override void OnHide(object data = null)
    {

    }

    private void OnBeginDrag(BaseEventData data) 
    {
        if (NowFruit == null)
            return;
        MusicMgr.Getinstance().PlayMusic("Audio/BeginDrag");

        OffsetX = cam.ScreenToWorldPoint(Input.mousePosition).x - NowFruit.transform.position.x;

        IsDraging = true;
    }

    private void OnDraging(BaseEventData data)
    {
        if (NowFruit == null)
            return;

        MusicMgr.Getinstance().PlayDragingMusic();

        IsDraging = true;

        NowFruitPos.Set(cam.ScreenToWorldPoint(Input.mousePosition).x - OffsetX, NowFruitPos.y, 0);

        LimitFruitPos();

        NowFruit.transform.position = NowFruitPos;
    }

    private void OnEngDrag(BaseEventData data)
    {
        if (NowFruit == null)
            return;
        MusicMgr.Getinstance().StopDragingMusic();
        MusicMgr.Getinstance().PlayMusic("Audio/EndDrag");

        NowFruit.GetComponent<Fruit>().OnDrag();
        NowFruit = null;

        Invoke("SetInstantiateFruit", 1);
    }

    //刷新水果
    private void SetInstantiateFruit() 
    {
        if (InstantFruitList.Count == 0) 
        {
            RequestNewFruitType();
            return;
        }

        int NowFruitType = InstantFruitList.Dequeue();
        NowFruit = GameObject.Instantiate(Resources.Load<GameObject>("Fruits/" + NowFruitType));
        NowFruit.name = NowFruitType.ToString();
        NowFruit.transform.position = new Vector3(0, GameController.GetInstance().RedLinePosY);
        NowFruit.transform.localScale = new Vector3(0.3f, 0.3f, 1);

        NowFruitPos = NowFruit.transform.position;

        NowFruit.GetComponent<Fruit>().OnInit();
    }

    //限制水果位置
    private void LimitFruitPos() 
    {
        Bounds bound = NowFruit.GetComponent<SpriteRenderer>().bounds;

        float FruitRadius = bound.size.x / 2;
        
        //如果左越界
        if(NowFruitPos.x-FruitRadius < LeftScreenBound)
            NowFruitPos.Set(LeftScreenBound + FruitRadius, NowFruitPos.y, 0);

        //如果右越界
        if (NowFruitPos.x + FruitRadius > RightScreenBound)
            NowFruitPos.Set(RightScreenBound - FruitRadius, NowFruitPos.y, 0);
    }

    //请求水果数据
    private void RequestNewFruitType() 
    {
        HttpTool.Instance._Get("app/v1/watermelon/nextFruit", null, new Action<List<int>>((result) =>
        {
            for (int i = 0; i < result.Count; i++)
            {
                InstantFruitList.Enqueue(result[i]);
            }
            SetInstantiateFruit();

        }), new Action<string, string>((code, msg) =>
        {
            Debug.unityLogger.Log("获取下一组生成的球 " + code + " msg " + msg);
        }));
    }


}
