//
//  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 currentBestPlan: StoreKit.Transaction? = nil
    
    var updateListenerTask: Task<Void, Error>? = nil

    fileprivate(set) var purchases: StoredCollection<Purchase>
    
    override init() {
        
        self.purchases = Store.main.registerCollection(synchronized: true, inMemory: true)

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

        Task {
            do {
                try await self.refreshPurchasedAppleProducts()
            } catch {
                Logger.error(error)
            }
        }
    }
    
    func refreshPurchasedAppleProducts() async throws {
        
        // Iterate through the user's purchased products.
        for await verificationResult in Transaction.currentEntitlements {
            let transaction = try await self.processTransactionResult(verificationResult)
            DispatchQueue.main.async {
                NotificationCenter.default.post(name: Notification.Name.StoreEventHappened, object: nil)
            }
            await transaction.finish()
        }
    }
    
    func listenForTransactions() -> Task<Void, Error> {
        return Task.detached {
            //Iterate through any transactions which didn't come from a direct call to `purchase()`.
            for await result in Transaction.updates {
                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)")
        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)
            }

        } catch {
            Logger.error(error)
        }
        
        self._updateBestPlan()
    }

    fileprivate func _addPurchaseIfPossible(transaction: StoreKit.Transaction) throws {
        if self.purchases.hasLoadedFromServer {
            if self._purchaseById(transaction.originalID) == nil {
                let purchase: Purchase = try transaction.purchase()
                try self.purchases.addOrUpdate(instance: purchase)
            }
        }
    }
    
    fileprivate func _updatePurchaseIfPossible(transaction: StoreKit.Transaction) throws {
        if self.purchases.hasLoadedFromServer {
            if let existing: Purchase = self._purchaseById(transaction.originalID) {
                existing.revocationDate = transaction.revocationDate
                try self.purchases.addOrUpdate(instance: existing)
            }
        }
    }
    
    fileprivate func _purchaseById(_ transactionId: UInt64) -> Purchase? {
        return self.purchases.first(where: { $0.identifier == 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? {
//        return .monthlyUnlimited
//        #if DEBUG
//            return .monthlyUnlimited
//        #else
        if let currentBestPlan = self.currentBestPlan, let plan = StoreItem(rawValue: currentBestPlan.productID) {
            return plan
        }
        return nil
//        #endif
    }
    
    func userFilteredPurchases() -> [StoreKit.Transaction] {
//        Logger.log("self.purchasedTransactions = \(self.purchasedTransactions.count)")
        guard let userId = Store.main.userId, let currentUserUUID: UUID = UUID(uuidString: userId) else {
            return []
        }
        
        let userTransactions = self.purchasedTransactions.filter { currentUserUUID == $0.appAccountToken }
        
        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() {

        // Make sure the purchase has been done with the logged user
        let userPurchases = self.userFilteredPurchases()
        
        if let unlimited = userPurchases.first(where: { $0.productID == StoreItem.monthlyUnlimited.rawValue }) {
            self.currentBestPlan = unlimited
        } else if let fivePerMonth = userPurchases.first(where: { $0.productID == StoreItem.fivePerMonth.rawValue }) {
            self.currentBestPlan = fivePerMonth
        } else {
            self.currentBestPlan = nil
        }

    }
    
    fileprivate func _purchasedTournamentCount() -> Int {
        let units = self.userFilteredPurchases().filter { $0.productID == StoreItem.unit.rawValue }
        return units.reduce(0) { $0 + $1.purchasedQuantity }
    }
    
    func paymentForNewTournament() -> Tournament.TournamentPayment? {
        
        switch self.currentPlan {
        case .monthlyUnlimited: 
            return Tournament.TournamentPayment.unlimited
        case .fivePerMonth:
            if let purchaseDate = self.currentBestPlan?.originalPurchaseDate {
                let tournaments = DataStore.shared.tournaments.filter { $0.creationDate > purchaseDate && $0.payment == .subscriptionUnit && $0.isCanceled == false }
                if tournaments.count < StoreItem.five {
                    return Tournament.TournamentPayment.subscriptionUnit
                }
            }
            return self._paymentWithoutSubscription()
        default:
            return self._paymentWithoutSubscription()
        }
        
    }
    
    fileprivate func _paymentWithoutSubscription() -> Tournament.TournamentPayment? {
        let freelyPayed: Int = DataStore.shared.tournaments.filter { $0.payment == .free && $0.isCanceled == false }.count
        if freelyPayed < 1 {
            return Tournament.TournamentPayment.free
        }
        let tournamentCreditCount: Int = self._purchasedTournamentCount()
        let unitlyPayed = DataStore.shared.tournaments.filter { $0.payment == .unit && $0.isCanceled == false }.count
        if tournamentCreditCount > unitlyPayed {
            return Tournament.TournamentPayment.unit
        }
        return nil
    }
    
    var remainingTournaments: Int {
        let unitlyPayed = DataStore.shared.tournaments.filter { $0.payment == Tournament.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() {
        self.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 = Store.main.userId else {
            throw StoreError.missingUserId
        }
        return Purchase(user: userId,
                        identifier: self.originalID,
                        purchaseDate: self.purchaseDate,
                        productId: self.productID,
                        quantity: self.purchasedQuantity)

    }
    
}
