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

using System;
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("로컬 업데이트", 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("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 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);
        });
    }
}