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

public class gameSelectAreaHandler : MonoBehaviour, IEventHandler
{
    public GameObject content = null;
    private GameObject nowGameObje = null;
    public int nowSelectId = 0;
    // Start is called before the first frame update
    public gameSelectAreaHandler():base()
    {
    }
    
    void Start()
    {
        utilsTools.onEvent(this, "show_SettingSelectArea");
        utilsTools.HideView(gameObject);
    }

    // Update is called once per frame
    void Update()
    {
    }
    void UpdateUI()
    {
        nowSelectId = getDBIntDataByKey("seting_areaSelectIdx", 0);
        utilsTools.ShowView(gameObject);
        int len = content.transform.childCount;
        for (int i = 0; i < len; i++)
        {
            Button button = content.transform.GetChild(i).GetComponent<Button>();
            button.interactable = i != nowSelectId;
            if (i == nowSelectId)
            {
                nowGameObje = button.gameObject;
            }
        }
    }
    public void onClick()
    {
        Debug.Log("µã»÷°´Å¥Î´Öª");
        GameObject obj = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject;
        if (nowGameObje.Equals(obj))
        {
            return;
        }
        int len = content.transform.childCount;
        nowGameObje = obj;
        for (int i = 0; i < len; i++)
        {
            Button button = content.transform.GetChild(i).GetComponent<Button>();
            button.interactable = true;
            if (nowGameObje.Equals(button.gameObject))
            {
                nowSelectId = i;
            }
        }
        content.transform.GetChild(nowSelectId).GetComponent<Button>().interactable = false;
    }
    public void onAckSelectArea()
    {
        //int len = content.transform.childCount;
        //for (int i = 0; i < len; i++)
        //{
        //    Button button = content.transform.GetChild(i).GetComponent<Button>();
        //    if (button.gameObject.Equals(nowGameObje))
        //    {
        //        this.nowSelectId = i;
        //        break;
        //    }
        //}
        Debug.Log("user areaid " + nowSelectId.ToString());
        setDBIntDataByKey("seting_areaSelectIdx", this.nowSelectId);
        utilsTools.HideView(gameObject);
        utilsTools.sendEvent("changeSelectGameTypeIdx");
    }

    private int getDBIntDataByKey(string key, int defvalue)
    {
        return utilsDB.getDBIntDataByKey(key, defvalue);
    }
    private void setDBIntDataByKey(string key, int value)
    {
        utilsDB.setDBIntDataByKey(key, value);
    }
    public void onCancelHandler()
    {
        utilsTools.HideView(gameObject);
    }
    public void onEventHandler(string eventName, List<Object> uobjects, List<object> objects)
    {
        if (eventName.Equals("show_SettingSelectArea"))
        {
            UpdateUI();
        }
    }
}
