//
//  XXGMobileTextField.m
//  XXGPlayKit
//
//  Created by apple on 2025/4/1.
//

#import "XXGMobileTextField.h"
#import "XXGUIDriver.h"
#import "Masonry.h"
#import "NSString+XXGString.h"
#ifdef XXGPLAYKITOS_TARGET
    #import "XXGCountryCodeButton.h"
#endif

@interface XXGMobileTextField()

#ifdef XXGPLAYKITOS_TARGET
@property (nonatomic,strong) XXGCountryCodeButton *xxpk_countryCodeButton;
#endif


@end

@implementation XXGMobileTextField

- (instancetype)initWithController:(UIViewController *)vc
{
    self = [super init];
    if (self) {
        self.layer.borderColor = [XXGUIDriver xxpk_mainColor].CGColor;
        self.layer.borderWidth = 0.6;
        self.layer.cornerRadius = 2;
        
#ifdef XXGPLAYKITOS_TARGET
        // 登录/注册按钮
        self.xxpk_countryCodeButton = [[XXGCountryCodeButton alloc] initWithCurrentViewController:vc];
        [self addSubview:self.xxpk_countryCodeButton];
        [self.xxpk_countryCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.height.mas_equalTo(XXGUIDriver.xxpk_data_ui.xxpk_float38);
        }];
        // 提高按钮水平的 content hugging 优先级，让它不会拉伸填充剩余空间
        [self.xxpk_countryCodeButton setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
#endif
        
        // xx手机号
        self.xxpk_mobileTextField = [XXGUIDriver xxpk_textFieldOfMobile];
        self.xxpk_mobileTextField.layer.borderWidth = 0;
        self.xxpk_mobileTextField.layer.cornerRadius = 2.f;
        self.xxpk_mobileTextField.layer.maskedCorners = kCALayerMaxXMaxYCorner | kCALayerMaxXMinYCorner;
        self.xxpk_mobileTextField.layer.masksToBounds = YES;
        [self addSubview:self.xxpk_mobileTextField];
        [self.xxpk_mobileTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
#ifdef XXGPLAYKITOS_TARGET
            make.left.mas_equalTo(self.xxpk_countryCodeButton.mas_right);
#endif
#ifdef XXGPLAYKITCN_TARGET
            make.left.mas_equalTo(0);
#endif
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(XXGUIDriver.xxpk_data_ui.xxpk_float38);
        }];
    }
    return self;
}

- (NSString *)xxpk_dial_code {
#ifdef XXGPLAYKITOS_TARGET
    return [NSString stringWithFormat:@"%@%@",XXGUIDriver.xxpk_data_ui.xxpk_code_jia,[self.xxpk_countryCodeButton.xxpk_currentCountry.xxpk_dialCode stringByReplacingOccurrencesOfString:@" " withString:@""]];
#endif
    return @"";
}

- (NSString *)xxpk_mobile_num {
    return self.xxpk_mobileTextField.text.xxpk_isNotEmpty ? [NSString stringWithFormat:@"%@%@",self.xxpk_dial_code,self.xxpk_mobileTextField.text] : @"";
}
@end
