﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BackEnd;
using System.Reflection;
using System;
using UnityEngine.UI;
public partial class BackendManager : MonoBehaviour
{
    // Start is called before the first frame update

    public void ChangeButtonToPost()
    {
        UIManager.instance.InitButton();
        int i = 0;
        UIManager.instance.SetFunctionButton(i++, "GetPostListV2", new List<string>(), GetPostListV2);
        UIManager.instance.SetFunctionButton(i++, "ReceiveAdminPostItemV2", new List<string>() { "string postIndate" }, ReceiveAdminPostItemV2);
        UIManager.instance.SetFunctionButton(i++, "ReceiveAdminPostAllV2", new List<string>(), ReceiveAdminPostAllV2);
        UIManager.instance.SetFunctionButton(i++, "SendPost", new List<string>() { "string postIndate", "string tableName", "string columnName", "string columnValue" }, SendPost);
        UIManager.instance.SetFunctionButton(i++, "ReceiveUserPostItem", new List<string>() { "string postIndate" }, ReceiveUserPostItem);
        UIManager.instance.SetFunctionButton(i++, "ReceiveUserPostAllV2", new List<string>(), ReceiveUserPostAllV2);
        UIManager.instance.SetFunctionButton(i++, "DeleteUserPostItem", new List<string>() { "string postIndate" }, DeleteUserPostItem);

    }


