using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using BackEnd;
public class BackendFriend {
private static BackendFriend _instance = null;
public static BackendFriend Instance {
get {
if(_instance == null) {
_instance = new BackendFriend();
}
return _instance;
}
}
private List<Tuple<string, string>> _requestFriendList = new List<Tuple<string, string>>();
public void SendFriendRequest(string nickName) {
var inDateBro = Backend.Social.GetUserInfoByNickName(nickName);
if(inDateBro.IsSuccess() == false) {
Debug.LogError("An error occurred while searching the name of the user. : " + inDateBro);
return;
}
string inDate = inDateBro.GetReturnValuetoJSON()["row"]["inDate"].ToString();
Debug.Log($"inDate value of {nickName} is {inDate}.");
var friendBro = Backend.Friend.RequestFriend(inDate);
if(friendBro.IsSuccess() == false) {
Debug.LogError($"{inDate} An error occurred while sending a friend request. : " + friendBro);
return;
}
Debug.Log("Friend request sent successfully." + friendBro);
}
public void GetReceivedRequestFriend() {
var bro = Backend.Friend.GetReceivedRequestList();
if(bro.IsSuccess() == false) {
Debug.Log("An error occurred while loading the received friend requests list. : " + bro);
return;
}
if(bro.FlattenRows().Count <= 0) {
Debug.LogError("There are no received friend requests.");
return;
}
Debug.Log("Successfully loaded the received friend requests list. : " + bro);
int index = 0;
foreach(LitJson.JsonData friendJson in bro.FlattenRows()) {
string nickName = friendJson["nickname"]?.ToString();
string inDate = friendJson["inDate"].ToString();
_requestFriendList.Add(new Tuple<string, string>(nickName, inDate));
Debug.Log($"{index}. {nickName} - {inDate}");
index++;
}
}
public void ApplyFriend(int index) {
if(_requestFriendList.Count <= 0) {
Debug.LogError("There are no friend requests.");
return;
}
if(index >= _requestFriendList.Count) {
Debug.LogError($"Exceeded the limit of the received friend request list. Select : {index} / List maximum : {_requestFriendList.Count}");
return;
}
var bro = Backend.Friend.AcceptFriend(_requestFriendList[index].Item2);
if(bro.IsSuccess() == false) {
Debug.LogError("An error occurred while accepting the friend request. : " + bro);
return;
}
Debug.Log($"{_requestFriendList[index].Item1} has become a friend. : " + bro);
}
public void GetFriendList() {
var bro = Backend.Friend.GetFriendList();
if(bro.IsSuccess() == false) {
Debug.LogError("An error occurred while loading the friends list. : " + bro);
return;
}
Debug.Log("Successfully loaded the friends list. : " + bro);
if(bro.FlattenRows().Count <= 0) {
Debug.Log("There are no friends.");
return;
}
int index = 0;
string friendListString = "Friends List\n";
foreach(LitJson.JsonData friendJson in bro.FlattenRows()) {
string nickName = friendJson["nickname"]?.ToString();
string inDate = friendJson["inDate"].ToString();
friendListString += $"{index}. {nickName} - {inDate}\n";
index++;
}
Debug.Log(friendListString);
}
}