//
//  LoginController-VAccessor.m
//  BathyScaphe
//
//  Updated by Tsutomu Sawada on 10/03/22.
//  Copyright 2005-2010 BathyScaphe Project. All rights reserved.
//  encoding="UTF-8"
//

#import "LoginController_p.h"
#import "SG2chConnector.h"


@implementation LoginController(ViewAccessor)
- (NSButton *)cancelButton
{
    return m_cancelButton;
}

- (NSButton *)okButton
{
    return m_okButton;
}

- (NSTextField *)passwordField
{
    return m_passwordField;
}

- (NSButton *)shouldSavePWBtn
{
    return m_shouldSavePWBtn;
}

- (NSTextField *)userIDField
{
    return m_userIDField;
}
@end


@implementation LoginController(UISetup)
- (void)updateButtonState
{
    BOOL      canLogin_;
    NSString *value_;

    // パスワードが入力されていない、あるいは
    // ユーザIDが入力されていない場合は
    // ログインできない。
    value_ = [[self userIDField] stringValue];
    canLogin_ = (value_ && [value_ length] > 0);
    value_ = [[self passwordField] stringValue];
    canLogin_ = canLogin_ && (value_ && [value_ length] > 0);

    [[self okButton] setEnabled:canLogin_];
}

- (void)setupUIComponents
{
    NSString        *account_ = nil;
    NSString        *password_;
    BOOL            hasAccountInKeychain_;

    if (m_type == BSKeychainAccountX2chAuth) {
        account_ = [[self preferences] x2chUserAccount];
        [m_messageTextField setStringValue:PluginLocalizedStringFromTable(@"x2chAuth_msg", nil, nil)];
        [m_informativeTextField setStringValue:PluginLocalizedStringFromTable(@"x2chAuth_info", nil, nil)];
        [m_accountLabelField setStringValue:PluginLocalizedStringFromTable(@"x2chAuth_account_label", nil, nil)];
    } else if (m_type == BSKeychainAccountBe2chAuth) {
        account_ = [[self preferences] be2chAccountMailAddress];
        [m_messageTextField setStringValue:PluginLocalizedStringFromTable(@"be2chAuth_msg", nil, nil)];
        [m_informativeTextField setStringValue:PluginLocalizedStringFromTable(@"be2chAuth_info", nil, nil)];
        [m_accountLabelField setStringValue:PluginLocalizedStringFromTable(@"be2chAuth_account_label", nil, nil)];
    } else if (m_type == BSKeychainAccountP22chNetAuth) {
        account_ = [[self preferences] p22chUserAccount];
        [m_messageTextField setStringValue:PluginLocalizedStringFromTable(@"p22chAuth_msg", nil, nil)];
        [m_informativeTextField setStringValue:PluginLocalizedStringFromTable(@"p22chAuth_info", nil, nil)];
        [m_accountLabelField setStringValue:PluginLocalizedStringFromTable(@"p22chAuth_account_label", nil, nil)];
    }

    password_ = nil;
    hasAccountInKeychain_ = [[self preferences] hasAccountInKeychain:m_type];

    if (hasAccountInKeychain_) {
        password_ = [[self preferences] passwordForType:m_type error:NULL];
        if (!password_) {
            password_ = @"";
        }
        [[self passwordField] setEnabled:YES];
        [[self passwordField] setStringValue:password_];
    }

    [[self userIDField] setStringValue:(account_ ?: @"")];
    [self updateButtonState];
}
@end


@implementation LoginController(NSControlDelegate)
- (void)controlTextDidChange:(NSNotification *)aNotification
{
   UTILAssertNotificationName(
     aNotification,
      NSControlTextDidChangeNotification);

    [self updateButtonState];
}
@end
