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

public class scenePagesHandler : MonoBehaviour, IEventHandler
{
    // Start is called before the first frame update
    private List<ScenePageCellGroupItemData> datas;
    void Start()
    {
        utilsTools.onEvent(this, "showSceneView");
        utilsTools.onEvent(this, "selectSceneClick");
        utilsTools.HideView(gameObject);

        datas = utilsDB.getScenePageCellGroupDataByDB();
        updateMusicState(getDBBoolDataByKey("music"));
        
        var scrollView = transform.Find("mainScenes").GetComponent<gridView>();
        utilsTools.addTimer_Sec(gameObject, "wait1", (string na) =>
        {
            scrollView.setData(datas.Count,
                (GameObject gameObject, int idx, ListViewBackTypeEnum backType) => {
                this.onListViewUpdateHandler(gameObject, idx, backType);
            }, true, (int i, ref string name, ref float height) => {
                this.onListViewItemInfoHandler(i, ref name, ref height);
            });
            return false;
        }, 5);
    }
    private void onListViewUpdateHandler(GameObject gameObject, int idx, ListViewBackTypeEnum backType)
    {
        if (backType == ListViewBackTypeEnum.create || backType == ListViewBackTypeEnum.update)
        {
            var spih = gameObject.GetComponent<scenePageItemHandler>();
            spih.Title = datas[idx].name;
            spih.setData(datas[idx].datas);
        }
    }
    private void onListViewItemInfoHandler(int i, ref string name, ref float height)
    {
        if (datas[i].datas.Count == 1)
        {
            name = "scenePageItem1";
            height = 270;
        }
        else if (datas[i].datas.Count == 3)
        {
            name = "scenePageItem3";
            height = 270;
        }
        else
        {
            name = "scenePageItem0";
            height = 270;
        }
    }
    public void onMusicClickHandler()
    {
        bool music = !getDBBoolDataByKey("music");
        updateMusicState(music);
        setDBBoolDataByKey("music", music);
    }
    private bool getDBBoolDataByKey(string key)
    {
        return utilsDB.getDBBoolDataByKey(key, false);
    }
    private void setDBBoolDataByKey(string key, bool value)
    {
        utilsDB.setDBBoolDataByKey(key, value);
    }
    public void onCloseClickHandler()
    {
        utilsTools.HideView(gameObject);
    }

    public void onEventHandler(string eventName, List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        if (eventName.Equals("showSceneView"))
        {
            updateMusicState(getDBBoolDataByKey("music"));
            utilsTools.ShowView(gameObject);
        }
        else if (eventName.Equals("selectSceneClick")){
            ScenePageCellItemData data = objects[0] as ScenePageCellItemData;
            Vector3 pos = (Vector3)objects[1];
            if(data.isLock == true || data.jindu != data.jindumax)
            {
                utilsTools.showTotal("Color to unlock the scene").show();
                return;
            }
            else
            {
                var ssHandler = transform.Find("switchScene").GetComponent<switchSceneHandler>();
                GameObject gobject = uobjects[1] as GameObject;
                ssHandler.onShowView(pos, new Vector3(100, 120), data, gobject.GetComponent<Image>());
            }
        }
    }
    public void updateMusicState(bool isOpen)
    {
        var off = transform.Find("music/off").gameObject;
        var on = transform.Find("music/on").gameObject;
        off.SetActive(!isOpen);
        on.SetActive(isOpen);
    }
}
