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

public class CustomHorizintalScrollView : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    private List<SceneValue> mSceneData;
    public RectTransform content;
    public List<SChooseItem> itemList = new List<SChooseItem>();
    private Vector3 lastPosition;
    private float offset;

    public int startIndex = 0;
    public int endIndex = 0;

    private float size;
    private float index0Y;

    private bool isMoveItem; // 标记当前的Item是否需要挪动
    private bool sliderUp;
    private bool isDrag;
    private float speed = 20; // 拖动速度

    public void Init(List<SceneValue> m_SceneData, SceneChoose SceneChoose)
    {
        Debug.Log("UpdateSceneItem411");
        mSceneData = m_SceneData;

        Debug.Log("UpdateSceneItem41");
        size = itemList[0].GetComponent<RectTransform>().rect.size.x;
        index0Y = itemList[0].transform.localPosition.x;

        Debug.Log("UpdateSceneItem42");
        if (m_SceneData.Count == 1)
        {
            itemList[2].gameObject.SetActive(false);
            itemList.Remove(itemList[2]);
            itemList[1].gameObject.SetActive(false);
            itemList.Remove(itemList[1]);
            Vector3 temp = itemList[0].gameObject.GetComponent<RectTransform>().localPosition;
            temp.x = 600;
            itemList[0].gameObject.GetComponent<RectTransform>().localPosition = temp;
        }
        else if (m_SceneData.Count == 2&& itemList.Count==3)
        {
            itemList[2].gameObject.SetActive(false);
            itemList.Remove(itemList[2]);
        }

        Debug.Log("UpdateSceneItem43");
        for (int i = 0; i < itemList.Count; i++) // 显示几个数据(这里为5个)
        {
            itemList[i].GetComponentInChildren<Text>().text = mSceneData[i].name;
            itemList[i].SceneData = mSceneData[i];
        }

        Debug.Log("UpdateSceneItem44");
        startIndex = 0;
        endIndex = itemList.Count - 2;
        Debug.Log("UpdateSceneItem45");
    }

    private void UpdateList(Vector2 position)
    {
        offset = position.x - lastPosition.x;
        offset = Mathf.Clamp(offset, -50, 50); // Item宽/2
        lastPosition = position;
        for (int i = 0; i < itemList.Count; i++)
        {
            itemList[i].transform.localPosition += Vector3.right * offset;
        }

        if (offset < 0) // 往左滑动
        {
            if (!sliderUp && isMoveItem)
            {
                startIndex++;
                if (startIndex > mSceneData.Count - 1)
                    startIndex = 0;
                isMoveItem = false;
            }
            sliderUp = true;

            if (itemList[0].transform.localPosition.x <= index0Y && !isMoveItem)
            {
                isMoveItem = true;
                Debug.Log("添加一个Item到末尾");
                itemList[itemList.Count - 1].transform.localPosition = itemList[itemList.Count - 2].transform.localPosition + Vector3.right * size;
                endIndex++;
                if (endIndex > mSceneData.Count - 1)
                    endIndex = 0;

                itemList[itemList.Count - 1].GetComponentInChildren<Text>().text = mSceneData[endIndex].name;
                itemList[itemList.Count - 1].SceneData = mSceneData[endIndex];
            }
            else
            {
                if (itemList[0].transform.localPosition.x <= index0Y - size)
                {
                    isMoveItem = false;
                    Debug.Log("将第一个Item放到末尾");
                    SChooseItem temp = itemList[0];
                    temp.transform.localPosition = itemList[itemList.Count - 1].transform.localPosition + Vector3.right * size;
                    for (int i = 1; i < itemList.Count; i++)
                    {
                        itemList[i - 1] = itemList[i];
                    }
                    itemList[itemList.Count - 1] = temp;

                    startIndex++;
                    if (startIndex > mSceneData.Count - 1)
                        startIndex = 0;
                }
            }

        }
        else if (offset > 0)// 往右滑动
        {
            if (sliderUp && isMoveItem)
            {
                endIndex--;
                if (endIndex < 0)
                    endIndex = mSceneData.Count - 1;
                isMoveItem = false;
            }
            sliderUp = false;

            if (itemList[0].transform.localPosition.x >= index0Y && !isMoveItem)
            {
                isMoveItem = true;
                Debug.Log("添加一个Item到顶部");
                itemList[itemList.Count - 1].transform.localPosition = itemList[0].transform.localPosition - Vector3.right * size;
                startIndex--;
                if (startIndex < 0)
                    startIndex = mSceneData.Count - 1;

                itemList[itemList.Count - 1].GetComponentInChildren<Text>().text = mSceneData[startIndex].name;
                itemList[itemList.Count - 1].SceneData = mSceneData[startIndex];
            }
            else
            {
                if (itemList[0].transform.localPosition.x >= size - index0Y)
                {
                    Debug.Log("将最后一个Item放到顶部");
                    SChooseItem temp = itemList[itemList.Count - 1];
                    temp.transform.localPosition = itemList[0].transform.localPosition - Vector3.right * size;
                    isMoveItem = false;
                    for (int i = itemList.Count - 1; i >= 1; i--)
                    {
                        itemList[i] = itemList[i - 1];
                    }
                    itemList[0] = temp;

                    endIndex--;
                    if (endIndex < 0)
                        endIndex = mSceneData.Count - 1;
                }
            }

        }
    }

    private Vector3 targetPos;
    private Vector3 currentPos;
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (itemList.Count < 3) return;
        if ((eventData.button == PointerEventData.InputButton.Left))
        {
            isDrag = true;
            lastPosition = eventData.position;
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (itemList.Count < 3) return;
        isDrag = false;
        targetPos = eventData.pointerPressRaycast.worldPosition + Vector3.Project(eventData.pointerPressRaycast.worldPosition - currentPos, Vector3.right) * speed;
        //targetPos = Input.mousePosition + Vector3.Project(Input.mousePosition - currentPos, Vector3.right) * speed;
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (itemList.Count < 3) return;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(this.viewRectTran, eventData.position, eventData.pressEventCamera, out Vector2 vector);
        {
            currentPos = eventData.pointerPressRaycast.worldPosition;
            currentPos = vector;
            UpdateList(currentPos);
        }
    }

    private RectTransform viewRectTran;
    private void Awake()
    {
        viewRectTran = GetComponent<RectTransform>();
    }
}