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

public class gameUserInfos : MonoBehaviour, IEventHandler
{
    private string gameSaveKey = "";
    private GameScenUserInfo gameScenUserInfo = null;
    Dictionary<string, EventChildItemBack> childEventHandlers = new Dictionary<string, EventChildItemBack>();
    void onEvent(string name, EventChildItemBack func)
    {
        utilsTools.onEvent(this, name);
        childEventHandlers.Add(name, func);
    }
    void Start()
    {
        onEvent("showAreaEventOk", onSelectAreaEvent);
        onEvent("showAreaPalyerEnd", onShowAreaPalyerEndEvent);
        utilsTools.addTimer_Sec(gameObject,"first", (string name) =>
        {
            if (utilsTools.ContainsKeyGDataByKey("nowUseGameId") == false)
            {
                loadTest();
            }
            else
            {
                string id = utilsTools.getGDataByKey<String>("nowUseGameId", "");
                startId(id);
            }
            return false;
        }, 1.0);
        
        
    }
    public void loadTest()
    {
        string id = "test_1";
        GameScenUserInfo gameScenUser = utilsTools.getGameScenUserInfo(id);
        if (gameScenUser == null)
        {
            //utilsTools.setGameScenUserInfo(id,"http://192.168.124.160:8795/srcImgs/colorFan/tuline.png","http://192.168.124.160:8795/srcImgs/colorFan/tucolor.png","http://192.168.124.160:8795/srcImgs/colorFan/tu.json");
            utilsTools.setGameScenUserInfo(id, "http://192.168.124.160:8795/srcImgs/temp2/tuline.png", "http://192.168.124.160:8795/srcImgs/temp2/tucolor.jpg", "http://192.168.124.160:8795/srcImgs/temp2/tu.json");
            gameScenUser = utilsTools.getGameScenUserInfo(id);
        }
        if (gameScenUser == null)
        {
            Debug.Log("无法加载测试图片信息");
            return;
        }
        startId(id);
    }
    // Update is called once per frame
    void Update()
    {   
    }
    public void startId(string id)
    {
        GameScenUserInfo gameScenUser = utilsTools.getGameScenUserInfo(id);
        if(gameScenUser == null)
        {
            return;
        }
        utilsTools.sendEventObjectParams("initViewShowData", gameScenUser);
        if(GetComponent<mainGameView>().loadGameImgId(gameScenUser) == false)
        {
            return;
        }
        else
        {
            gameScenUserInfo = gameScenUser;
        }
    }

    void IEventHandler.onEventHandler(string eventName, List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        if (childEventHandlers.ContainsKey(eventName)) childEventHandlers[eventName](uobjects, objects);
    }
    //永久保存
    void onSelectAreaEvent(List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        if(gameScenUserInfo != null)
        {
            gameAreaItem gameArea = (gameAreaItem)objects[0];
            short x = (short)objects[1];
            short y = (short)objects[2];
            AreaInfoUseData areaInfoUseData = new AreaInfoUseData();
            areaInfoUseData.clickX = x;
            areaInfoUseData.clickY = y;
            areaInfoUseData.id = gameArea.id;
            gameScenUserInfo.areaInfoUseData.Add(areaInfoUseData);
            //utilsTools.saveGameScenUserInfo(gameScenUserInfo);
        }
    }
    void onShowAreaPalyerEndEvent(List<UnityEngine.Object> uobjects, List<System.Object> objects)
    {
        if(gameScenUserInfo == null)
        {
            utilsTools.saveGameScenUserInfo(gameScenUserInfo);
            return;
        }
    }
}
