using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using static TMPro.SpriteAssetUtilities.TexturePacker_JsonArray;
using static UnityEngine.UI.Button;

public class scenePageItemHandler : MonoBehaviour
{
    private string __title = "Relax Your Mind";
    private List<ScenePageCellItemData> nowdatas = null;
    public string Title
    {
        set
        {
            __title = value;
            transform.Find("title").GetComponent<TextMeshProUGUI>().text = "<i>" + __title + "</i>";
        }
        get
        {
            return __title;
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        for(int i = 0;i < 3; i++)
        {
            var groupTransform = transform.Find("group" + (i + 1).ToString());
            if (groupTransform == null) break;
            var framebg = groupTransform.Find("clickArea");
            Button button = framebg.GetComponent<Button>();
            if(button == null)
            {
                button = framebg.AddComponent<Button>();
            }
            utilsTools.setUserData(button.gameObject, "idx", i);
            ButtonClickedEvent buttonClickedEvent = new ButtonClickedEvent();
            buttonClickedEvent.AddListener(() => {
                int idx = 0;
                utilsTools.getUserData<int>(button.gameObject,"idx",out idx,-111);
                this.onClickArea(idx,button.gameObject,groupTransform.Find("showMax/showImg").gameObject);
              });
            button.onClick = buttonClickedEvent;
        }
    }
    void onClickArea(int idx,GameObject gameObject,GameObject imgObje)
    {
        if(nowdatas == null)
        {
            return;
        }
        Vector3 pos;
        Quaternion rotation;
        gameObject.transform.GetPositionAndRotation(out pos, out rotation);
        List<System.Object> list = new List<System.Object>
        {
            nowdatas[idx],
            pos
        };
        List<UnityEngine.Object> lists = new List<UnityEngine.Object> { gameObject, imgObje };
        utilsTools.sendEventUObjects("selectSceneClick", lists, list);
    }
    // Update is called once per frame
    void Update()
    {
    }
    bool checkDataIsMateView(int size)
    {
        if(size == 0)
        {
            return transform.Find("endimg") != null;
        }
        else if(size == 3)
        {
            return transform.Find("group3") != null;
        }else if(size == 1)
        {
            return transform.Find("group1") != null && transform.Find("group3") == null;
        }
        return false;
    }
    public void setLock(Transform groupTransform, bool isLock)
    {
        groupTransform.Find("showMax").gameObject.SetActive(!isLock);
        groupTransform.Find("lock").gameObject.SetActive(isLock);
        groupTransform.Find("pace").gameObject.SetActive(!isLock);
        groupTransform.Find("sceneName").gameObject.SetActive(!isLock);
        if (isLock)
        {
            var framebg = groupTransform.Find("framebg");
            framebg.GetComponent<Image>().color = new Color(125,125,125);
        }
    }
    public void setPace(Transform groupTransform, int jindu,int jinduMax)
    {
        var pace = groupTransform.Find("pace");
        pace.gameObject.SetActive(jindu != jinduMax);
        var sceneName = groupTransform.Find("sceneName");
        sceneName.gameObject.SetActive(jindu == jinduMax);
        if (jindu != jinduMax)
        {
            groupTransform.Find("pace").gameObject.SetActive(true);
            var pacevalue = pace.Find("pacevalue");
            pacevalue.GetComponent<Text>().text = jindu.ToString() + " / " + jinduMax.ToString();
        }
    }
    public void setSelect(Transform groupTransform,bool isSelect)
    {
        var isSelectObj = groupTransform.Find("showMax/selectFlg");
        isSelectObj.gameObject.SetActive(isSelect);
        var framebg = groupTransform.Find("framebg");
        if(isSelect == true)
        {
            framebg.GetComponent<Image>().color = new Color(128, 0, 0);
        }
        else
        {
            framebg.GetComponent<Image>().color = new Color(255,255,255);
        }
        
    }
    public void loadSceneImg(Transform groupTransform, string path,string name)
    {
        string first = path.Substring(0, 3);
        Image img = groupTransform.Find("showMax/showImg").GetComponent<Image>();
        if (first.Equals("xxx"))
        {
            return;
        }
        else if (first.Equals("htt"))
        {
            string localpath = "Resources/temps/" + name+".png";
            utilsTools.httpDOWNIMG(this, path, localpath, (string id, string url, bool isOk, System.Object imgdata) =>
            {
                onLoadImageHandler(img,url, localpath, isOk, imgdata);
                return 0;
            });
        }
        else
        {
            
            utilsTools.loadLocalImage(this, img, path);
        }
    }

    private void onLoadImageHandler(Image img, string url, string localpath, bool isOk, object imgdata)
    {
        if (isOk == false)
        {
            utilsTools.httpDOWNIMG(this, url, localpath, (string id, string url, bool isOk, System.Object imgdata) =>
            {
                onLoadImageHandler(img,url, localpath, isOk, imgdata);
                return 0;
            });
            return;
        }
        Texture2D texture2d = imgdata as Texture2D;
        Sprite sprite = Sprite.Create(texture2d, new Rect(0, 0, texture2d.width, texture2d.height), new Vector2(0.5f, 0.5f));
        img.sprite = sprite;
    }

    public bool setData(List<ScenePageCellItemData> datas)
    {
        if(checkDataIsMateView(datas.Count) == false)
        {
            return false;
        }
        nowdatas = datas;
        if (datas.Count == 0) return true;
        for(int i = 0;i < datas.Count; i++)
        {
            ScenePageCellItemData scenePageCellItem = datas[i];
            var groupTransform = transform.Find("group" + (i + 1).ToString());
            if (groupTransform == null) return false;
            bool isLock = scenePageCellItem.isLock;
            setLock(groupTransform, isLock);
            if(isLock == true)
            {
                continue;
            }
            loadSceneImg(groupTransform, scenePageCellItem.imgPath,scenePageCellItem.name);
            int jindu = scenePageCellItem.jindu;
            int jindumax = scenePageCellItem.jindumax;
            setPace(groupTransform, jindu, jindumax);
            if (jindu != jindumax)
            {
                groupTransform.Find("showMax/selectFlg").gameObject.SetActive(false);
                continue;
            }
            var sceneName = groupTransform.Find("sceneName");
            if (datas.Count == 1)
            {
                sceneName.GetComponent<Text>().text = scenePageCellItem.name;
            }
            else
            {
                sceneName.GetComponent<TextMeshProUGUI>().text = scenePageCellItem.name;
            }
            setSelect(groupTransform,scenePageCellItem.isSelect);
        }
        return true;
    }
}