﻿// Copyright 2013-2022 AFI,Inc. All rights reserved

using System;
using System.Collections.Generic;
using BackEnd;
using BackEnd.GlobalSupport;
using UnityEngine;

public class CDNGroup : BaseGroup {
    
    public override string GetGroupName() {
        return "CDN 관리";
    }
    
    public override void SetSubFunctionButton() {
        
        // 커스텀 계정
        // UIManager.Instance.AddSubFunction("그룹 리스트 불러오기", GetGroupList);
        //
        UIManager.Instance.AddSubFunction("컨텐트 전부 불러오기", GetContent);
        UIManager.Instance.AddSubFunction("컨텐트 폴더 불러오기", GetContentFolder);
        UIManager.Instance.AddSubFunction("확률 전부 불러오기", GetProbability);
        UIManager.Instance.AddSubFunction("로컬에 저장", LocalSave);
        UIManager.Instance.AddSubFunction("로컬에서 불러오기", LocalLoad);
        UIManager.Instance.AddSubFunction("로컬에서 삭제하기", LocalDelete);
        UIManager.Instance.AddSubFunction("로컬 업데이트", LocalUpdate);
        UIManager.Instance.AddSubFunction("로컬 초기화", LocalReset);
    }
    
    private void GetContent() {
        requestUI.SetExecuteButtonAction( () => {
            var bro = Backend.CDN.Content.Table.Get();

            if (bro.IsSuccess() == false)
            {
                ShowResultData(bro);
            }
            
            var bro2 = Backend.CDN.Content.Get(bro.GetContentTableItemList());

            foreach (var item in bro2.GetContentList())
            {
                Debug.Log(item.ToString());
            }
            
            ShowResultData(bro);

        });
    }
    
    private void GetContentFolder() {
        requestUI.AddInputField("폴더 ID");

        requestUI.SetExecuteButtonAction( () => {
            var folderId = InputParseByString(0, 0);

            var bro = Backend.CDN.Content.Table.Get(folderId);

            if (bro.IsSuccess() == false)
            {
                ShowResultData(bro);
            }
            
            var bro2 = Backend.CDN.Content.Get(bro.GetContentTableItemList());

            foreach (var item in bro2.GetContentList())
            {
                Debug.Log(item.ToString());
            }
            
            ShowResultData(bro);

        });
    }
    
    
    
    private void GetProbability() {
        requestUI.SetExecuteButtonAction( () => {
            var bro = Backend.CDN.Probability.Table.Get();

            if (bro.IsSuccess() == false)
            {
                ShowResultData(bro);
            }
            
            var bro2 = Backend.CDN.Probability.Get(bro.GetProbabilityTableItemList());

            foreach (var item in bro2.GetProbabilityContentList())
            {
                Debug.Log(item.ToString());
            }
            
            ShowResultData(bro);

        });
    }

    private void LocalSave()
    {
        requestUI.SetExecuteButtonAction( () => {
            var bro = Backend.CDN.Content.Table.Get();

            if (bro.IsSuccess() == false)
            {
                ShowResultData(bro);
            }
            
            var bro2 = Backend.CDN.Content.Get(bro.GetContentTableItemList());
            
            foreach (var item in bro2.GetContentList())
            {
                Debug.Log(item.ToString());
            }

            bool isSuccess = Backend.CDN.Content.Local.Save(bro2.GetContentList(), out Exception e);

            if (isSuccess == false)
            {
                Debug.LogError("Local Save Error : " + e);
            }
            
            ShowResultData(bro);
        });
    }
    
    private void LocalLoad()
    {
        requestUI.SetExecuteButtonAction( () =>
        {
            var bro = Backend.CDN.Content.Local.Load();
            
            foreach (var item in bro.GetContentList())
            {
                Debug.Log(item.ToString());
            }
            
            ShowResultData(bro);
        });
    }

    private void LocalDelete()
    {
        requestUI.AddInputField("삭제할 chartId1");
        requestUI.AddInputField("삭제할 chartId2");
        requestUI.AddInputField("삭제할 chartId3");
        requestUI.AddInputField("삭제할 chartId4");
        requestUI.AddInputField("삭제할 chartId5");

        requestUI.SetExecuteButtonAction(() =>
        {
            var idList = new List<string>();
            for (var i = 0; i < 5; i++)
            {
                var chartId = InputParseByStringWithSpecialChar(i, 0);
                if (string.IsNullOrEmpty(chartId))
                {
                    continue;
                }

                idList.Add(chartId);
            }

            var isSuccess = Backend.CDN.Content.Local.Delete(idList, out var e);
            if (isSuccess)
            {
                Debug.Log("Local Delete Success");

                var bro = Backend.CDN.Content.Local.Load();
                foreach (var item in bro.GetContentList())
                {
                    Debug.Log(item.ToString());
                }

                ShowResultData(bro);
            }
            else
            {
                Debug.LogError("Local Delete Error : " + e);
                ShowResultData(new BackendReturnObject());
            }
        });
    }

    private void LocalReset()
    {
        requestUI.SetExecuteButtonAction( () =>
        {
            try
            {
                Backend.CDN.Content.Local.Reset();
            }
            catch (Exception e)
            {
                Debug.LogError("Reset Error : " + e);
            }
            
            Debug.Log("Reset Success");
            
            ShowResultData(new BackendReturnObject());
        });
    }
    
    private void LocalUpdate()
    {
        requestUI.SetExecuteButtonAction( () => {
            var bro = Backend.CDN.Content.Table.Get();

            if (bro.IsSuccess() == false)
            {
                ShowResultData(bro);
            }
            
            var bro2 = Backend.CDN.Content.Local.Update(bro.GetContentTableItemList());
            
            foreach (var item in bro2.GetContentList())
            {
                Debug.Log(item.ToString());
            }

            ShowResultData(bro);
        });
    }
}