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

using System;
using UnityEngine;
using BackEnd;
using LitJson;

public class ChartGroup : BaseGroup {
    public override string GetGroupName() {
        return "차트 관리";
    }

    public override void SetSubFunctionButton() {
        UIManager.Instance.AddSubFunction("차트 리스트 조회", GetChartList);
        UIManager.Instance.AddSubFunction("차트 리스트 조회 V2", GetChartList);
        UIManager.Instance.AddSubFunction("폴더명으로 차트 리스트 조회", GetChartListByFolder);
        UIManager.Instance.AddSubFunction("폴더명으로 차트 리스트 조회 V2", GetChartListByFolderV2);
        UIManager.Instance.AddSubFunction("차트 내용 조회", GetChartContents);
        UIManager.Instance.AddSubFunction("차트 개별 조회 및 로컬 저장", GetOneChartAndSave);
        UIManager.Instance.AddSubFunction("폴더명 차트 조회 및 로컬 저장", GetChartByFolderAndSave);
        UIManager.Instance.AddSubFunction("폴더명 차트 조회 및 로컬 저장 V2", GetChartByFolderAndSaveV2);
        UIManager.Instance.AddSubFunction("모든 차트 조회 및 로컬 저장", GetAllChartAndSave);
        UIManager.Instance.AddSubFunction("모든 차트 조회 및 로컬 저장 V2", GetAllChartAndSaveV2);
        UIManager.Instance.AddSubFunction("로컬에 저장된 차트 조회", GetLocalChartData);
        UIManager.Instance.AddSubFunction("로컬 차트 삭제", DeleteLocalChartData);
    }

    private void GetChartList() {
        requestUI.SetExecuteButtonAction( () => {
            var bro = Backend.Chart.GetChartList();
            ShowResultData(bro);
        });
    }
    
    // [5.9.4] 차트 리스트 불러오기 : 파일 적용 안된 차트 제거, old 컬럼 제거
    private void GetChartListV2() {
        requestUI.SetExecuteButtonAction( () => {
            var bro = Backend.Chart.GetChartListV2();
            ShowResultData(bro);
        });
    }
    
    private void GetChartListByFolder() {
        requestUI.AddInputField("폴더 ID");
        
        requestUI.SetExecuteButtonAction( () => {

            var folderId = InputParseByInt(0, 0);

            var bro = Backend.Chart.GetChartListByFolder(folderId);
            ShowResultData(bro);
        }); 
    }
    
    // [5.9.4] 차트 폴더로 리스트 불러오기 : 파일 적용 안된 차트 제거, old 컬럼 제거
    private void GetChartListByFolderV2() {
        requestUI.AddInputField("폴더 ID");
        
        requestUI.SetExecuteButtonAction( () => {

            var folderId = InputParseByInt(0, 0);

            var bro = Backend.Chart.GetChartListByFolderV2(folderId);
            ShowResultData(bro);
        }); 
    }
    private void GetChartContents() {
        requestUI.AddInputField("File ID");
        
        requestUI.SetExecuteButtonAction( () => {

            string selectedChartFileId = requestUI.GetValueInputFieldList()[0].GetInputFields(0);
            
            var bro = Backend.Chart.GetChartContents(selectedChartFileId);
            ShowResultData(bro);
        }); 
    }
    
    private void GetOneChartAndSave() {
        requestUI.AddInputField("File ID");
        
        requestUI.SetExecuteButtonAction( () => {

            var chartFileId = InputParseByString(0, 0);
            
            var bro = Backend.Chart.GetOneChartAndSave(chartFileId);
            ShowResultData(bro);
        }); 
    }
    private void GetAllChartAndSave() {
        requestUI.AddInputField("차트명 자동 여부(bool)","true");

        requestUI.SetExecuteButtonAction( () => {

            var isChartKeyIsName = InputParseByBool(0, 0);

            var bro = Backend.Chart.GetAllChartAndSave(isChartKeyIsName);
            ShowResultData(bro);
        }); 
    }
    
    // [5.9.4] 차트 리스트로 불러와 저장 : 파일 적용 안된 차트 제거, old 컬럼 제거
    private void GetAllChartAndSaveV2() {
        requestUI.AddInputField("차트명 자동 여부(bool)","true");

        requestUI.SetExecuteButtonAction( () => {

            var isChartKeyIsName = InputParseByBool(0, 0);

            var bro = Backend.Chart.GetAllChartAndSaveV2(isChartKeyIsName);
            ShowResultData(bro);
        }); 
    }
    
    private void GetChartByFolderAndSave() {
        requestUI.AddInputField("차트 폴더 ID","000");
        requestUI.AddInputField("차트명 자동 여부(bool)","true");

        requestUI.SetExecuteButtonAction( () => {

            var folderId = InputParseByInt(0, 0);
            var isChartKeyIsName = InputParseByBool(1, 0);

            var bro = Backend.Chart.GetChartByFolderAndSave(folderId, isChartKeyIsName);
            ShowResultData(bro);
        }); 
    }
    
    // [5.9.4] 차트 리스트로 불러와 저장 : 파일 적용 안된 차트 제거, old 컬럼 제거
    private void GetChartByFolderAndSaveV2() {
        requestUI.AddInputField("차트 폴더 ID","000");
        requestUI.AddInputField("차트명 자동 여부(bool)","true");

        requestUI.SetExecuteButtonAction( () => {

            var folderId = InputParseByInt(0, 0);
            var isChartKeyIsName = InputParseByBool(1, 0);

            var bro = Backend.Chart.GetChartByFolderAndSaveV2(folderId, isChartKeyIsName);
            ShowResultData(bro);
        }); 
    }
    
    private void GetLocalChartData() {
        requestUI.AddInputField("chartKey");
        
        requestUI.SetExecuteButtonAction( () => {
            var chartKey = InputParseByString(0, 0);

            var broString = Backend.Chart.GetLocalChartData(chartKey);

            if (string.IsNullOrEmpty(broString)) {
                Alert($"로컬에 {chartKey}의 차트가 존재하지 않습니다.");
                return;
            }

            try {
                JsonData chartJson = JsonMapper.ToObject( broString );
                ShowResultData(chartJson);

            } catch (Exception e) {
                Alert("로컬 차트 파싱 도중 에러가 발생했습니다\n" + e);
                return;
            }
            
        }); 
    }
    
    private void DeleteLocalChartData() {
        requestUI.AddInputField("chartKey");
        
        requestUI.SetExecuteButtonAction( () => {
            var chartKey = InputParseByString(0, 0);

             Backend.Chart.DeleteLocalChartData(chartKey);
             UIManager.Instance.OpenResultUI("로컬 차트 삭제에 성공했습니다");
        }); 
    }
}