//
//  PPCDCommon.h
//  PPCashDeskSDK
//
//  Created by 刘晓祥 on 2026/1/5.
//

#import <UIKit/UIKit.h>

/* Weak Self */
#pragma mark - Weak Self
#define weakify_self __weak typeof(self) weakSelf = self;
#define strongify_self __strong typeof(weakSelf) strongSelf = weakSelf;

/* Device Infos */
#pragma mark - Device Infos

/* App 版本 */
#define AppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

/* 当前系统版本 */
#define SystemVersion [[[UIDevice currentDevice] systemVersion] floatValue]

/* 是否iPad */
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
/* 是否iPhone */
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
/* 是否iPhone X系列设备 */
#define IS_IPHONE_X ({BOOL isX = NO; if (@available(iOS 11.0, *)) {UIEdgeInsets insets = PPCDKeyWindow().safeAreaInsets; isX = insets.bottom > 0.0;} else {isX = NO;} isX;})

/* 屏幕尺寸 */
#define SCREEN_SIZE [[UIScreen mainScreen] bounds].size
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define SAFEAREAINSETS ({UIEdgeInsets i; if (@available(iOS 11.0, *)) {if (IS_IPHONE_X) { i = UIEdgeInsetsMake(22, 22, 21, 22); } else { i = UIEdgeInsetsZero; }} else { i = UIEdgeInsetsZero; } i;})
#define SAFEAREA_BOTTOM ({CGFloat bottom; if (@available(iOS 11.0, *)) {if (IS_IPHONE_X) { bottom = 21; } else { bottom = 0; }} else { bottom = 0; } bottom;})

#pragma mark - Sandbox
#define SandboxDirectory(type) NSSearchPathForDirectoriesInDomains(type, NSUserDomainMask, YES).firstObject
#define LPHDocumentDirectory SandboxDirectory(NSDocumentDirectory) /* 沙盒地址，Document */
#define LPHCacheDirectory SandboxDirectory(NSCachesDirectory)      /* 沙盒地址，Cache */
#define LPHLibraryDirectory SandboxDirectory(NSLibraryDirectory)   /* 沙盒地址，Library */

#define PPCDColorBlue PPCDColorWithHexString(@"1A83F7")
#define PPCDColorGreen PPCDColorWithHexString(@"17c883")
#define PPCDAlhpaGray [UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:0.7f]
#define PPCDTextGray [UIColor colorWithRed:178/255.0 green:178/255.0 blue:178/255.0 alpha:1.0f]
#define PPLightGrayBordColorCGColor [UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:1.0f].CGColor
#define PPLightGrayBordColor [UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:1.0f]
#define PPInputerBoardColor [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1.0f]
#define PPCDMASKCOLOR [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5f]
#define PPCDCVVGrayColor [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:0.16f]


#define PPCDCardListBoardColor [UIColor colorWithRed:236.0/255.0f green:236.0/255.0f blue:236.0/255.0f alpha:1.0f]
//收银台背景色色值
#define DeskBgColorHexString @"DeskBgColorHexString"

//支付按钮文字色值
#define DeskPayBtnTitleColorHexString @"DeskPayBtnTitleColorHexString"

//收银台首页列表字体大小
#define DeskMainListTextFontValue @"DeskMainListTextFontValue"

//卡Bin位数长度
#define DeskCardBinLengthValue @"DeskCardBinLengthValue"

//Bugly APPID
#define PPCDBUGLY_APPID @""

#define PPCDRequestIdKey @"PPCDRequestIdKey"

#define PPCDRequestIdLength 64

#define PPCDCardViewFooterHieght 60

#define PPCDAPPLEPAYMERCHANTID @"PPCDAPPLEPAYMERCHANTID"

static NSInteger PPCDWindowViewTag = 10000001;

extern UIWindow * _Nullable PPCDKeyWindow(void);


