//
//  StoreView.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 17/02/2023.
//

import SwiftUI
import StoreKit

fileprivate enum Feature: Int, Identifiable, CaseIterable {
    case unlimitedTimers
    case allSounds
    case longSounds
    case allStats
    
    var id: Int { self.rawValue }
    
    var localizedString: String {
        switch self {
        case .unlimitedTimers: return NSLocalizedString("Unlimited timers and stopwatches", comment: "")
        case .allSounds: return NSLocalizedString("Access all the sound library", comment: "")
        case .longSounds: return NSLocalizedString("Access long version of sounds", comment: "")
        case .allStats: return NSLocalizedString("See all your activities in detail", comment: "")
        }
    }
    
}

struct StoreView: View, StoreDelegate {
    
    @StateObject var store: Store = Store()
    
    @State var errorMessage: String? = nil
    
    init() {
        
        self.store.delegate = self
        
//        if SKPaymentQueue.canMakePayments() {
//            self._store = Store(delegate: self)
//        } else {
//            self.errorMessage = NSLocalizedString("In-app purchase disabled", comment: "")
//        }
        
    }
    
    var body: some View {
        
        if let product = self.store.products.first {
            
            PlanView(productName: product.displayName,
                     price: product.displayPrice) {
                self._purchase()
            }
            
        } else {
            ProgressView()
                .progressViewStyle(.circular)
        }
        
    }
    
    fileprivate func _purchase() {
        
        if let product = self.store.products.first {
            Task {
                try await store.purchase(product)
            }
        }
        
    }
    
    // MARK: - StoreDelegate
    
    func productsReceived() {
        
    }
    
    func errorDidOccur(error: Error) {
        
    }
    
}

struct PlanView: View {
    
    var productName: String
    var price: String
    
    var actionHandler: () -> ()
    
    var body: some View {
        
        VStack {
            
            Text(productName)
                .font(.title)
                .fontWeight(.bold)
                .padding()
            
            Group {
                ForEach(Feature.allCases) { feature in
                    HStack {
                        Text(feature.localizedString)
                        Spacer()
                        Image(systemName: "checkmark.circle.fill")
                            .foregroundColor(.accentColor)
                    }.fontWeight(.medium)
                }
            }
            .padding(.horizontal, 24.0)
            .padding(.vertical, 2.0)
            
            Spacer()
            
            Button {
                self.actionHandler()
            } label: {
                HStack {
                    Spacer()
                    VStack {
                        Text("Purchase")
                        Text(self.price).font(.title3)
                    }
                    Spacer()
                }
            }
            .font(.title)
            .buttonStyle(.borderedProminent).fontWeight(.medium)
            
        }.padding()
    }
    
}

struct StoreView_Previews: PreviewProvider {
    static var previews: some View {
        PlanView(productName: "Pro version",
                 price: "$0.99 / month",
                 actionHandler: {})
    }
}