    void GetPostListV2(InputField[] inputFields) // 10
    {
        string methodName = MethodBase.GetCurrentMethod().Name;

        if (backendType == BackendFunctionTYPE.SYNC)
        {
            result = Backend.Social.Post.GetPostListV2();

            Debug.Log($"({backendType.ToString()}){methodName} : {result}");

            if (result.IsSuccess())
            {
                string adminPosts = string.Empty;
                foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["fromAdmin"])
                {
                    adminPosts += string.Format("우편의 inDate : {0} / 내용 : {1}\n", json["inDate"]["S"].ToString(), json["content"]["S"].ToString());
                }
                Debug.Log("받은 관리자 우편 내용 : \n" + adminPosts);


                string userPosts = string.Empty;

                foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["fromAdmin"])
                {
                    userPosts += string.Format("우편의 inDate : {0} / 내용 : {1}\n", json["inDate"]["S"].ToString(), json["content"]["S"].ToString());
                }
                Debug.Log("받은 관리자 우편 내용 : \n" + userPosts);
            }
        }
        else if (backendType == BackendFunctionTYPE.ASYNC)
        {
            Backend.Social.Post.GetPostListV2(result =>
            {
                Debug.Log($"({backendType.ToString()}){methodName} : {result}");

                if (result.IsSuccess())
                {
                    string adminPosts = string.Empty;
                    foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["fromAdmin"])
                    {
                        adminPosts += string.Format("우편의 inDate : {0} / 내용 : {1}\n", json["inDate"]["S"].ToString(), json["content"]["S"].ToString());
                    }
                    Debug.Log("받은 관리자 우편 내용 : \n" + adminPosts);


                    string userPosts = string.Empty;

                    foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["fromAdmin"])
                    {
                        userPosts += string.Format("우편의 inDate : {0} / 내용 : {1}\n", json["inDate"]["S"].ToString(), json["content"]["S"].ToString());
                    }
                    Debug.Log("받은 관리자 우편 내용 : \n" + userPosts);
                }
            });
        }
        else
        {
            SendQueue.Enqueue(Backend.Social.Post.GetPostListV2, result =>
              {
                  Debug.Log($"({backendType.ToString()}){methodName} : {result}");

                  if (result.IsSuccess())
                  {
                      string adminPosts = string.Empty;
                      foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["fromAdmin"])
                      {
                          adminPosts += string.Format("우편의 inDate : {0} / 내용 : {1}\n", json["inDate"]["S"].ToString(), json["content"]["S"].ToString());
                      }
                      Debug.Log("받은 관리자 우편 내용 : \n" + adminPosts);


                      string userPosts = string.Empty;

                      foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["fromAdmin"])
                      {
                          userPosts += string.Format("우편의 inDate : {0} / 내용 : {1}\n", json["inDate"]["S"].ToString(), json["content"]["S"].ToString());
                      }
                      Debug.Log("받은 관리자 우편 내용 : \n" + userPosts);
                  }
              });
        }
    }

    void ReceiveAdminPostItemV2(InputField[] inputFields) // 10
    {
        string methodName = MethodBase.GetCurrentMethod().Name;

        string postIndate = inputFields[0].text;

        if (backendType == BackendFunctionTYPE.SYNC)
        {
            result = Backend.Social.Post.ReceiveAdminPostItemV2(postIndate);

            Debug.Log($"({backendType.ToString()}){methodName} : {result}");

            if (result.IsSuccess())
            {
                Debug.Log(result.GetReturnValuetoJSON()["item"]["M"].ToString());
                //Debug.Log(result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString()); // <- 이와 같은 방식으로 아이템에 접근
            }
        }
        else if (backendType == BackendFunctionTYPE.ASYNC)
        {
            Backend.Social.Post.ReceiveAdminPostItemV2(postIndate, result =>
            {
                Debug.Log($"({backendType.ToString()}){methodName} : {result}");

                if (result.IsSuccess())
                {
                    Debug.Log(result.GetReturnValuetoJSON()["item"]["M"].ToString());
                    //Debug.Log(result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString()); // <- 이와 같은 방식으로 아이템에 접근
                }
            });
        }
        else
        {
            SendQueue.Enqueue(Backend.Social.Post.ReceiveAdminPostItemV2, postIndate, result =>
               {
                   Debug.Log($"({backendType.ToString()}){methodName} : {result}");

                   if (result.IsSuccess())
                   {
                       Debug.Log(result.GetReturnValuetoJSON()["item"]["M"].ToString());
                       //Debug.Log(result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString()); // <- 이와 같은 방식으로 아이템에 접근
                   }
               });
        }
    }


    void ReceiveAdminPostAllV2(InputField[] inputFields) // 10
    {
        string methodName = MethodBase.GetCurrentMethod().Name;

        if (backendType == BackendFunctionTYPE.SYNC)
        {
            result = Backend.Social.Post.ReceiveAdminPostAllV2();

            Debug.Log($"({backendType.ToString()}){methodName} : {result}");

            if (result.IsSuccess())
            {
                string items = string.Empty;
                foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["items"])
                {
                    items += result.GetReturnValuetoJSON()["item"]["M"].ToString() + "\n";
                    items += result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString() + "\n";

                }
                Debug.Log("수령한 우편 : " + items);
            }
        }
        else if (backendType == BackendFunctionTYPE.ASYNC)
        {
            Backend.Social.Post.ReceiveAdminPostAllV2(result =>
            {
                Debug.Log($"({backendType.ToString()}){methodName} : {result}");

                if (result.IsSuccess())
                {
                    string items = string.Empty;
                    foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["items"])
                    {
                        items += result.GetReturnValuetoJSON()["item"]["M"].ToString() + "\n";
                        items += result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString() + "\n";

                    }
                    Debug.Log("수령한 우편 : " + items);
                }
            });
        }
        else
        {
            SendQueue.Enqueue(Backend.Social.Post.ReceiveAdminPostAllV2, result =>
              {
                  Debug.Log($"({backendType.ToString()}){methodName} : {result}");

                  if (result.IsSuccess())
                  {
                      string items = string.Empty;
                      foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["items"])
                      {
                          items += result.GetReturnValuetoJSON()["item"]["M"].ToString() + "\n";
                          items += result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString() + "\n";

                      }
                      Debug.Log("수령한 우편 : " + items);
                  }
              });
        }
    }


    void SendPost(InputField[] inputFields) // 9
    {
        string methodName = MethodBase.GetCurrentMethod().Name;

        string inDate = inputFields[0].text;
        string tableName = inputFields[1].text;
        string columnName = inputFields[2].text;
        int columnValue = Int32.Parse(inputFields[3].text);


        Param param = new Param();
        param.Add(columnName, columnValue);
        var bro = Backend.GameData.Insert(tableName, param);

        if (bro.IsSuccess() == false)
        {
            Debug.LogError("데이터 삽입에 실패했습니다.");
            return;
        }
        PostItem postItem = new PostItem
        {
            Title = "유저 우편 발송",
            Content = "내용을 입력",
            TableName = tableName,
            RowInDate = bro.GetInDate(),
            Column = columnName
        };


        if (backendType == BackendFunctionTYPE.SYNC)
        {
            result = Backend.Social.Post.SendPost(inDate, postItem);

            Debug.Log($"({backendType.ToString()}){methodName} : {result}");

        }
        else if (backendType == BackendFunctionTYPE.ASYNC)
        {
            Backend.Social.Post.SendPost(inDate, postItem, result =>
             {
                 Debug.Log($"({backendType.ToString()}){methodName} : {result}");

             });
        }
        else
        {
            SendQueue.Enqueue(Backend.Social.Post.SendPost, inDate, postItem, result =>
              {
                  Debug.Log($"({backendType.ToString()}){methodName} : {result}");
              });
        }
    }

    void ReceiveUserPostItem(InputField[] inputFields) // 10
    {
        string methodName = MethodBase.GetCurrentMethod().Name;

        string postIndate = inputFields[0].text;

        if (backendType == BackendFunctionTYPE.SYNC)
        {
            result = Backend.Social.Post.ReceiveUserPostItem(postIndate);

            Debug.Log($"({backendType.ToString()}){methodName} : {result}");

            if (result.IsSuccess())
            {
                Debug.Log(result.GetReturnValuetoJSON()["item"]["M"].ToString());
                //Debug.Log(result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString()); // <- 이와 같은 방식으로 아이템에 접근
            }
        }
        else if (backendType == BackendFunctionTYPE.ASYNC)
        {
            Backend.Social.Post.ReceiveUserPostItem(postIndate, result =>
            {
                Debug.Log($"({backendType.ToString()}){methodName} : {result}");

                if (result.IsSuccess())
                {
                    Debug.Log(result.GetReturnValuetoJSON()["item"]["M"].ToString());
                    //Debug.Log(result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString()); // <- 이와 같은 방식으로 아이템에 접근
                }
            });
        }
        else
        {
            SendQueue.Enqueue(Backend.Social.Post.ReceiveUserPostItem, postIndate, result =>
               {
                   Debug.Log($"({backendType.ToString()}){methodName} : {result}");

                   if (result.IsSuccess())
                   {
                       Debug.Log(result.GetReturnValuetoJSON()["item"]["M"].ToString());
                       //Debug.Log(result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString()); // <- 이와 같은 방식으로 아이템에 접근
                   }
               });
        }
    }


    void ReceiveUserPostAllV2(InputField[] inputFields) // 10
    {
        string methodName = MethodBase.GetCurrentMethod().Name;

        if (backendType == BackendFunctionTYPE.SYNC)
        {
            result = Backend.Social.Post.ReceiveUserPostAllV2();

            Debug.Log($"({backendType.ToString()}){methodName} : {result}");

            if (result.IsSuccess())
            {
                string items = string.Empty;
                foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["items"])
                {
                    items += result.GetReturnValuetoJSON()["item"]["M"].ToString() + "\n";
                    items += result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString() + "\n";

                }
                Debug.Log("수령한 우편 : " + items);
            }
        }
        else if (backendType == BackendFunctionTYPE.ASYNC)
        {
            Backend.Social.Post.ReceiveUserPostAllV2(result =>
            {
                Debug.Log($"({backendType.ToString()}){methodName} : {result}");

                if (result.IsSuccess())
                {
                    string items = string.Empty;
                    foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["items"])
                    {
                        items += result.GetReturnValuetoJSON()["item"]["M"].ToString() + "\n";
                        items += result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString() + "\n";

                    }
                    Debug.Log("수령한 우편 : " + items);
                }
            });
        }
        else
        {
            SendQueue.Enqueue(Backend.Social.Post.ReceiveUserPostAllV2, result =>
              {
                  Debug.Log($"({backendType.ToString()}){methodName} : {result}");

                  if (result.IsSuccess())
                  {
                      string items = string.Empty;
                      foreach (LitJson.JsonData json in result.GetReturnValuetoJSON()["items"])
                      {
                          items += result.GetReturnValuetoJSON()["item"]["M"].ToString() + "\n";
                          items += result.GetReturnValuetoJSON()["item"]["M"]["itemId"]["S"].ToString() + "\n";

                      }
                      Debug.Log("수령한 우편 : " + items);
                  }
              });
        }
    }

    void DeleteUserPostItem(InputField[] inputFields) // 9
    {
        string methodName = MethodBase.GetCurrentMethod().Name;

        string inDate = inputFields[0].text;
        if (backendType == BackendFunctionTYPE.SYNC)
        {
            result = Backend.Social.Post.DeleteUserPostItem(inDate);

            Debug.Log($"({backendType.ToString()}){methodName} : {result}");

        }
        else if (backendType == BackendFunctionTYPE.ASYNC)
        {
            Backend.Social.Post.DeleteUserPostItem(inDate, result =>
             {
                 Debug.Log($"({backendType.ToString()}){methodName} : {result}");

             });
        }
        else
        {
            SendQueue.Enqueue(Backend.Social.Post.DeleteUserPostItem, inDate, result =>
              {
                  Debug.Log($"({backendType.ToString()}){methodName} : {result}");
              });
        }
    }


}