//
//  Gdpr.swift
//  GoogleUtilities
//
//  Created by cxb on 2023/5/23.
//

import UIKit
import UserMessagingPlatform

//NOT_REQUIRED 与REQUIRED sdk中status是相反的，这里转换成与安卓一致
enum ConsentStatus :Int{
    // Consent Form Not Available
    case NOT_AVAILABLE = -100;
    // Unknown consent status.
    case UNKNOWN = 0;
    // User consent required but not yet obtained.
    case NOT_REQUIRED = 1;
    // User consent not required. For example, the user is not in the EEA or the UK.
    case REQUIRED = 2;
    // User consent obtained. Personalization not defined.
    case OBTAINED = 3;
    
}

///android
//public @interface ConsentStatus {
//      int UNKNOWN = 0;
//      int NOT_REQUIRED = 1;
//      int REQUIRED = 2;
//      int OBTAINED = 3;
//  }

/// ios
//typedef NS_ENUM(NSInteger, UMPConsentStatus) {
//  UMPConsentStatusUnknown = 0,      ///< Unknown consent status.
//  UMPConsentStatusRequired = 1,     ///< User consent required but not yet obtained.
//  UMPConsentStatusNotRequired = 2,  ///< Consent not required.
//  UMPConsentStatusObtained =
//      3,  ///< User consent obtained, personalized vs non-personalized undefined.
//};


//class Gdpr {
//    static func initialize(debugGeography:Int,testDeviceId:String,_ completion: @escaping ((Swift.Result<Bool, Error>) -> Void)) {
//        let parameters = UMPRequestParameters()
//        // 设置未满同意年龄的标签。此处false表示用户达到年龄
//        //"135F6955-4691-4488-946F-2F627C3DE714"
//        parameters.tagForUnderAgeOfConsent = false
//        if(!testDeviceId.isEmpty && debugGeography != 0){
//            let debugSettings = UMPDebugSettings()
//            debugSettings.testDeviceIdentifiers = [testDeviceId]
//            debugSettings.geography = UMPDebugGeography(rawValue: debugGeography) ?? .disabled
//            parameters.debugSettings = debugSettings
//        }
//
//        // 请求最新同意信息
//        UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(
//            with: parameters,
//            completionHandler: { error in
//                if let error = error {
//                    // 请求同意信息失败
//                    completion(.failure(error))
//
//                } else {
//                    // 判断是否需要加载表单
//                    if UMPConsentInformation.sharedInstance.formStatus == .available {
//                        loadForm(completion)
//
//                    } else {
//                        // 无表单需要加载
//                        completion(.success(true))
//                    }
//                }
//            }
//        )
//    }
//
//    private static func loadForm(_ completion: @escaping ((Swift.Result<Bool, Error>) -> Void)) {
//        let information = UMPConsentInformation.sharedInstance
//
//        // 加载表单
//        UMPConsentForm.load(
//            completionHandler: { form, error in
//                if let error = error {
//                    // 表单加载失败
//                    completion(.failure(error))
//
//                } else {
//                    switch information.consentStatus {
//                    case .required: // 是否需要用户同意
//                        guard let controller = UIViewController.top() else {
//                            completion(.success(false))
//                            return
//                        }
//                        // 打开弹窗
//                        form?.present(
//                            from: controller,
//                            completionHandler: { error in
//                                if let error = error {
//                                    // 弹窗失败
//                                    completion(.failure(error))
//
//                                } else {
//                                    // 是否已同意
//                                    completion(.success(information.consentStatus == .obtained))
//                                }
//                            }
//                        )
//
//                    case .notRequired, .obtained: // 是否不需要用户同意。例如，用户不在 EEA 或英国
//                        completion(.success(true))
//
//                    default:
//                        completion(.success(false))
//                    }
//                }
//            }
//        )
//    }
//
//    static func reset() {
//        UMPConsentInformation.sharedInstance.reset()
//    }
//
//}

