//
//  Guard.swift
//  Poker Analytics 6
//
//  Created by Laurent Morvillier on 20/04/2022.
//

import Foundation
import StoreKit
import LeStorage

@available(iOS 15, *)
@objc class Guard: NSObject {
    
    static var main: Guard = Guard()
    
    @Published private(set) var purchasedTransactions = Set<StoreKit.Transaction>()

    var currentBestPurchase: Purchase? = nil
    
    var updateListenerTask: Task<Void, Never>? = nil
    
    fileprivate let _freeTournaments: Int = 3
    
    override init() {

        super.init()
        
        self.updateListenerTask = self.listenForTransactions()

        Task {
            do {
                try await self.refreshPurchasedAppleProducts()
            } catch {
                Logger.error(error)
            }
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(collectionDidLoad), name: NSNotification.Name.CollectionDidLoad, object: nil)

    }
    
    deinit {
        self.updateListenerTask?.cancel()
        NotificationCenter.default.removeObserver(self)
    }
    
    @objc func collectionDidLoad(notification: Notification) {
        if let _ = notification.object as? StoredCollection<Purchase> {
            self._updateBestPlan()
        }
    }
    
    func productIds() async -> [String] {
        var productIds: [String] = []
        for await result in Transaction.all {
            do {
                let verified = try self.checkVerified(result)
                productIds.append(verified.productID)
            } catch {
                Logger.error(error)
            }
        }
        return productIds
    }
    
    func refreshPurchasedAppleProducts() async throws {
        
        // Iterate through the user's purchased products.
        for await verificationResult in Transaction.currentEntitlements {
            let transaction = try await self.processTransactionResult(verificationResult)
            print("processs product id = \(transaction.productID)")
            DispatchQueue.main.async {
                NotificationCenter.default.post(name: Notification.Name.StoreEventHappened, object: nil)
            }
            await transaction.finish()
        }
    }
    
    func listenForTransactions() -> Task<Void, Never> {
        return Task(priority: .background) {
            //Iterate through any transactions which didn't come from a direct call to `purchase()`.
            for await result in Transaction.updates {
//                Logger.log(">>> update = \(result)")
                do {
                    let transaction = try self.checkVerified(result)

                    //Deliver content to the user.
                    await self.updatePurchasedIdentifiers(transaction)

                    //Always finish a transaction.
                    await transaction.finish()
                } catch {
                    Logger.error(error)
                    //StoreKit has a receipt it can read but it failed verification. Don't deliver content to the user.
                    print("Transaction failed verification")
                }
            }
        }
    }
    
    func checkVerified<T>(_ result: VerificationResult<T>) throws -> T {
        //Check if the transaction passes StoreKit verification.
        switch result {
        case .unverified:
            //StoreKit has parsed the JWS but failed verification. Don't deliver content to the user.
            throw StoreManagerError.failedVerification
        case .verified(let safe):
            //If the transaction is verified, unwrap and return it.
            return safe
        }
    }
    
    @MainActor
    func updatePurchasedIdentifiers(_ transaction: StoreKit.Transaction) async {
        
//        Logger.log("\(transaction.productID) > purchase = \(transaction.originalPurchaseDate), exp date= \(transaction.expirationDate), rev date = \(transaction.revocationDate)")
        
//        Logger.log("purchase date = \(transaction.purchaseDate)")
        
        do {
            if transaction.revocationDate == nil {
                // If the App Store has not revoked the transaction, add it to the list of `purchasedIdentifiers`.
                purchasedTransactions.insert(transaction)
                
//                try self._addPurchaseIfPossible(transaction: transaction)
            } else {
                // If the App Store has revoked this transaction, remove it from the list of `purchasedIdentifiers`.
                purchasedTransactions.remove(transaction)
//                try self._updatePurchaseIfPossible(transaction: transaction)
            }

            try self._updatePurchase(transaction: transaction)
            
        } catch {
            Logger.error(error)
        }
        
        self._updateBestPlan()
    }
    
    fileprivate func _updatePurchase(transaction: StoreKit.Transaction) throws {
        let purchases = DataStore.shared.purchases
        if let purchase = self._purchaseByTransactionId(transaction.originalID) {
            purchase.revocationDate = transaction.revocationDate
            purchase.expirationDate = transaction.expirationDate
            purchase.purchaseDate = transaction.purchaseDate
            purchase.productId = transaction.productID
            try purchases.addOrUpdate(instance: purchase)
        } else {
            let purchase: Purchase = try transaction.purchase()
            try purchases.addOrUpdate(instance: purchase)
        }
    }

//    fileprivate func _addPurchaseIfPossible(transaction: StoreKit.Transaction) throws {
//        
//        let purchases = DataStore.shared.purchases
//        
//        if self._purchaseByTransactionId(transaction.originalID) == nil {
//            let purchase: Purchase = try transaction.purchase()
//            try purchases.addOrUpdate(instance: purchase)
//        }
//    }
//    
//    fileprivate func _updatePurchaseIfPossible(transaction: StoreKit.Transaction) throws {
//        let purchases = DataStore.shared.purchases
//        if let existing: Purchase = self._purchaseByTransactionId(transaction.originalID) {
//            existing.revocationDate = transaction.revocationDate
//            try purchases.addOrUpdate(instance: existing)
//        }
//    }
    
    fileprivate func _purchaseByTransactionId(_ transactionId: UInt64) -> Purchase? {
        let purchases = DataStore.shared.purchases
        return purchases.first(where: { $0.id == transactionId })
    }
    
