//
//  Constants.swift
//  Plugin
//
//  Created by Rajan Arora on 15/09/23.
//  Copyright © 2023 Max Lynch. All rights reserved.
//

import Foundation

enum PPError: String {
    case env
    case ionicEnv
    case merchantId
    case flowId
    case controller
    case initSDK
    case paymentType
    case inValidType
    case json
    case orderId
    case token
    case appSchema
    case paymentMode
    case typeCaste
    case argument
    
    func getMessage() -> String {
        switch self {
        case .env:
            return "Environment not found"
        case .ionicEnv:
            return "Environment is in-correct"
        case .merchantId:
            return "Merchant Id not found"
        case .flowId:
            return "Flow Id not found"
        case .controller:
            return "Controller not found"
        case .initSDK:
            return "Initialize PhonePe SDK! first"
        case .paymentType:
            return "Payment Type not found"
        case .inValidType:
            return "Invalid type found"
        case .json:
            return "Request Json not found"
        case .orderId:
            return "Order Id not found"
        case .token:
            return "Token not found"
        case .appSchema:
            return "App Schema not found"
        case .paymentMode:
            return "Payment Mode not found"
        case .typeCaste:
            return "Type Caste Error"
        case .argument:
            return "Argument Missing"
        }
    }
}


struct PaymentRequest {
    let merchantId: String
    let orderId: String
    let token: String
    let appSchema: String
    let paymentMode: [String: Any]
}

struct Constants {
    
    //KEYS
    static let environment = "environment"
    static let enableLogs = "enableLogs"
    static let merchantId = "merchantId"
    static let flowId = "flowId"
    static let appSchema = "appSchema"
    static let status = "status"
    static let error = "error"
    static let url = "url"
    
    // Values
    static let success = "SUCCESS"
    static let failure = "FAILURE"
    static let interrupted = "INTERRUPTED"
    static let unknown = "UNKNOWN"
    static let collectDetails = "Collect Details"
    
    // Payment Details
    static let request = "request"
    static let orderId = "orderId"
    static let token = "token"
    static let type = "type"
    static let bankId = "bankId"
    static let vpa = "vpa"
    static let targetApp = "targetApp"
    static let paymentMode = "paymentMode"
    
    static let upiIntent = "UPI_INTENT"
    static let netBanking = "NET_BANKING"
    static let upiCollect = "UPI_COLLECT"
    static let ppeIntent = "PPE_INTENT"
    static let payPage = "PAY_PAGE"
}

extension String {
    func toJson() -> [String: Any]? {
        guard let jsonData = self.data(using: .utf8) else {
            return nil
        }
        return try? JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as? [String: Any]
    }
    
    func isNotEmpty() -> Bool {
        return !self.isEmpty
    }
}
