using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace BuildSystem
{

    public class SetPanel : MonoBehaviour
    {
        GridUI grid;
        PlaceObject_SO placeObject;
        public Image img;
        public Transform road;
        public Transform[] dir;

        public GameObject cansetPre;
        public GameObject notsetPre;
        public GameObject roadPre;

        public Button close;
        public Button sure;

        private void Awake()
        {
            close.onClick.AddListener(() => {
                gameObject.SetActive(false);
            });

            sure.onClick.AddListener(SetData);
        }
        public void Init(GridUI grid, PlaceObject_SO placeObject_SO, Sprite sprite)
        {
            this.grid = grid;
            placeObject = placeObject_SO;
            this.img.sprite = sprite;
            CreatePanel();
        }

        void CreatePanel()
        {
            List<Toggle> jilu = new List<Toggle>();
            //
            int roadID = 1;
            for (int i = 0; i < placeObject.roadGoInfo.Count; i++)
            {
                object[] curRoadGoInfo = placeObject.roadGoInfo[i];
                //判断该车道是否存在
                bool flag = false;
                foreach (object o in curRoadGoInfo)
                {
                    if (o != null)
                    {
                        flag = true;
                        break;
                    }
                }

                //对存在车道的道路进行ui创建
                if (flag)
                {
                    GameObject text = Instantiate(roadPre, road);
                    text.GetComponent<Text>().text = roadID.ToString();
                    int nonullnumber = 0;
                    jilu.Clear();
                    for (int l = 0; l < curRoadGoInfo.Length; l++)
                    {
                        int transID = 0;
                        if (i < 3) { //-2 <0 +4
                            transID = l - 2;
                            if (transID < 0)
                            {
                                transID += 4;
                            }
                        }
                        else if (i < 6) //+3 >0 -4
                        {
                            transID = 3 + l;
                            if (transID > 3)
                            {
                                transID -= 4;
                            }
                        }
                        else if (i < 9)
                        {
                            transID = l;
                        }
                        else
                        {
                            transID = l + 1;
                            if (transID > 3)
                            {
                                transID -= 4;
                            }
                        }


                        if (curRoadGoInfo[transID] == null)
                        {
                            Instantiate(notsetPre, dir[l]);
                        }
                        else
                        {
                            nonullnumber++;
                            GameObject toggle = Instantiate(cansetPre, dir[l]);
                            toggle.GetComponent<Toggle>().isOn = (bool)curRoadGoInfo[transID];
                            jilu.Add(toggle.GetComponent<Toggle>());
                        }
                    }
                    if (nonullnumber == 1) {
                        jilu[0].interactable = false;
                    }
                    jilu.Clear();
                    roadID++;
                }
            }
        }


        void SetData()
        {
            int roadID = 0;
            for(int i=0;i< placeObject.roadGoInfo.Count;i++)
            {
                object[] curRoadGoInfo = placeObject.roadGoInfo[i];
                //判断车道是否存在
                bool flag = false;
                foreach (object o in curRoadGoInfo)
                {
                    if (o != null)
                    {
                        flag = true;
                        break;
                    }
                }


                if (flag)
                {
                    for (int d = 0; d < 4; d++)
                    {
                        int transID = 0;
                        if (i < 3)
                        { //-2 <0 +4
                            transID = d + 2;
                            if (transID > 03)
                            {
                                transID -= 4;
                            }
                        }
                        else if (i < 6) //+3 >0 -4
                        {
                            transID =  d + 1;
                            if (transID > 3)
                            {
                                transID -= 4;
                            }
                        }
                        else if (i < 9)
                        {
                            transID = d;
                        }
                        else
                        {
                            transID = d - 1;
                            if (transID < 0)
                            {
                                transID += 4;
                            }
                        }

                        if (dir[transID].GetChild(roadID).GetComponent<Toggle>())
                        {
                            curRoadGoInfo[d] = dir[transID].GetChild(roadID).GetComponent<Toggle>().isOn;
                        }
                        else
                        {
                            curRoadGoInfo[d] = null;
                        }
                    }
                    roadID++;
                }
            }

            grid.SetData(placeObject.roadGoInfo);
        }

       

        private void OnDisable()
        {
            //清空之前设置的显示
            for (int t = 0; t < road.childCount; t++)
            {
                Destroy(road.GetChild(t).gameObject);
            }

            foreach (Transform trans in dir)
            {
                for (int t = 0; t < trans.childCount; t++)
                {
                    Destroy(trans.GetChild(t).gameObject);
                }
            }
            BuildSystemManager.Instance.isCanPlace = true;
        }

        private void OnEnable()
        {
            BuildSystemManager.Instance.isCanPlace = false;
        }


    }
}