Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Fluctio.FluctioSim.Common.Configuration;
using Fluctio.FluctioSim.EditorUtils.EditorGeneral;
using Fluctio.FluctioSim.EditorUtils.Gui;
using Fluctio.FluctioSim.Utils.Extensions;
using Fluctio.FluctioSim.Utils.General;
using UnityEditor;
using UnityEngine;

namespace Fluctio.FluctioSim.EditorCore.AccountManagement
{
	public class AccountWindow : ConfigurableEditorWindow
	{
		
		#region Window settings
		
		public override string Title => "Account Information";
		public override bool RequiresConstantRepaint => true;
		public override EditorWindowType WindowType => EditorWindowType.AlwaysOnTop;
		
		#endregion

		#region Overriden functions

		protected override void Initialize()
		{
			base.Initialize();
			_ = Account.PingAPI();
		}

		protected override void OnDisable()
		{
			base.OnDisable();
			Account.StopLogIn();
		}

		protected override void OnGUI()
		{
			base.OnGUI();
			DrawAccountSegment();
			EditorGUIExtensions.HorizontalLine();
			DrawLicenseSegment();
			DrawNetworkAvailability();
		}
		
		#endregion

		#region Account segment
		
		private static void DrawAccountSegment()
		{
			EditorGUILayout.LabelField("Account", EditorStyles.boldLabel);
			if (Account.IsLoggedIn)
			{
				DrawAccount();
			}
			else
			{
				DrawNoAccount();
			}
		}

		private static void DrawAccount()
		{
			DrawProfile();
			if (EditorGUILayout.LinkButton("Log out"))
			{
				Account.LogOut();
			}
		}

		private static void DrawProfile()
		{
			const float pictureSize = 50;
			using var horizontalScope = new EditorGUILayout.HorizontalScope();
			GUILayout.Box(Account.ProfilePicture, EditorStyles.label, GUILayout.Width(pictureSize), GUILayout.Height(pictureSize));
			
			using var verticalScope = new EditorGUILayout.VerticalScope();
			EditorGUILayout.LabelField(Account.Info.Email, GUILayout.Height(pictureSize));
			//TODO: "Manage profile" link?
		}

		private static void DrawNoAccount()
		{
			EditorGUILayout.HelpBox("You are not logged in\nPlease, log in to verify your license", MessageType.Error);
			if (EditorGUILayout.LinkButton("Log In / Sign Up"))
			{
				Account.StartLogIn();
			}
		}

		#endregion

		#region License segment

		private static void DrawLicenseSegment()
		{
			EditorGUILayout.LabelField("License", EditorStyles.boldLabel);

			if (Account.IsLicenseUpdating)
			{
				EditorGUILayout.LabelField("Updating information...");
				return;
			}

			DrawLicenseType();
			DrawLicenseGiftedBy();
			DrawLicenseExpirationDate();
			DrawLicenseActionButtons();
		}

		private static void DrawLicenseType()
		{
			switch (License.Type)
			{
				case LicenseType.None:
					EditorGUILayout.HelpBox($"You have no valid license and the trial has ended\nBuy a license to continue using {Config.RawName}", MessageType.Error);
					break;
				case LicenseType.Trial:
					EditorGUILayout.HelpBox("You have a trial license\nReminder: this license is only for evaluation purposes", MessageType.Warning);
					break;
				case >= LicenseType.Paid when Enum.IsDefined(typeof(LicenseType), License.Type):
					var licenseDisplayName = License.Type.AsField().GetDescription();
					EditorGUILayout.LabelField($"You have a {licenseDisplayName} license");
					break;
				default:
					throw new InvalidEnumArgumentException(
						nameof(License.Type),
						(int)License.Type,
						License.Type.GetType());
			}
		}

		private static void DrawLicenseGiftedBy()
		{
			if (License.GiftedBy != null)
			{
				EditorGUILayout.LabelField($"This license was gifted by {License.GiftedBy}");
			}
		}

		private static void DrawLicenseExpirationDate()
		{
			if (License.ValidTill != DateTime.MaxValue)
			{
				var timeLeft = License.ValidTill - DateTime.UtcNow;
				var validTillString = License.ValidTill.ToShortDateString();
				var timeLeftString = timeLeft.ToReadableString();
				EditorGUILayout.LabelField($"Valid till: {validTillString} ({timeLeftString} left)");
			}
			else if (License.Type != LicenseType.None)
			{
				EditorGUILayout.LabelField("This license has no expiration date");
			}
		}

		#endregion

		#region License action buttons

		private static void DrawLicenseActionButtons()
		{
			if (License.Type <= LicenseType.Trial)
			{
				DrawLicenseBuyButton();
			}
			else
			{
				DrawLicenseManageButton();
			}
			DrawLicenseRecheckButton();
		}

		private static void DrawLicenseBuyButton()
		{
			if (EditorGUILayout.LinkButton("Buy a license"))
			{
				var url = new UriBuilder("https://api.fluctio.ai/buy_fluctio_sim");
				if (Account.IsLoggedIn)
				{
					url.Query = new Dictionary<string, string>
					{
						["email"] = Account.Info.Email.NullIfWhiteSpace(),
						["first_name"] = Account.Info.GivenName.NullIfWhiteSpace(),
						["last_name"] = Account.Info.FamilyName.NullIfWhiteSpace(),
					}.ToQueryString();
				}
				Application.OpenURL(url.ToString());
			}
		}

		private static void DrawLicenseManageButton()
		{
			if (EditorGUILayout.LinkButton("Manage the license"))
			{
				Application.OpenURL("https://api.fluctio.ai/manage_subscription");
			}
		}

		private static void DrawLicenseRecheckButton()
		{
			if (Account.IsLoggedIn)
			{
				if (EditorGUILayout.LinkButton("Recheck"))
				{
					Account.UpdateLicenseInfoAsync();
				}	
			}
		}

		#endregion
		
		private static void DrawNetworkAvailability()
		{
			if (Account.IsOnline)
			{
				return;
			}
			
			EditorGUIExtensions.HorizontalLine();
			EditorGUILayout.LabelField("Internet Connection", EditorStyles.boldLabel);
			
			EditorGUILayout.HelpBox("There are problems with internet connection\nThe information might be outdated", MessageType.Warning);
		}
		
	}
}