using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class HomeScriptProxy : MonoBehaviour
{
    public Sprite[] selectImgs;
    public Sprite[] normalImgs;
    public Image[] imgObjs;
    public GameObject switchBtnBase = null;
    public GameObject switchPageBase = null;
    public string nowSelectName = "library";
    private List<string> names = new List<string>();
    void Start()
    {
        if(switchBtnBase != null && switchPageBase != null) {
            for (int i = 0; i < switchBtnBase.transform.childCount; i++)
            {
                var obj = switchBtnBase.transform.GetChild(i);
                names.Add(obj.name);
                if (obj.name.Equals(nowSelectName))
                {
                    imgObjs[i].sprite = selectImgs[i];
                    switchPageBase.transform.Find(obj.name).gameObject.SetActive(true);
                }
                else
                {
                    imgObjs[i].sprite = normalImgs[i];
                    switchPageBase.transform.Find(obj.name).gameObject.SetActive(false);
                }
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
    }
    public void onSelectHandler(GameObject byClick)
    {
        if(byClick.name.Equals(nowSelectName))
        {
            return;
        }
        var oldSelectName = nowSelectName;
        nowSelectName = byClick.name;
        for (int i = 0; i < switchBtnBase.transform.childCount; i++)
        {
            var obj = switchBtnBase.transform.GetChild(i);
            if (obj.name.Equals(nowSelectName))
            {
                imgObjs[i].sprite = selectImgs[i];
                switchPageBase.transform.Find(obj.name).gameObject.SetActive(true);
            }
            else
            {
                imgObjs[i].sprite = normalImgs[i];                
                switchPageBase.transform.Find(obj.name).gameObject.SetActive(false);
            }
        }
        switchPageAnimation(oldSelectName, nowSelectName);
    }
    //old now
    private void switchPageAnimation(string name1,string name2)
    {
        var gameObject1 = switchPageBase.transform.Find(name1).gameObject;
        var gameObject2 = switchPageBase.transform.Find(name2).gameObject;
        var idx1 = names.IndexOf(name1);
        var idx2 = names.IndexOf(name2);
        Vector3 vector3;
        Quaternion quaternion;
        float width = gameObject1.GetComponent<RectTransform>().rect.width;
        if(idx1 < idx2)
        {
            //向左边
            gameObject1.transform.GetPositionAndRotation(out vector3, out quaternion);
            vector3.x = 0;
            gameObject1.transform.SetPositionAndRotation(vector3, quaternion);
            gameObject2.transform.GetPositionAndRotation(out vector3, out quaternion);
            vector3.x = width;
            gameObject2.transform.SetPositionAndRotation(vector3, quaternion);
            ActionManages.addMoveByRelativePos(gameObject1, new Vector2(0 - width, vector3.y),0.5f);
            ActionManages.addMoveByRelativePos(gameObject2, new Vector2(0, vector3.y), 0.5f,(GameObject gameObj,int aid) => {
                Debug.Log("切换<完成");
            });
        }
        else
        {
            //向右边
            gameObject1.transform.GetPositionAndRotation(out vector3, out quaternion);
            vector3.x = 0;
            gameObject1.transform.SetPositionAndRotation(vector3, quaternion);
            gameObject2.transform.GetPositionAndRotation(out vector3, out quaternion);
            vector3.x = 0- width;
            gameObject2.transform.SetPositionAndRotation(vector3, quaternion);
            ActionManages.addMoveByRelativePos(gameObject1, new Vector2(width, vector3.y), 0.5f);
            ActionManages.addMoveByRelativePos(gameObject2, new Vector2(0, vector3.y), 0.5f, (GameObject gameObj, int aid) => {
                Debug.Log("切换>完成");
            });
        }
    }
    
}
