using Godot;
        using System;
        using System.Threading.Tasks;
        using SessionZeroClient;
        using SessionZeroClient.API;
        
        public partial class AccountScreenLogic : Node
        {
            [ExportGroup("UI Elements")]
            // Selection Screen
            [ExportSubgroup("Selection Screen")]
            [Export] private VBoxContainer SelectionScreenVboxContainer { get; set; }
            [Export] private Button RegisterSelectionButton { get; set; }
            [Export] private Button LoginSelectionButton { get; set; }
            [Export] private LineEdit UrlLineEdit { get; set; }
            
            // Register Screen
            [ExportSubgroup("Register Screen")]
            [Export] private VBoxContainer RegisterScreenVboxContainer { get; set; }
            [Export] private LineEdit RegisterUsernameLineEdit { get; set; }
            [Export] private LineEdit RegisterEmailLineEdit { get; set; }
            [Export] private LineEdit RegisterPasswordLineEdit { get; set; }
            [Export] private Button RegisterButton { get; set; }
            
            // Login Screen
            [ExportSubgroup("Login Screen")]
            [Export] private VBoxContainer LoginScreenVboxContainer { get; set; }
            [Export] private LineEdit LoginUsernameLineEdit { get; set; }
            [Export] private LineEdit LoginPasswordLineEdit { get; set; }
            [Export] private Button LoginButton { get; set; }
            
            // Account Screen
            [ExportSubgroup("Account Screen")]
            [Export] private VBoxContainer AccountScreenVboxContainer { get; set; }
            [Export] private Label AccountScreenGreetingLabel { get; set; }
            [Export] private Button LogoutButton { get; set; }
            [Export] private Button OfflineModeButton { get; set; }
            
            // Other
            [ExportSubgroup("Other")]
            [Export] Button BackButton { get; set; }
            [Export] RichTextLabel StatusLabel { get; set; }
            
            private IApiHandler _apiHandler;
            private AccountManager _accountManager;
            private string _url = "";
            
            public void Init(IApiHandler apiHandler, AccountManager accountManager)
            {
                _apiHandler = apiHandler;
                _accountManager = accountManager;
                SetupSignals();
                InitialState();
            }
            
            private void InitialState()
            {
                SelectionScreenVboxContainer.Visible = true;
                RegisterScreenVboxContainer.Visible = false;
                LoginScreenVboxContainer.Visible = false;
                AccountScreenVboxContainer.Visible = false;
                BackButton.Visible = false;
            }
            
            private void RegisterState()
            {
                SelectionScreenVboxContainer.Visible = false;
                RegisterScreenVboxContainer.Visible = true;
                LoginScreenVboxContainer.Visible = false;
                AccountScreenVboxContainer.Visible = false;
                BackButton.Visible = true;
            }
            
            private void LoginState()
            {
                SelectionScreenVboxContainer.Visible = false;
                RegisterScreenVboxContainer.Visible = false;
                LoginScreenVboxContainer.Visible = true;
                AccountScreenVboxContainer.Visible = false;
                BackButton.Visible = true;
            }
            
            private void AccountSuccessState()
            {
                SelectionScreenVboxContainer.Visible = false;
                RegisterScreenVboxContainer.Visible = false;
                LoginScreenVboxContainer.Visible = false;
                AccountScreenVboxContainer.Visible = true;
                BackButton.Visible = false;
                
                AccountScreenGreetingLabel.Text = $"Welcome, {_accountManager.Username}!";
            }
        
            private void SetupSignals()
            {
                // Selection Screen
                RegisterSelectionButton.Pressed += RegisterSelectionButtonPressed;
                LoginSelectionButton.Pressed += LoginSelectionButtonPressed;
                OfflineModeButton.Pressed += () => GD.Print("Offline mode not implemented yet.");
                
                // Register Screen
                RegisterButton.Pressed += RegisterButtonPressed;
                
                // Login Screen
                LoginButton.Pressed += LoginButtonPressed;
                
                // Account Screen
                LogoutButton.Pressed += Logout;
                
                // Other
                BackButton.Pressed += BackButtonPressed;
            }
            
            private async void RegisterSelectionButtonPressed()
            {
                var (status, isValidServer) = await _apiHandler.ValidateServerUrl(UrlLineEdit.Text);
                if (status == OperationStatus.Success && isValidServer)
                {
                    _url = UrlLineEdit.Text;
                    RegisterState();
                }
                else
                {
                    SetStatusLabel($"Server validation failed: {status}", Colors.Red);
                }
            }
            
            private async void LoginSelectionButtonPressed()
            {
                var (status, isValidServer) = await _apiHandler.ValidateServerUrl(UrlLineEdit.Text);
                if (status == OperationStatus.Success && isValidServer)
                {
                    _url = UrlLineEdit.Text;
                    LoginState();
                }
                else
                {
                    SetStatusLabel($"Server validation failed: {status}", Colors.Red);
                }
            }
            
            private async void RegisterButtonPressed()
            {
                var username = RegisterUsernameLineEdit.Text;
                var email = RegisterEmailLineEdit.Text;
                var password = RegisterPasswordLineEdit.Text;
                
                var (status, response) = await _accountManager.Register(_url, username, email, password);
                
                SetStatusLabel($"Register status: {status}", status == OperationStatus.Success ? Colors.Green : Colors.Red);
                
                if (status == OperationStatus.Success)
                {
                    var (loginStatus, loginResponse) = await _accountManager.Login(_url, username, password);
                    
                    SetStatusLabel($"Login status: {loginStatus}", loginStatus == OperationStatus.Success ? Colors.Green : Colors.Red);
        
                    if (loginStatus == OperationStatus.Success)
                    {
                        AccountSuccessState();    
                    }
                    else
                    {
                        SetStatusLabel($"Login failed: {loginStatus}", Colors.Red);
                    }
                }
                else
                {
                    SetStatusLabel($"Registration failed: {status}", Colors.Red);
                }
            }
            
            private async void LoginButtonPressed()
            {
                var username = LoginUsernameLineEdit.Text;
                var password = LoginPasswordLineEdit.Text;
                var (status, response) = await _accountManager.Login(_url, username, password);
                
                SetStatusLabel($"Login status: {status}", status == OperationStatus.Success ? Colors.Green : Colors.Red);
                
                if (status == OperationStatus.Success)
                {
                    AccountSuccessState();    
                }
                else
                {
                    if (status == OperationStatus.GeneralError)
                    {
                        SetStatusLabel($"Login failed: Check username and password", Colors.Red);
                        return;
                    }
                    
                    SetStatusLabel($"Login failed: {status}", Colors.Red);
                }
            }
        
            private void Logout()
            {
                _accountManager.Logout();
                GD.Print("Logged out");
                SetStatusLabel("Logged out", Colors.Cyan);
                InitialState();
            }
            
            private void BackButtonPressed()
            {
                InitialState();
            }
            
            private void SetStatusLabel(string message, Color color)
            {
                StatusLabel.Text = $"[color={color.ToHtml()}] {message} [/color]";
            }
        }