#if UNITY_EDITOR
using Reinterop;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using UnityEngine.Networking;


namespace CesiumForUnity
{
    [Reinterop]
    internal partial class ConfigureReinteropEditor
    {
        public void ExposeToCPP()
        {
            Debug.Log("log");

            UnityWebRequest request = UnityWebRequest.Get("url");
            request.Abort();
            request.Dispose();

            var uploadHandler = new UploadHandlerRaw(new byte[0]);

            var rawBytes = new NativeArray<byte>(1, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            unsafe
            {
                NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(rawBytes);
            }
            uploadHandler = new UploadHandlerRaw(rawBytes, true);
            request = new UnityWebRequest("url", "method", new NativeDownloadHandler(), uploadHandler);

            bool isDone = request.isDone;
            string e = request.error;
            string method = request.method;
            string url = request.url;
            if (request.result == UnityWebRequest.Result.Success) { }
            ;
            request.downloadHandler = new NativeDownloadHandler();
            request.SetRequestHeader("name", "value");
            request.GetResponseHeader("name");
            Dictionary<string, string>.Enumerator enumerator = request.GetResponseHeaders().GetEnumerator();
            while (enumerator.MoveNext())
            {
                string key = enumerator.Current.Key;
                string value = enumerator.Current.Value;
            }
            request.downloadHandler.Dispose();
            long responseCode = request.responseCode;
            UnityWebRequestAsyncOperation op = request.SendWebRequest();
            op.completed += o => { };

            Task.Run(() => { });

            CesiumIonSession session = CesiumIonServerManager.instance.GetSession(null);
            CesiumIonServer server = CesiumIonServerManager.instance.current;
            session = CesiumIonServerManager.instance.currentSession;
            session.IsConnected();
            server = session.server;
            session.server = server;

            IReadOnlyList<CesiumIonServer> servers = CesiumIonServerManager.instance.servers;
            for (int i = 0; i < servers.Count; ++i)
            {
                server = servers[i];
                session = CesiumIonServerManager.instance.GetSession(server);
                bool isBusy = session.IsBusy();
            }            

            List<QuickAddItem> items = new List<QuickAddItem>();
            items = session.GetQuickAddItems();

            QuickAddItem item = new QuickAddItem();
            item.type = QuickAddItemType.BlankTileset;
            item.name = "name";
            item.tooltip = "tooltip";
            item.tilesetName = "tilesetName";
            item.tilesetId = 1;
            item.overlayName = "overlayName";
            item.overlayId = 2;

            item = new QuickAddItem(QuickAddItemType.IonTileset, "name", "tooltip", "tilesetName", 1, "overlayName", 2);
            items.Add(item);

            server.defaultIonAccessToken = "";
            server.defaultIonAccessTokenId = "";
            server.apiUrl = "";
            server.serverUrl = "";
            server.oauth2ApplicationID = 1;

            CesiumIonServerManager.instance.GetUserAccessToken(null);
            CesiumIonServerManager.instance.GetUserRefreshToken(null);
            CesiumIonServerManager.instance.SetUserAccessTokenAndRefreshToken(null, null, null);

            EditorPrefs.HasKey("Key");
            EditorPrefs.GetString("Key");
            EditorPrefs.SetString("Key", "Value");
            EditorPrefs.DeleteKey("Key");

            Application.OpenURL("URL");
            string temporaryCachePath = Application.temporaryCachePath;
            string applicationVersion = Application.version;
            string unityVersion = Application.unityVersion;
            string applicationPlatform = Helpers.ToString(Application.platform);
            string productName = Application.productName;
            string osVersion = System.Environment.OSVersion.VersionString;

            TreeViewItem root = new TreeViewItem(0, -1, "root");
            int id = root.id;

            TreeViewState treeState = new TreeViewState();
            IonAssetsTreeView treeView = new IonAssetsTreeView(treeState);
            treeView.Reload();

            MultiColumnHeader header = treeView.multiColumnHeader;
            int sorted = header.sortedColumnIndex;
            bool ascending = header.IsSortedAscending(sorted);

            string searchString = treeView.searchString;
            int length = searchString.Length;
            searchString.Contains("string", StringComparison.CurrentCultureIgnoreCase);
            string.Equals("stringA", "stringB");
            string.IsNullOrEmpty("value");

            Rect r = new Rect(0, 0, 50, 50);
            GUI.Label(r, "Label");

            session.BroadcastConnectionUpdate();
            session.BroadcastAssetsUpdate();
            session.BroadcastProfileUpdate();
            session.BroadcastTokensUpdate();
            session.BroadcastDefaultsUpdate();

            CesiumEditorWindow editorWindow = CesiumEditorWindow.currentWindow;

            IonAssetDetails.FormatType("type");
            IonAssetDetails.FormatDate("date");

            SelectIonTokenWindow.GetDefaultNewTokenName();
            SelectIonTokenWindow.ShowWindow(null);
            SelectIonTokenWindow tokenWindow = SelectIonTokenWindow.currentWindow;
            tokenWindow.server = null;
            tokenWindow.tokenSource = IonTokenSource.Create;
            string name = tokenWindow.createdTokenName;
            int tokenIndex = tokenWindow.selectedExistingTokenIndex;
            tokenIndex = 0;
            string token = tokenWindow.specifiedToken;
            List<string> tokens = tokenWindow.GetExistingTokenList();
            tokens.Clear();
            tokens.Add(token);
            tokenWindow.RefreshExistingTokenList();
            tokenWindow.Close();

            int requestsPerCachePrune = CesiumRuntimeSettings.requestsPerCachePrune;
            ulong maxItems = CesiumRuntimeSettings.maxItems;

            Cesium3DTileset[] tilesets = UnityEngine.Object.FindObjectsOfType<Cesium3DTileset>();
            Cesium3DTileset tileset = tilesets[0];
            for (int i = 0; i < tilesets.Length; i++)
            {
                tileset = tilesets[i];
                tileset.tilesetSource = CesiumDataSource.FromCesiumIon;
                token = tileset.ionAccessToken;
                tileset.RecreateTileset();
            }

            tileset = tileset.gameObject.GetComponent<Cesium3DTileset>();

            CesiumIonRasterOverlay ionOverlay = tileset.gameObject.GetComponent<CesiumIonRasterOverlay>();
            token = ionOverlay.ionAccessToken;
            ionOverlay.Refresh();
            GameObject gameObject = new GameObject("Name");
            tileset = gameObject.AddComponent<Cesium3DTileset>();
            ionOverlay = gameObject.AddComponent<CesiumIonRasterOverlay>();
            tileset.ionAssetID = 0;
            ionOverlay.ionAssetID = 0;

            CesiumRasterOverlay[] rasterOverlays = tileset.gameObject.GetComponents<CesiumRasterOverlay>();
            CesiumRasterOverlay overlay = rasterOverlays[0];
            UnityEngine.Object.DestroyImmediate(overlay, true);
            UnityEngine.Object.DestroyImmediate(overlay);

            CesiumIonRasterOverlay[] ionRasterOverlays =
                tileset.gameObject.GetComponents<CesiumIonRasterOverlay>();
            ionOverlay = ionRasterOverlays[0];

            string substring = "string";
            substring = string.Concat(substring, new long[] { 100 });
            string message = string.Concat(substring, "string");
            Debug.Log(message);
            Debug.LogWarning(message);
            Debug.LogError(message);

            Selection.activeGameObject = gameObject;

            CesiumEditorUtility.FindFirstTileset();
            CesiumEditorUtility.FindFirstTilesetWithAssetID(0);
            CesiumEditorUtility.CreateTileset("name", 0);
            CesiumEditorUtility.AddBaseOverlayToTileset(tileset, 0);

            IonMissingAssetWindow.ShowWindow("Asset Name", 0);

            IonTokenTroubleshootingWindow troubleshootingWindow = null!;
            IonTokenTroubleshootingWindow.UseDefaultToken(troubleshootingWindow.ionAsset);
            TokenTroubleshootingDetails tokenDetails = troubleshootingWindow.assetTokenDetails;
            tokenDetails = troubleshootingWindow.defaultTokenDetails;
            token = tokenDetails.token;
            tokenDetails.isValid = true;
            tokenDetails.allowsAccessToAsset = true;
            tokenDetails.associatedWithUserAccount = true;
            tokenDetails.loaded = true;

            CesiumIonAsset asset = troubleshootingWindow.ionAsset;
            name = asset.objectName;
            string type = asset.type;
            string componentType = asset.componentType;
            string accessToken = asset.ionAccessToken;
            long assetId = asset.ionAssetID;
            bool isNull = asset.IsNull();
            tileset = asset.tileset;
            server = tileset.ionServer;
            ionOverlay = asset.overlay;
            server = ionOverlay.ionServer;

            AssetTroubleshootingDetails assetDetails = troubleshootingWindow.assetDetails;
            assetDetails.assetExistsInUserAccount = true;
            assetDetails.loaded = true;

            EditorApplication.ExecuteMenuItem("Window/General/Hierarchy");

            EditorUtility.SetDirty(null);

            System.Exception exception = null;
            var exceptionMessage = exception.Message;
            
            AssemblyReloadEvents.afterAssemblyReload += () => { };
            AssemblyReloadEvents.beforeAssemblyReload += () => { };
        }
    }
}
#endif
