//
//  KMAdvertisementConfig.swift
//  KMAdvertisement
//
//  Created by lizhe on 2022/11/24.
//

#if os(OSX)
    import AppKit
#elseif os(iOS)
    import UIKit
#endif

//APP类型
public enum KMAdvertisementAppNameType: String, Codable {
    case RecordIt = "RecordIt"
    case RecordItPro = "RecordItPro"
    case RecordItDMG = "RecordItDMG"
    case FilmageScreen = "FilmageScreen"
    case FilmageScreenPro = "FilmageScreenPro"
    case FilmageScreenDMG = "FilmageScreenDMG"
    case FilmageEditorMac = "FilmageEditorMac"
    case FilmageEditorMacPro = "FilmageEditorMacPro"
    case FilmageEditorMacDMG = "FilmageEditorMacDMG"
    case FilmageConverterMac = "FilmageConverterMac"
    case FilmageConverterMacPro = "FilmageConverterMacPro"
    case FilmageConverterMacDMG = "FilmageConverterMacDMG"
    case FilmagePlayerMac = "FilmagePlayerMac"
    case FilmagePlayerMacPro = "FilmagePlayerMacPro"
    case FilmagePlayerMacDMG = "FilmagePlayerMacDMG"
    case PDFReaderProMac = "PDFReaderProMac"
    case PDFReaderProMacPro = "PDFReaderProMacPro"
    case PDFReaderProMacDMG = "PDFReaderProMacDMG"
    case PDFReaderProIOS = "PDFReaderProIOS"
    case PDFReaderProIOSPro = "PDFReaderProIOSPro"
}

//显示样式
public enum KMAdvertisementShowType: String, Codable {
    case unknow = "unknow"
    case list = "list" //列表
    case pop = "pop"//弹窗
    case scroll = "scroll"//循环
    case view = "view" //视图显示
}

//平台
public enum KMAdvertisementPlatformType: String, Codable {
    case unknow = "unknow"
    case Mac = "Mac" //
    case iOS = "iOS" //
}

//订阅状态
public enum KMAdvertisementSubscribeType: String, Codable {
    case unknow = "unknow"
    case unsubscribed = "unsubscribed"//未订阅
    case subscribed = "subscribed"//已订阅
}

//点击视图后跳转选项
public enum KMAdvertisementActionType: String, Codable {
    case unknow = "unknow"
    case URL = "URL"//链接
    case comparative = "comparative"//比价表
}

//多语
public enum KMAdvertisementLanguageType: String {
    case unknow = "unknow"
    case en = "en"//
    case zhCN = "zhCN"//
    case zhTW = "zhTW"//
}

open class KMAdvertisementConfig: NSObject {
    var appName: KMAdvertisementAppNameType = .FilmageEditorMac
    var platform: KMAdvertisementPlatformType = .Mac
    var subscribeType: KMAdvertisementSubscribeType = .unsubscribed
    var language: KMAdvertisementLanguageType = .unknow
    
    func activityBaseURL() -> String {
        if (KMAdvertisementManager.manager.debug) {
            // 测试 服务器
            return "http://test-store.kdan.cn:3019"
        } else {
            //正式版 服务器
            return "https://content.filmagepro.com"
        }
    }
    
//    [AdvertNetworkTool configInfoWithAppID:@"FE" appType:AdvertNetworkAppTypeGetValue() platform:@"Mac"];
//    [AdvertNetworkTool loadDataWithcompletion:^(NSDictionary * _Nonnull info, NSError * _Nonnull error) {
//        if (!error) {
//            NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
//            if ([info isKindOfClass:[NSDictionary class]]) {
//                NSMutableArray *arr = [[NSMutableArray alloc] init];
//                for (NSDictionary *dict in info[@"data"]) {
//                    NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
//                    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//                    NSDate *startDate  = [formatter dateFromString:dict[@"start_at"]];
//                    NSDate *endDate  = [formatter dateFromString:dict[@"end_at"]];
//                    NSTimeInterval startTime = [startDate timeIntervalSince1970];
//                    NSTimeInterval endTime = [endDate timeIntervalSince1970];
//                    BOOL isTimeValid = NO;
//                    if (currentTime > startTime &&
//                        currentTime < endTime) {
//                        isTimeValid = YES;
//                    }
//                    if (startTime == 0 ||
//                        endTime == 0) {
//                        isTimeValid = YES;
//                    }
//                    if ([dict[@"status"] boolValue] && isTimeValid) {
//                        if (dict[@"style_name"] &&
//                            [dict[@"style_name"] isEqualToString:@"Editor_banner_style"]) {
//
//                            [arr addObject:dict];
//
//                        }
//                    }
//                }
//            }
//        }
//    }];
    
    public func initParameters(appName: KMAdvertisementAppNameType, subscribeType:KMAdvertisementSubscribeType, platform: KMAdvertisementPlatformType) {
        self.appName = appName
        self.subscribeType = subscribeType
        self.platform = platform
    }
    
    static func adapterDark(light: UIColor, dark: UIColor) -> UIColor {
#if os(iOS)
        if #available(iOS 13.0, *) {
            let color = UIColor.init { trainCollection -> UIColor in
                if trainCollection.userInterfaceStyle == UIUserInterfaceStyle.dark {
                    return dark
                } else {
                    return light
                }
            }
            return color
        }
#elseif os(OSX)
        if #available(macOS 10.14, *) {
            let dict = UserDefaults.standard.persistentDomain(forName: UserDefaults.globalDomain) ?? nil
            guard let dicts: Dictionary = dict else {
                print("不是字典类型")
                return light
            }
            
            guard let style = dicts["AppleInterfaceStyle"] as? String else {
                print("style is not string")
                return light
            }
            
            let isDarkMode : Bool = style.compare("Dark").rawValue == 0
            
            if isDarkMode {
                print("暗黑模式\(style)")
                return dark
            } else {
                print("正常模式\(style)")
                return light
            }
        }
#endif
    }
}
