using Godot;
using System;
using SessionZeroClient;

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; }
    
    // Other
    [ExportSubgroup("Other")]
    [Export] Button BackButton { get; set; }
    
    
    private AccountManager _accountManager;
    private string _url = "";
    
    public void Init(AccountManager accountManager)
    {
        _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;
        
        // Register Screen
        RegisterButton.Pressed += RegisterButtonPressed;
        
        // Login Screen
        LoginButton.Pressed += LoginButtonPressed;
        
        // Account Screen
        LogoutButton.Pressed += Logout;
        
        // Other
        BackButton.Pressed += BackButtonPressed;
    }
    
    private void RegisterSelectionButtonPressed()
    {
        _url = UrlLineEdit.Text; // TODO: Add validation
        RegisterState();
    }
    
    private void LoginSelectionButtonPressed()
    {
        _url = UrlLineEdit.Text; // TODO: Add validation
        LoginState();
    }
    
    private async void RegisterButtonPressed()
    {
        var username = RegisterUsernameLineEdit.Text;
        var email = RegisterEmailLineEdit.Text;
        var password = RegisterPasswordLineEdit.Text;
        
        var success = await _accountManager.Register(_url, username, email, password);
        
        GD.Print($"Register success: {success}");
        
        if (success)
        {
            var loginSuccess = await _accountManager.Login(_url, username, password);
            
            GD.Print($"Login success: {loginSuccess}");

            if (loginSuccess)
            {
                AccountSuccessState();    
            }
        }
    }
    
    private async void LoginButtonPressed()
    {
        var username = LoginUsernameLineEdit.Text;
        var password = LoginPasswordLineEdit.Text;
        var success = await _accountManager.Login(_url, username, password);
        
        GD.Print($"Login success: {success}");
        
        if (success)
        {
            AccountSuccessState();    
        }
    }
    
    private void Logout()
    {
        _accountManager.Logout();
        InitialState();
    }
    
    private void BackButtonPressed()
    {
        InitialState();
    }
}