// 16位字符串转UIColor (支持 RGB: #RRGGBB / 0xRRGGBB 和 ARGB: #AARRGGBB / 0xAARRGGBB)
static inline UIColor * _Nonnull PPCDColorWithHexString(NSString * _Nullable hexString) {
    NSString *cleanString = [hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    cleanString = [cleanString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    cleanString = [cleanString stringByReplacingOccurrencesOfString:@"0x" withString:@""];
    cleanString = [cleanString stringByReplacingOccurrencesOfString:@"0X" withString:@""];

    if ([cleanString length] == 6) {
        // RGB 格式: #RRGGBB / 0xRRGGBB
        unsigned int rgbValue = 0;
        NSScanner *scanner = [NSScanner scannerWithString:cleanString];
        [scanner scanHexInt:&rgbValue];
        return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0
                               green:((rgbValue & 0x00FF00) >> 8)/255.0
                                blue:(rgbValue & 0x0000FF)/255.0
                               alpha:1.0];
    } else if ([cleanString length] == 8) {
        // ARGB 格式: #AARRGGBB / 0xAARRGGBB
        unsigned int argbValue = 0;
        NSScanner *scanner = [NSScanner scannerWithString:cleanString];
        [scanner scanHexInt:&argbValue];

        CGFloat alpha = ((argbValue & 0xFF000000) >> 24) / 255.0;
        CGFloat red   = ((argbValue & 0x00FF0000) >> 16) / 255.0;
        CGFloat green = ((argbValue & 0x0000FF00) >> 8) / 255.0;
        CGFloat blue  = ((argbValue & 0x000000FF)) / 255.0;

        return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    } else {
        return [UIColor blackColor];
    }
}

#define PPCDUIEdge 15

#define PPCDUIScaleLarge 20

#define PPCDUIScale 10

#define PPCDUIScaleSmall 4

#define PPCDCompletedDelay 2


NS_ASSUME_NONNULL_BEGIN

typedef enum{
    English,    //英语
    ChineseSimple, //汉语简体
    ChineseTraditional,    //汉语繁体
//    Russia,     //俄语
//    Japanese,   //日语
//    Spain,  //西班牙
//    Portugal,    //葡萄牙
//    Thailand    //泰国
}PPCDSDKLanguegeType;

@interface PPCDCommon : NSObject


+ (instancetype)sharedInstance;

+ (NSBundle *)defaultBundle;

/// 读取默认Bundle图片
/// - Parameter name: 图片名
+ (UIImage *)imageNamed:(NSString *)name;

/// 显示提示框
/// - Parameters:
///   - title: 提示框标题
///   - contentStr: 提示内容
///   - confirmStr: 确认按钮标题
///   - vc: 提示框显示在那个VC
+ (void)showAlertViewWithTitle:(NSString *)title andContent:(NSString *)contentStr andConfirmStr:(NSString *)confirmStr andVC : (UIViewController *)vc;

/// 设置SDK的语言
/// - Parameter type: 语言类型
+ (void)settingSDKLanguegeType:(PPCDSDKLanguegeType )type;

/// 写入日志到文件
/// - Parameter logString: 日志内容
- (void)writeLogToFile:(NSString *)logString;

/// 读取当前日志文件内容
/// - Returns: 日志文件内容
- (NSString *)readCurrentLog;

/// 获取日志文件路径
/// - Returns: 日志文件路径
- (NSString *)logFilePath;

/// 清空日志文件
- (void)clearLogFile;

/// 获取设备唯一识别码
+ (NSString *)getDeviceUniqueIdentifier;

/// 获取当前时间，格式为yyyy-MM-dd HH:mm:ss
+ (NSString *)getCurrentTime;

+ (NSString *)makeEncryptedPayloadByCardNum:(NSString *)cardNum andcardBrand:(NSString *)cardBrand andExpireYear:(NSString *)expireYear andExpireMonth:(NSString *)expireMonth andCVVCode:(NSString *)cvvCode andCardToken:(NSString *)cardToken andPin:(NSString *)pin andPublicKey:(NSString *)publicKey;

/// 查找视图所在的当前视图控制器
/// - Parameter view: 要查找的视图
/// - Returns: 当前视图控制器
+ (UIViewController *)findCurrentViewControllerForView:(UIView *)view;

+ (UIViewController *)getTopViewController;

/**
 随机生成一个长度不超过64位的随机字符串
 
 @param length 字符串长度，最大为64
 @return 生成的随机字符串
 */
+ (NSString *)generateRandomStringWithLength:(NSUInteger)length;

/// 获取国家信息列表
/// - Returns: 国家信息数组
+ (NSArray *)getCountries;

/// 根据国家信息获取对应省份列表
/// - Parameter country: 国家对象
/// - Returns: 省份列表
+ (NSArray *)getProvincesForCountry:(id)country;

/// 显示支付完成确认提示框
/// - Parameters:
///   - vc: 提示框显示在那个VC
///   - paidBlock: 点击paid按钮的回调
///   - notPaidBlock: 点击Not paid按钮的回调
+ (void)showPaymentCompletedAlertWithVC:(UIViewController *)vc paidBlock:(void(^)(void))paidBlock notPaidBlock:(void(^)(void))notPaidBlock;

/// 使用Luhn算法验证卡号
/// - Parameter cardNumber: 要验证的卡号
/// - Returns: 卡号是否有效
+ (BOOL)validCardNumber:(NSString *)cardNumber;

#pragma mark -- key window

+ (UIWindow *)keyWindow;

@end

NS_ASSUME_NONNULL_END