    func processTransactionResult(_ result: VerificationResult<StoreKit.Transaction>) async throws -> StoreKit.Transaction {
        
        let transaction = try checkVerified(result)

        // Deliver content to the user.
        await updatePurchasedIdentifiers(transaction)

        return transaction
    }
    
    var currentPlan: StoreItem? {
//        #if DEBUG
//        return .monthlyUnlimited
//        #elseif TESTFLIGHT
//            return .monthlyUnlimited
//        #elseif PRODTEST
//            return .monthlyUnlimited
//        #else
        if let currentBestPurchase = self.currentBestPurchase, let plan = StoreItem(rawValue: currentBestPurchase.productId) {
            return plan
        }
        return nil
//        #endif
    }
    
    func userFilteredPurchases() -> [StoreKit.Transaction] {
//        Logger.log("self.purchasedTransactions = \(self.purchasedTransactions.count)")
        guard let userId = StoreCenter.main.userId, let currentUserUUID: UUID = UUID(uuidString: userId) else {
            return []
        }
        
        let userTransactions = self.purchasedTransactions.filter { currentUserUUID == $0.appAccountToken || $0.appAccountToken == nil }
        
        let now: Date = Date()
//        print("now = \(now)")
        return userTransactions.filter { transaction in
            if let expirationDate = transaction.expirationDate {
//                print("exp = \(expirationDate)")
                return expirationDate > now
            } else {
                return true
            }
        }
        
    }
    
    /// Update best plan by filtering Apple purchases with registered purchases by the user
    fileprivate func _updateBestPlan() {

        var purchases: [Purchase] = []
        
        // Make sure the purchase has been done with the logged user
        let userPurchases = self.userFilteredPurchases().compactMap { try? $0.purchase() }
        purchases.append(contentsOf: userPurchases)
        
        let validPurchases = DataStore.shared.purchases.filter { $0.isValid() }
        Logger.log("valid purchases = \(validPurchases.count)")
        purchases.append(contentsOf: validPurchases)
        
        if let purchase = purchases.first(where: { $0.productId == StoreItem.monthlyUnlimited.rawValue }) {
            self.currentBestPurchase = purchase
        } else if let purchase = purchases.first(where: { $0.productId == StoreItem.fivePerMonth.rawValue }) {
            self.currentBestPurchase = purchase
        }
        
    }
    
    fileprivate func _purchasedTournamentCount() -> Int {
        
        let purchases = DataStore.shared.purchases

        let units = purchases.filter { $0.productId == StoreItem.unit.rawValue }
        return units.reduce(0) { $0 + ($1.quantity ?? 0) }
        
//        let units = self.userFilteredPurchases().filter { $0.productID == StoreItem.unit.rawValue }
//        return units.reduce(0) { $0 + $1.purchasedQuantity }
    }
    
    func paymentForNewTournament() -> TournamentPayment? {
        
        switch self.currentPlan {
        case .monthlyUnlimited: 
            return TournamentPayment.unlimited
        case .fivePerMonth:
            if let purchaseDate = self.currentBestPurchase?.purchaseDate {
                let tournaments = DataStore.shared.tournaments.filter { $0.creationDate > purchaseDate && $0.payment == .subscriptionUnit && $0.isCanceled == false }
                if tournaments.count < StoreItem.five {
                    return TournamentPayment.subscriptionUnit
                }
            }
            return self._paymentWithoutSubscription()
        default:
            return self._paymentWithoutSubscription()
        }
        
    }
    
    fileprivate func _paymentWithoutSubscription() -> TournamentPayment? {
        let freelyPayed: Int = DataStore.shared.tournaments.filter { $0.payment == .free && $0.isCanceled == false }.count
        if freelyPayed < self._freeTournaments {
            return TournamentPayment.free
        }
        let tournamentCreditCount: Int = self._purchasedTournamentCount()
        let unitlyPayed = DataStore.shared.tournaments.filter { $0.payment == .unit && $0.isCanceled == false }.count
        if tournamentCreditCount > unitlyPayed {
            return TournamentPayment.unit
        }
        return nil
    }
    
    var remainingTournaments: Int {
        let unitlyPayed = DataStore.shared.tournaments.filter { $0.payment == TournamentPayment.unit }.count
        let tournamentCreditCount = self._purchasedTournamentCount()
//        let notDeletedTournamentCount = DataStore.shared.tournaments.filter { $0.isDeleted == false }.count
        
//        Logger.log("unitlyPayed = \(unitlyPayed), purchased = \(tournamentCreditCount) ")
        return tournamentCreditCount - unitlyPayed
    }
    
    func disconnect() {
        let purchases = DataStore.shared.purchases
        purchases.reset()
    }
    
}

struct PurchaseRow: Identifiable {
    var id: UInt64
    var name: String
    var item: StoreItem
    var quantity: Int?
}

fileprivate extension StoreKit.Transaction {
    
    func purchase() throws -> Purchase {
        guard let userId = StoreCenter.main.userId else {
            throw StoreError.missingUserId
        }
        
        return Purchase(transactionId: self.originalID,
                        user: userId,
                        purchaseDate: self.purchaseDate,
                        productId: self.productID,
                        quantity: self.purchasedQuantity,
                        revocationDate: self.revocationDate,
                        expirationDate: self.expirationDate)

    }
    
}
