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

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
using BackEnd;
using TMPro;
using UnityEngine.UI;

public enum DataType {
    NORMAL,
    LIST,
    DICTIONARY,
    TRANSACTION_INSERT,
    TRANSACTION_UPDATE,
    TRANSACTION_DELETE,
    TRANSACTION_READ_INDATE,
    TRANSACTION_READ_QUERY,
    PLAYER_DATA_TRANSACTION_INSERT,
    PLAYER_DATA_TRANSACTION_UPDATE,
    PLAYER_DATA_TRANSACTION_DELETE,
    PLAYER_DATA_TRANSACTION_READ_ALL,
    PLAYER_DATA_TRANSACTION_READ_OTHER,
    PLAYER_DATA_TRANSACTION_READ_MY,

}

public class ValueInputField : MonoBehaviour {

    
    [Header("부모 객체")]
    [SerializeField] private GameObject oneValueObject;
    [SerializeField] private GameObject severalValueObject;
    
    [Header("값 1개 필드")]
    [SerializeField] private TMP_Text oneValueText;
    [SerializeField] private TMP_InputField oneValueInputField;
    
    [Header("값 여러개 필드")]
    [SerializeField] private TMP_Text[] severalValueTexts;
    [SerializeField] private TMP_InputField[] severalValueInputFields;


    private int _currentValueCount = 0;
    public DataType dataType = DataType.NORMAL;
    
    public string GetInputFields(int index, bool isSpecialChar = true) {
        if (index > _currentValueCount) {
            throw new Exception($"현재 인자의 갯수는 다음과 같습니다({_currentValueCount})\n입력하신 값은 다음과 같습니다({index})");
        }
        
        if (_currentValueCount == 0) {
            throw new Exception($"함수의 인자값 하나당 최대 4개까지 값을 입력할 수 있습니다.(입력값 : {_currentValueCount}");
        }

        string inputString = string.Empty;
        if (_currentValueCount == 1) {
            inputString = oneValueInputField.text;
        } else {
            // 여러 value(0은 솔로 value가 사용중이므로 편의를 위해 0처럼 보이게 한다)

            inputString = severalValueInputFields[index].text;
        }
        

        inputString = inputString.TrimStart().TrimEnd();
        
        if (oneValueInputField.text.EndsWith("\n")) {
            inputString = inputString.TrimEnd();
        }


        if (isSpecialChar) {
            string result = Regex.Replace(inputString, @"[^a-zA-Z0-9가-힣\s-_:.@]", "");
            if (result != inputString) {
            
                throw new Exception($"{inputString} 인자값에 특수한 문자가 들어가있습니다('-', '_', ':' 및 띄어쓰기를 제외한 특수문자)");
            }
        }

        
        return inputString;
    }


    public void Reset() {
        gameObject.SetActive(false);
    }
    
    public void AddInputField(string valueName, string placeHolderName) {
        gameObject.SetActive(true);

        _currentValueCount = 1;
        
        oneValueObject.gameObject.SetActive(true);
        severalValueObject.gameObject.SetActive(false);

        oneValueText.text = valueName;
        oneValueInputField.text = placeHolderName;
    }

    public void AddInputField(string[] valueNameList, DataType settingDataType) {
        gameObject.SetActive(true);

        dataType = settingDataType;
        
        _currentValueCount = valueNameList.Length;
        
        if (_currentValueCount == 0) {
            AlertUI.Instance.OpenUI($"함수의 인자값 하나당 최대 4개까지 값을 입력할 수 있습니다.(입력값 : {_currentValueCount}");
            return;
        }  else if (_currentValueCount == 1) {
            AddInputField(valueNameList[0], String.Empty);
        }  else if (_currentValueCount > 4) {
            AlertUI.Instance.OpenUI($"함수의 인자값 하나당 최대 4개까지 값을 입력할 수 있습니다.(입력값 : {_currentValueCount}");
            return;
        }

        oneValueObject.gameObject.SetActive(false);
        severalValueObject.gameObject.SetActive(true);

        // 0번째는 oneValue에서 할당되어있으므로 1부터 시작.
        for (int i = 0; i < _currentValueCount; i++) {
            severalValueTexts[i].text = valueNameList[i];
            severalValueInputFields[i].text = string.Empty;
            
            severalValueTexts[i].transform.parent.gameObject.SetActive(true);
        }

        for (int i = _currentValueCount; i <  severalValueTexts.Length;  i++) {
            severalValueTexts[i].transform.parent.gameObject.SetActive(false);
        }
    }
    
    public void AddInputField(string valueName, DataType settingDataType) {
        dataType = settingDataType;
        
        gameObject.SetActive(true);

        _currentValueCount = 1;
        
        oneValueObject.gameObject.SetActive(true);
        severalValueObject.gameObject.SetActive(false);

        oneValueText.text = valueName;

    }
}