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

public class FriendScrollView : MonoBehaviour
{
    public Transform contentTransform;
    
    public GameObject gameInfoItem;
#if USE_MS_GAMECORE
    private List<DHXboxGameFriendBean> beanList = new List<DHXboxGameFriendBean>();
#endif

    public void handleReresh(string friendDataJson)
    {
        if (!string.IsNullOrEmpty(friendDataJson))
        {
            Debug.Log("重刷好友列表");
            updateFriendState(friendDataJson);
        }
        else
        {
            Debug.Log("haoyou cachedata is null");
        }
    }

    public void updateFriendState(string data)
    {
#if USE_MS_GAMECORE
        beanList.Clear();
        for (int i = 0;i< contentTransform.childCount; i++)
        {
            GameObject obj = contentTransform.transform.GetChild(i).gameObject;
            if(obj != null)
            {
                Debug.Log("历史好友信息，正在移除");
                Destroy(obj);
            }
        }
        DHXboxGameFriendListBean target = JsonUtility.FromJson<DHXboxGameFriendListBean>(data);
        DHXboxGameFriendBean[] list = target.target;
        beanList.AddRange(list);
        Debug.Log("haoyou geshu :" + beanList.Count);
        for(int i = 0;i< beanList.Count; i++)
        {
            DHXboxGameFriendBean bean = list[i];
            Transform temp = Instantiate(gameInfoItem.transform).transform;
            
            temp.SetParent(contentTransform);
            Vector3 v3 = new Vector3(0, i * -50);
            temp.localPosition = v3;
            temp.localRotation = Quaternion.identity;
            temp.localScale = Vector3.one;

            TMPro.TMP_Text nickName = temp.Find("friendName").GetComponent<TMPro.TMP_Text>();
            TMPro.TMP_Text status = temp.Find("friendStatus").GetComponent<TMPro.TMP_Text>();
            nickName.SetText(bean.nickName);
            if (bean.userId == "")
            {
                status.SetText("no game account");
            }
            else
            {
                status.SetText(bean.inGame ? "in game" : "no in game");
            }
            Button joinButton = temp.Find("joinBtn").GetComponent<Button>();
            if(joinButton == null)
            {
                Debug.Log("not found joinBtn");
                break;
            }
            if (bean.inGame)
            {
                joinButton.gameObject.SetActive(true);
                Debug.Log("joinBtn show");
                string userId = bean.userId;
                joinButton.onClick.AddListener(delegate(){
                    Debug.Log("joinBtn set addlistener:"+ userId);
                    JoinFriend(userId);
                });
            }
            else
            {
                joinButton.gameObject.SetActive(false);
                Debug.Log("joinBtn hide");
            }
        }
#endif
    }

    public void JoinFriend(string userID)
    {
        Debug.Log("JoinFriend clicked "+ userID);
        DHSDKHelper.joinFrinedLobby(userID);
    }
}
